jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i > -1; i--) {
		jQuery("<img>").attr("src", a[i]);
	}
}
$(document).ready(function() {
	$.preloadImages('images/home.png','images/business.png','images/contactus.png','images/incentives.png','images/solutions.png');
	/**
	* Performs Cross Fades
	*/
	var fadeBack = false; //Eathier the function to fade back to origonal on a time out or false
	var fading = false;
	var orig = $("#nav #backgrounds div:visible").attr("class"); //The class name of the origonally visible image
	$("#nav a").mouseover(function() {
      tgt = $(this).attr('id'); //The name of the targetd link (and image)
	  if (fadeBack && (orig != tgt)) {clearTimeout(fadeBack); fadeBack = false;} // if fadeback is set but were over a new image clear the timeout and set fadeback to false
	  if ((orig != tgt) && (fading == false)) { //if were not on the currently visible tab perform the fade
	  	  fading = true;
		  $("#backgrounds div:visible").fadeOut();
		  $("#backgrounds div."+tgt).fadeIn();
		  fading = false;
	  }
    }).mouseout(function(){
		if (orig != tgt && (fading == false)) {
			fadeBack = setTimeout(function() {
				fading = true;
				$("#backgrounds div:visible").fadeOut();
				$("#backgrounds div."+orig).fadeIn();
				fading = false;
				fadeBack = false;
			},50);
		}
    });
	$('#thankyou').hide();
	$.validator.setDefaults({
		submitHandler: function(form) {
			var dataString;
			dataString = $("#theForm").serialize()+'&ajax=1';
			$('#form_info').html('Submitting your request. Please Wait.').css('display','block');
			$.ajax({  
			  type: "POST",  
			  url: "includes/processForm.php",  
			  data: dataString,  
			  success: function(data) {  
			  	$('#form_info').css('display','none');
			   	if (data == 'added') {
					$('#form').fadeOut(1000,function() {
						$('#thankyou').fadeIn(1000);
					});
				} else {
					$('#submission_error').html('There was an error submitting your request. Please try again.').css('display','block');
				}
			  }
			}); 
		},
		errorPlacement: function(error, element) {
		   error.appendTo( element.next("span") );
		},
		errorClass: "form_error",
		errorElement: "p"
	});
	$("#theForm").validate({
		rules: {
			FirstName: "required",
			LastName: "required",
			Email: {
				required:true,
				email:true
			},
			City: "required",
			Phone1: {
				required:true,
				phone:true
			},
			_IntrestedIn: "required"
			
		},
		messages: {
			FirstName: "Please Enter Your First Name",
			LastName: "Please Enter Your Last Name",
			Email: "Please Enter a Valid Email",
			City: "Please Enter Your City",
			Phone1: "Please Enter a Valid Phone Number",
			_IntrestedIn: "Please Select What You Are Intrested In"
		}
	});
	$('#faqs div').hide();
	$('#faqs h4').click(function() {
		if ($(this).next('div').is(':visible')) $(this).next('div').slideUp(1000);
		else $(this).next('div').slideDown(1000);
	});
});
