/*
*
*	custom.js
*
*	by Nils Haebel
*	www.nilshaebel.com
*
*/



// Basic IE HTML5 compatibility
elements = [ "article", "aside", "command", "datalist", "figure", "figcaption", "footer", "header", "hgroup", "menu", "nav", "section", "source", "summary" ];

for(var i = 0, l = elements.length; i < l; i++)
{
	document.createElement(elements[i]);
}



$(document).ready(function()
{

	if(($.browser == "msie" && $.browser.version < 8.0) || ($.browser == "mozilla" && version < 3.0))
	{
		alert("You are using an old version of your browser. Please update it to the latest version.");
	}

	// Hide url Bar (iPhone)
	if(navigator.userAgent.indexOf('iPhone') != -1)
	{
		window.scrollTo(0,1);
	}
	
	
	// Hide contact form
	$("#contact-form").hide();

	
	// Show contact form (via link)
	$(".contact-form-link").click(function()
	{
		$("#contact-form").hide().fadeIn(250);
	});
	
	
	// Show contact form (via url)
	if(location.hash=="#contact-form")
	{
		$("#contact-form").hide().fadeIn(250);
	}
	
	
	// Form submit
	$("#form_submit").click(function()
	{
		var fname = $("#form_name").val() + "";
		var femail = $("#form_email").val() + "";
		var fmessage = $("#form_message").val() + "";
		
		// Validate input
		if(fname=="" || femail=="" || !femail.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/) || fmessage=="")
		{
			$("body").append('<div class="alert">Please fill out all fields correctly!</div>');
			$(".alert").hide().fadeIn(250).delay(2500).fadeOut(500,function(){ $(this).remove(); });
		}
		else
		{
			// Send data via POST
			$.post("sendmail.php", { form_name: fname, form_email: femail, form_message: fmessage }, function(data){
				$("body").append('<div class="alert">' + data + '</div>');
				$(".alert").hide().fadeIn(250).delay(2500).fadeOut(500,function(){ $(this).remove(); });
				$("#contact-form").fadeOut(250);
			});
		}

		return false;
	});
	
	
	// Form reset
	$("#form_reset").click(function()
	{
		if($("#form_name").val() == "" && $("#form_email").val() == "" && $("#form_message").val() == "")
		{
			$("#contact-form").fadeOut(250);
			location.hash = "#";
		}
		else if(confirm("Are you sure that you want to cancel?"))
		{
			$("#contact-form").fadeOut(250);
			location.hash = "#";
			return true;
		}

		return false;
	});
	
	
	// Hide form layer via esc
	$(window).keydown(function(e){
		if(e.keyCode == 27)
		{
			$("#contact-form").fadeOut(250);
			location.hash = "#";
		}
	});
	
});
