$(document).ready(function(){  

//set H2 click/animate state
$("H2").click(function() {
	$(".builderBar ul").slideUp("slow");
	$(this).parent().find("ul").slideDown("slow");
	$(".builderBar h2 a").css("color", "#333333");
	$(this).parent().find("h2 a").css("color", "#2762a8");
});	

//Set hover state for H2 & images
//$("H2 a").hover(function(){$(this).css("color", "#333");},function(){$(this).css("color", "#ccc");});
$(".builderBar img").hover(function(){$(this).css("border-color", "#2762a8");},function(){$(this).css("border-color", "#ccc");});


//Set image option states
$(".builderBar img").click(function(){
	$(this).parent().parent().find("img").css("opacity", "1");
	$(this).animate({opacity: 0.4}, 500);
	
	var newPicSrc = $(this).attr("src").replace('-thumbs', '');
	var parentOption = $(this).parent().parent().parent().attr('class');	
	
	if(parentOption != 'user-background'){
		$("#"+parentOption+"-hidden").val(newPicSrc);
		var parentOption = parentOption+"-display";
		$("."+parentOption).attr("src", newPicSrc);		
	} else {
		$("#emailPreview-hidden").val(newPicSrc);
		$(".emailPreview").css("background-image", "url("+newPicSrc+")");		
	}
});

//update form values on keypress/blur
$('#recipient-name').keyup(function(){updateDisplay("to",$(this).val())});
$('#your-name').keyup(function(){updateDisplay("fro",$(this).val())});
$('#your-message').keyup(function(){updateDisplay("user-message-display",$(this).val())});
$('#recipient-name').blur(function(){updateDisplay("to",$(this).val())});
$('#your-name').blur(function(){updateDisplay("fro",$(this).val())});
$('#your-message').blur(function(){updateDisplay("user-message-display",$(this).val())});

$('#recipient-email').focus(function(){if($(this).val() == "Please enter an email"){$(this).val('');}});

function updateDisplay(cl, val){if(val != undefined){	$("."+cl).replaceWith('<span class="'+cl+'">'+val+'</span>');}}

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);
}

//form Validation and Send
$("#formMessage").submit(function() {
	var containsErrors = false;
	var errorMessages = new Array();
	var missingInformation = false;
	var errorList ='';	
	
	//manage error handlers
	if(!isValidEmailAddress($('#recipient-email').val())){	$('#recipient-email').addClass("error"); errorMessages.push('Please enter a valid email address for your recipient');}
	if(!isValidEmailAddress($('#your-email').val())){$('#your-email').addClass("error");	errorMessages.push('Please enter a valid email address for yourself');}
	if(containsErrors == true){errorMessages.push('Please enter text for the highlighted fields');}
	
	//compose error message or send AJAX
	if(errorMessages.length > 0){
		$(errorMessages).each(function (i) {errorList += '<li>'+errorMessages[i]+'</li>';});
		$(".custom-error").html("<ul>"+errorList+"</ul>");
		jQuery.facebox($('#submission-error').show());
		return false;	
	} else {
		var datastring = '&stamp='+$('#user-stamp-hidden').val()
		+'&bkgd='+$('#emailPreview-hidden').val()
		+'&picture='+$('#user-picture-hidden').val()
		+'&r_name='+$('#recipient-name').val()
		+'&r_email='+$('#recipient-email').val()
		+'&y_name='+$('#your-name').val()
		+'&y_email='+$('#your-email').val()
		+'&msg='+$('#your-message').val()	
		+'&newsletter='+$('#newsletter:checked').val()
		+'&copy='+$('#send-copy:checked').val()	;
		$.ajax({type: "POST", url: "Scripts/process-card.php", data: datastring, complete: function(data){jQuery.facebox($("#confirmation-message").show());}});
		return false;				
	}
 });


//Set defaults
$('a[rel*=facebox]').facebox();
$(".builderBar").find("ul").hide();
$(".builderBar").find("ul:first").show();
$(".builderBar").find("h2 a:first").css("color", "#2762a8"); 
jQuery.facebox($("#welcome-message").show());


//end on ready	
});