$(document).ready(function(){

	// When the user clicks the load-form link
	$(".load-form").click(function(){
	 
		// Hide load-form link, and show contact form
		$(".load-form").fadeOut("normal", function(){
			// Animate form into view
			$(".email-form-box").show();				
		});
		
		// Show loading thingy
		$(".status-box").slideDown();
	 
	 	// Asyncronously load the form
		$(".email-form-box").load("mail-form.php", {}, function(){
			
			// Setup form submission code
		 	$(".btnSubmit").click(function(){
				$("#contactForm").submit();
			});
			
			// Setup fancy button overlay
			$(".btnSubmit").hover(
				function(){
					$(this).addClass("btnSubmitON");
				}, 
				function(){
					$(this).removeClass("btnSubmitON");
				}
			);
			
			// Verification prior to submitting
			$("#contactForm").submit(function(){
			 
			 	$(".error-box").slideUp("fast", function(){
					$(".status-box").slideDown();
				});
				
				// Gather the details
				var senderName = $("#sender-name");
				var senderEmail = $("#sender-email");
				var senderPhone = $("#sender-phone");
				var senderMessage = $("#sender-message");
				
				// Send the details
				$.post(
					"send-email.php", 
					{
					 	name:senderName.val(), 
						email:senderEmail.val(), 
						phone:senderPhone.val(), 
						message:senderMessage.val() 
					},
					function (data){
					 
						if (data == "1") {
							
							$(".email-form-box").slideUp("slow", function(){
								
								$(".error-box").html("<p>Email Sent.</p>");
								$(".status-box").fadeOut("normal", function(){
									$(".error-box").fadeIn();	
								});
								
							});
							
						} else {
						 
							$(".error-box").html(data);	
							$(".status-box").slideUp("normal", function(){
								$(".error-box").slideDown();
							});	
														
						}
					 
					}
				);
				
				return false;
				
			});
			
			// Fix fancy insets
			fancyInsets();
			
			// Hide loading thingy
			$(".status-box").slideUp();
					
		});
		
		// Don't allow default link activity
		return false;
		
	});
		
});

function fancyInsets() {
 
	$("div.fancy-inset").each(function(){
		var content = $(this).html();
		$(this).html("<div class='a'><div class='b'><div class='c'><div class='d'><div class='fancyLifter'>" + content + "</div></div></div></div><span class='cornerA'></span><span class='cornerB'></span><span class='cornerC'></span><span class='cornerD'></span></div>");
		
		// No idea why, but IE won't focus on the inputs automatically
		$(this).click(function(){
			$(this).find(".styler").focus();
		});
		
	});
}