var contactActive = 0;
var internetExplorer = 0;

$(document).ready(function(){	
	
	// detect IE
	if (navigator.appName == 'Microsoft Internet Explorer'){
		
		internetExplorer = 1;
		
	}
		
	// bind contact buttons
	$("#contact").click(function(e){ 

		// prevent mailto link, used as fallback fron non JS browsers
		e.preventDefault();
		openContact(); 
		
	});	

	$("#close").click(function(e){
		
		e.preventDefault();
		closeContact();
		
	});
	
	// contact submit
	$("#submit-btn").click(function(e){
		
		e.preventDefault();
		submitForm();
		
	});
	
	// contact input placeholders
	$("#contact-form input, #message").focus(function(){
		
		if ($(this).val() == "Name" || $(this).val() == "Email" || $(this).val() == "Your query"){
		
			$(this).val("");
		}
	});
	
	$("#name").blur(function(){
		
		if ($(this).val() == ""){ $(this).val("Name"); }
		
	});
	
	$("#email").blur(function(){
		
		if ($(this).val() == ""){ $(this).val("Email"); }
		
	});
	
	$("#message").blur(function(){
		
		if ($(this).val() == ""){ $(this).val("Your query"); }
		
	});
	
});	

function openContact(){
	
	contactActive = 1;
	
	// show overlay
	$("body").append("<div id='overlay'></div>");
	$("#overlay").animate({ opacity: "0.8" }, 600);
	
	// hide scrollbars
	$("body").css({ overflow: "hidden" });
	
	// move contact form into browser and fade in
	$("#contact-form").css({ top: "+=510px" }).animate({ opacity: "1" }, 620)
	
		// add close button
	 if (internetExplorer){
	 	
	 	// remove default close button
	 	$("#close").remove();
	 	
	 	// add close button and tooltip outside of lightbox
	 	$("body").append("<a id='close'></a>");
	 
	 	// determine position
	 	var offset = $("#contact-form").offset();
	 	var yPos = offset.top - 12;
	 	var xPos = offset.left + $("#contact-form").width() + 60;
	 		
	 	// position close button
	 	$("#close").css({ top: yPos, left: xPos }).click(closeContact);
	 	
	 }

}

function closeContact(){
	
	contactActive = 0;

	if (internetExplorer){
		
		$("#close").remove();
		
	}

	// fade out contact form
	$("#contact-form").animate({ opacity: "0" }, 620, function(){
			
			// move out of viewport and remove overlay
			$(this).css({ top: "-=510px" })
			$("#overlay").animate({ opacity: "0" }, function(){
				
				$(this).remove();
				$("#close").remove();
				
			});
					
			// show scrollbars
			$("body").css({ overflow: "auto" });
	
	});
}

function submitForm(){
	
		// get field values
		var name = $("#name").val();
		var email = $("#email").val();
		var message = $("#message").val();
		
		// check for blank fields
		if (name == "" || name == "Name" || email == "email" || email == "Email" || 
				message == "" || message == "Your query"){
					
				$("#contact-form").css({ height: "+=10px"}).append("<p class='error'>* You must fill out each of the fields above</p>");			
					
		}
		else {
			
			// send data to mailer script
			$.ajax({ type: "POST", url: "mailer.php", data: "name=" + name + "&email=" + email + "&message=" + message,
			success: function(response){
				
				// clear contact box
				$("#contact-form").children(":not(h1, a)").remove();
				
				// display response message
				if (response){
					
					$("#contact-form").append("<p>Thank you. Your message has been sent.</p>");
					
				}
				else {
					
					$("#contact-form").append("<p>Sorry. There was a problem sending your email, please try again.</p>");
					
				}
	
			}});
		}
}
