$(function() {
	$("#site-nav .popout").hover(function() {
		$(this).find(".sub").show();
	},
	function() {
		$(this).find(".sub").fadeOut(500);
	});
});

$(function() {
	$("#site-nav .sub li").hover(function() {
		$(this).find(".info").show();
	},
	function() {
		$(this).find(".info").fadeOut(500);
	});
});

$(function() {
	$(".contact-form form").submit(function() {
		var form = $(this);
		
		$.ajax({
			url: "/ajax/contact.ajax.php",
			type: "POST",
			data: ({
				name: form.find("input[name='name']").val(),
				email: form.find("input[name='email']").val(),
				phone: form.find("input[name='phone']").val(),
				message: form.find("textarea[name='message']").val(),
				code: form.find("input[name='code']").val()
			}),
			dataType: "json",
			success: function(json) {
				if (json.errorMessage) {
					form.find(".message").html("<p>" + json.errorMessage + "</p>");
					form.find(".message").css("color", "#F00");
					form.find(".message").slideUp(function() {
						$(this).slideDown();
					});
					
					form.find(".captcha").attr('src', '/captcha/?' + new Date().getTime());
					
					form.find("input[name='code']").val("");
				}
				else {
					form.find(".message").html("<p>" + json.message + "</p>");
					form.find(".message").css("color", "#000");
					form.find(".message").slideUp(function() {
						$(this).slideDown();
					});
					
					form.find(".captcha").attr('src', '/captcha/?' + new Date().getTime());
					
					form.find("input[name='name'], input[name='email'], input[name='phone'], input[name='code'], textarea[name='message'], input[name='code']").val("");
				}
			},
			error: function () {
				return location.reload(alert("There has been an unexpected error\n"));
			}
		});
		
		return false;
	});
});

function contact()
{
	$(".contact-form").find(".message").hide();
	
	$(".contact-form").fadeIn("slow");
	
	set_bg_fade()
}

function contact_close()
{
	$(".contact-form").fadeOut("slow");
	
	set_bg_fade(1)
}

function set_bg_fade(remove)
{
	if (remove) {
		$("#bgFade").fadeOut("slow", function() {
			$("body").css("overflow", "auto");
		});
		
		return;
	}
	
	$("body").css("overflow", "hidden");
	
	var size = getPageSize();
	
	$("#bgFade").css("width", size[0]);
	$("#bgFade").css("height", size[1]);
	$("#bgFade").fadeIn("slow");
}


function getPageSize()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if (document.documentElement.clientWidth)
			windowWidth = document.documentElement.clientWidth; 
		else
			windowWidth = self.innerWidth;
		
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth)	
		pageWidth = xScroll;		
	else
		pageWidth = windowWidth;

	return [pageWidth,pageHeight];
}
