$(document).ready(function(){
	//THIS IS THE FORM AUTO EMPTY
	var isYou = 0;
	var isFriend = 0;
	
	$("#yourEmail").focus(function(){
		if(isYou == 0) {
			$(this).val('');
			isYou = 1;
		}
	});
	
	$("#friendEmail").focus(function(){
		if(isFriend == 0) {
			$(this).val('');
			isFriend = 1;
		}
	});
	//END FORM AUTO EMPTY********************************
	
	//TELL BUTTON CLICK FUNCTION*************************
	//close popup
	$('#darkBG').css({"opacity":"0.5"});
	$('#darkBG').click(function() {
			$(this).hide();
			$('.pop').hide();
	});
	$('.closeButton').click(function() {
			$('#darkBG').hide();
			$('.pop').hide();
			return false;
	});
	
	//pops
	$('#tellButton').click(function() {
		$("#fYourEmail").val($("#yourEmail").val());
		$("#fFriendEmail").val($("#friendEmail").val());
		$('#darkBG').fadeIn("normal",function(){
			center_popup($('#tellFriendPop'));
		});
		return false;
	});
	//END TELL BUTTON CLICK FUNCTION*********************
	//EMAIL US BUTTON************************************
	$("#sEmailButton").click(function(){
		$("#tellFriendPop").html("<h1>Sending Email</h1>");
	
		//defign the variables
		var userEmail = $("#yourEmail").val();
		var friendEmail = $("#friendEmail").val();
		var subject = $("#EmailSubject").val();
		var messageText = $("#content").val();
		var transmitSuccess = 0;
			alert(subject);
		$.ajax({
			type: "POST",
			url: "bin/tellFriend.php",
			data: {'userEmail': userEmail,'friendEmail': friendEmail,'subject': subject,'messageText': messageText},
			success: function(data) {
				if(data == 'sent') {
					$("#tellFriendPop").html("<h1>Message Sent</h1><br/><input type='submit' id='closeMe' value='Close'/>");
					transmitSuccess = 1;
				}else{
					$("#tellFriendPop").html("<h1>There was a server error, please hit refresh and try again.</h1>");
					transmitSuccess = 0;
				}
			},
			complete: function() {
				setTimeout(function() {
						$('#darkBG').fadeOut();
						$('.pop').fadeOut();
						if (transmitSuccess == 0) {
							
						}
				},5000);
				
				
			}
		});
		return false;
	});
	//END EMAIL US BUTTON********************************
});

//FUNCTIONS
function center_popup(element) {
	var win_height = document.documentElement.clientHeight;
	var win_width = document.documentElement.clientWidth;
	var pop_height = element.height();
	var pop_width = element.width();
	
	element.css({
		"top": win_height/2-pop_height/2,
		"left": win_width/2-pop_width/2
	});
	
	element.fadeIn("normal");
}
