function centerOverlay() {
	var overlay = $('#overlay');
	var windowWidth = $(window).width();  
	var windowHeight = $(window).height();  
	var overlayHeight = overlay.height();
	var overlayWidth = overlay.width();

	overlay.css({
		"position": "absolute",
		"top": windowHeight/2 - overlayHeight/2 - 36,
		"left": windowWidth/2-overlayWidth/2 - 6
	});

	$("#overlayBg").css({
		width: $(window).width(),
		height: $(window).height()
	});
}
		
$(function () {

	//Iterate inline form labels
	$("#overlay form input").each(function (type) {
		$(this).focus(function () {
			$(this).prev("label").addClass("focus");
		});
		 
		$(this).keypress(function () {
			$(this).prev("label").addClass("hasText").removeClass("focus");
		});
		 
		$(this).blur(function () {
			if($(this).val() == "") {
				$(this).prev("label").removeClass("hasText").removeClass("focus");
			}
		});
	});
	//Repeated for textarea
	$("#overlay form textarea").each(function (type) {
		$(this).focus(function () {
			$(this).prev("label").addClass("focus");
		});
		 
		$(this).keypress(function () {
			$(this).prev("label").addClass("hasText").removeClass("focus");
		});
		 
		$(this).blur(function () {
			if($(this).val() == "") {
				$(this).prev("label").removeClass("hasText").removeClass("focus");
			}
		});
	});
	
	//Enable overlay
	$("#contact").click(function(event) {
		$('#contactBubble').css({display: 'none'});
		centerOverlay();

		$("#overlay").fadeIn(200);
		$("#overlayBg").fadeIn(200);
	});
	
	//Close option
	$("#close").click(function(event) {
		$("#overlay").fadeOut(200);
		$("#overlayBg").fadeOut(200);
	});
	
	//2nd close option
	$("#overlayBg").click(function() {
		$("#overlay").fadeOut(200);
		$("#overlayBg").fadeOut(200);
	});
	
	//Keeping one resize function, do actions for image and overlay
	$(window).resize(function() {
		var img = $('#loader div.current img.current');
    	_size_image(img);
    	_center_image(img);
		
		centerOverlay();
		
		centerLoading($('#loading-image'));
	});


	//Form submission
	$("#submit").click(function(){
		$(".error").hide();
		$("form label").removeClass(hasError);
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		//Validate name field
		var nameVal = $("#name").val();
		if(nameVal == '') {
			$("#label_name").addClass("hasError").append('<span class="error">Don\'t be shy, identify yourself.</span>');
			hasError = true;
		}
		
		//Valide email field
		var emailVal = $("#email").val();
		if (emailVal == '') {
			$("#label_email").addClass("hasError").append('<span class="error">Please enter an email address. It won\'t be shared.</span>');
			hasError = true;
		} else if (!emailReg.test(emailVal)) {
			$("#label_email").addClass("hasError").append('<span class="error">Your email address was not valid. Nice try...</span>');
			hasError = true;
		}
		
		//Valide message field
		var messageVal = $("#message").val();
		if (messageVal == '') {
			$("#label_message").addClass("hasError").append('<span class="error">Lost for words? I understand, but do your best!</span>');
			hasError = true;
		}
		
		//Test message
		if(hasError == false) {
			$("label").addClass("hasText");
			$("#submit").addClass("sending");
			$.post("send_email.php",
			   { name: nameVal, email: emailVal, message: messageVal },
			   	function(data){
					$("form").html('<div id="confirmation">Message sent successfully. Thank you!</div>');
				}
			);
		} else {
			$("form input").each(function() {
				if ($(this).prev("label").hasClass("hasError")) {
					$(this).val("");
				} else {
					$(this).prev("label").addClass("hasText")
				}
			});
			$("form textarea").each(function() {
				if ($(this).prev("label").hasClass("hasError")) {
					$(this).val("");
				} else {
					$(this).prev("label").addClass("hasText")
				}
			});
		}
	});
});
