$(document).ready(function(){
	$("form input.date").datepicker({ dateFormat: "mm/dd/yy", speed: "", showButtonPanel: true, changeMonth: true, changeYear: true, showAnim: "fadeIn" });
	$("#top-nav ul li").hover(function(){ var width = $(this).width(); $(this).stop().children('ul').css({'right' : '-' + ((200 - width) / 2) + 'px'}).fadeIn('fast'); }, function(){ $(this).stop().children('ul').fadeOut('fast'); });
});

// Wait until all images / css have loaded before prefetching other images
$(window).load(function(){
	preFetch();
});

// pre-load non-displayed images so that when they are accessed, they are quick to display
var arrPrefetch = [];
var prefetchedImages = [];
function preFetch()
{
	// load the css hero background images
	if (globals.heroes)
	{
		for (var i in globals.heroes)
			if (globals.heroes[i].img)
				arrPrefetch.push("/site_media/uploads/case_studies/hero/"+ globals.heroes[i].img);
		setTimeout(startHeroSwitcher, 1000);
	}
	
	// append images that have been specified in global's prefetch array
	if (globals.prefetch && globals.prefetch.length > 0)
		arrPrefetch = arrPrefetch.concat(globals.prefetch);
	
	for (var i = 0; i < arrPrefetch.length; i ++)
	{
		tmp = new Image();
		tmp.src = arrPrefetch[i];
		prefetchedImages.push(tmp);
	}
}


function doContact()
{
	form = validate([
		{id: "contact_name", alias: "name", validators: ["allowed_chars", {regex: new function(){ this.test = function(strInput){ return !/^name$/i.test(strInput) } }}]},
		{id: "contact_email", alias: "email", validators: ["email"]},
		{id: "contact_message", alias: "message", validators: [{regex: new function(){ this.test = function(strInput){ return !/^message$/i.test(strInput) } }}]}
	]);
	if (!form.isValid)
		alertInvalidForm(form);

	if (form.isValid && !($('#attachment').val()))
	{
		clearErrors(form, true);
		form.values.ajax = 1;
		REST("contact", form.values, function(data){ trackVirtual("/contact/contact_success/"); });
		return false;
	}
	return form.isValid;
}

function doComment()
{
	form = validate([
		{id: "name", alias: "name", validators: ["allowed_chars"]},
		{id: "email", alias: "email", validators: ["email"]},
		{id: "comment", alias: "comment"},
	]);
	if (!form.isValid)
	{
		alertInvalidForm(form);
		$('#comment-error').slideDown();
	}

	else
	{
		$('#comment-error').hide();
		clearErrors(form, true);
		form.values.ajax = 1;
		form.values.entry_id = $('#entry_id').val();
		REST("comment", form.values, function(data){ $('#comment-success').slideDown(); });
	}
	return false;
}

function checkClientPortalForm()
{
	form = validate([ {id: "username"}, {id: "password"} ]);
	if (!form.isValid)
		alertInvalidForm(form);
	return form.isValid
}
function checkClientPortalForgotPasswordForm()
{
	form = validate([ {id: "email", validators: ["email"]} ]);
	if (!form.isValid)
		alertInvalidForm(form);
	return form.isValid
}

function quickModal(strOut)
{
	modalBox(strOut, { 'pin': false });
}

