$(document).ready(function(){
	
	//handle homepage bucket mouse overs
	$(".homepage_bucket").mouseenter(function(){
		
		$(this).children(".title_zone").children("p").stop();
		$(this).children(".title_zone").children("p").animate({left: '10'}, 400, function(){});
		return false;
		
	}).mouseleave(function(){
		
		$(this).children(".title_zone").children("p").stop();
		$(this).children(".title_zone").children("p").animate({left: '0'}, 400, function(){});
		return false;
		
	});
	
	//handle the biographys page
	$("#bio_list").children("li").children("h3").mouseenter(function(){
		
		$(this).css("cursor", "pointer");
		
	}).mouseleave(function(){
		
		$(this).css("cursor", "auto");
		
	});
	
	$("#bio_list").children("li").children("h3").click(function(){
		
		if( $(this).parent().children("ul").is(":visible") ){
			
			$("#bio_list").children("li").each(function(){
				
				$(this).children("ul").hide("fast");
				$(this).children("h3").css("color", "#5F9832");
				$(this).children("h3").css("text-decoration", "none");
				
			});
			
		} else {
			
			
			$("#bio_list").children("li").each(function(){
				
				$(this).children("ul").hide("fast");
				$(this).children("h3").css("color", "#5F9832");
				$(this).children("h3").css("text-decoration", "none");
				
			});
			
			$(this).css("color", "#FFFFFF");
			$(this).css("text-decoration", "underline");
			$(this).parent().children("ul").show("fast");
			
		}
		
	});
	
	
});

//handle form submission
function handleMailListJoin(){
	
	var emailAddy = $("#userEmailAddy").val();
	if(emailAddy == "" || !isValidEmailAddress(emailAddy)){
		alert('Please enter a valid email address');
		return false;
	}
	
	var datastring = "email_address="+emailAddy;
	
	$.ajax({
		type: "POST",
		url: "./includes/scripts/joinMailList.php",
		data: datastring,
		success: function(loadData){
			
			if(loadData.indexOf('<entry xmlns="http://www.w3.org/2005/Atom">') != -1){
				alert('Thank You for joining the mailing list');
			} else {
				alert('There was an error adding you to the mailling list');
			}
			
		}
	});
	
	
}

function shareByEmail(){
	
	var senderEmail = $("#yourEmailInput").val();
	var friendEmail = $("#friendsEmailInput").val();
	
	if(senderEmail == "" || isValidEmailAddress(senderEmail) == false){
		alert('Please enter a proper email address for the sender');
		return false;
	}
	
	if(friendEmail == "" || isValidEmailAddress(friendEmail) == false){
		alert('Please enter a proper email address for the friend');
		return false;
	}
	
	var datastring = "sender="+senderEmail+"&friend="+friendEmail;
	
	$.ajax({
		type: "POST",
		url: "./includes/scripts/shareByEmail.php",
		data: datastring,
		success: function(loadData){
			
			if(loadData == 'ok'){
				alert('Your email has been sent');
			} else {
				alert('There was a problem sending your email. Please try again');
			}
			
		}
	});
	
}

function handleContactUs(){
	
	var senderName  = $("#contactSenderName").val();
	var senderEmail = $("#contactSenderEmail").val();
	var senderSubject = $("#contactSenderSubject").val();
	var senderMessage = $("#contactSenderMessage").val();
	var sendTo = $("#contact").val();
	
	if(senderEmail == "" || isValidEmailAddress(senderEmail) == false){
		alert('Please enter a proper email address for the sender');
		return false;
	}
	
	if(senderName == ""){
		alert('Please enter a Name');
		return false;
	}
	
	if(senderSubject == ""){
		alert('Please enter a Subject');
		return false;
	}
	
	if(senderMessage == ""){
		alert('Please enter a Message');
		return false;
	}
	
	if(sendTo == ""){
		alert('Please choose someone to send the email to');
		return false;
	}
	
	var datastring = "sender="+senderEmail+"&name="+senderName+"&subject="+senderSubject+"&message="+senderMessage+"&sendto="+sendTo;
	
	$.ajax({
		type: "POST",
		url: "./includes/scripts/contactUs.php",
		data: datastring,
		success: function(loadData){
			
			if(loadData == 'ok'){
				alert('Your email has been sent');
			} else {
				alert('There was a problem sending your email. Please try again');
			}
			
		}
	});
	
}


//show the share by email form
function showShareForm(){
	
	if( $("#shareForm").is(":visible") ){
		$("#shareForm").hide("fast");
	} else {
		$("#shareForm").show("fast");
	}
	
}


//regex validate email addresses
function isValidEmailAddress(emailAddress) {

	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

	return pattern.test(emailAddress);

}



