(function() {
  	$(document).ready(function() {
	
		// As soon as the DOM is ready, make the nav invisible
		if($('nav').css('visibility') != 'visible') {
    		$('nav').css('visibility', 'hidden');        
		}
        
		handlersForContact();
		
		$("form").submit(function(e){
			return contactErrorCheck(e);
		});
		
		// thank you hover
		if(QueryString("showTY") == "Y") {
			$("#ty_shadows").show();
			$("#ty_message").show();
		}
		$("#ty_shadows").click(function() { closeHover(); });
		$("#ty_message a").click(function() { closeHover(); });
		
		// setup html                       
		var newHTML = "";
		$("#home_slider").cycle({
			fx: "fade", speed: 2500, timeout: 4500, pause: 1
		});                                                                                                  
  	});

    function closeHover() {
		$("#ty_shadows").hide();
		$("#ty_message").hide();   
	};
	
	function handlersForContact() {
		// handlers for contact form labels
		$input_name = $("#input_name"); 
		$label_name = $("#label_name"); 
		$input_email = $("#input_email"); 
		$label_email = $("#label_email"); 
		$input_phone = $("#input_phone"); 
		$label_phone = $("#label_phone"); 
		$input_interested = $("#input_interested"); 
		$label_interested = $("#label_interested"); 
		
		if($input_name.val() != "") { $label_name.hide(); }
		if($input_email.val() != "") { $label_email.hide(); }
		if($input_phone.val() != "") { $label_phone.hide(); }
		if($input_interested.val() != "") { $label_interested.hide(); }
		
		$label_name.click(function(){ $label_name.hide(); });
	    $label_name.focus(function(){ $label_name.hide(); });    
	    $input_name.click(function(){ $label_name.hide(); });
	    $input_name.focus(function(){ $label_name.hide(); });
		$input_name.blur(function(){
			if($input_name.val() == "") {
				$label_name.show();
			}                      
		});
		$label_email.click(function(){ $label_email.hide(); });
	    $label_email.focus(function(){ $label_email.hide(); });    
	    $input_email.click(function(){ $label_email.hide(); });
	    $input_email.focus(function(){ $label_email.hide(); });
		$input_email.blur(function(){
			if($input_email.val() == "") {
				$label_email.show();
			}                      
		});
		$label_phone.click(function(){ $label_phone.hide(); });
	    $label_phone.focus(function(){ $label_phone.hide(); });    
	    $input_phone.click(function(){ $label_phone.hide(); });
	    $input_phone.focus(function(){ $label_phone.hide(); });
		$input_phone.blur(function(){
			if($input_phone.val() == "") {
				$label_phone.show();
			}                      
		});
		$label_interested.click(function(){ $label_interested.hide(); });
	    $label_interested.focus(function(){ $label_interested.hide(); });    
	    $input_interested.click(function(){ $label_interested.hide(); });
	    $input_interested.focus(function(){ $label_interested.hide(); });
		$input_interested.blur(function(){
			if($input_interested.val() == "") {
				$label_interested.show();
			}                      
		});
		
	}
	   
	// contact form error checking
	function contactErrorCheck(e) {    
		var errorMessage = "";        
		$name = $("#input_name").val()
		$email = $("#input_email").val()
		$phone = $("#input_phone").val()
		if($name == "" && $email == "" && $phone == "") {
			errorMessage = "Please fill out your name and an email address or phone number where we can contact you. ";
		} else {                 
			if($name == "") {
				errorMessage = "Please provide us with your name. ";
			} 
			if($email == "" && $phone == "") {
			   errorMessage = "Please provide an email or phone number where we can contact you. ";
			} else { 
				if($email != "") {
	    			var emailBad = false;
					var emailFilter=/^.+@.+\..{2,6}$/;
			    	if (!(emailFilter.test($email))) { 
			    		emailBad = true;
			    	}

			    	var illegalChars= /[\s\!\*\(\)\<\>\,\;\:\\\/\"\[\]]/
			    	if ($email.match(illegalChars)) {
			    		emailBad = true;
			    	}
				    if(emailBad) {
						errorMessage += "There's an issue with your email address. "; 
					}                             
				}
				if($phone != "" && $phone.length < 10) {
				    errorMessage += "Your phone number is invalid, we need at least 10 digits. ";                              
				}                                                                  
			}
		}   
		
		// no errors, we're good.. else show error message
		if(errorMessage == "") {
			return true;
		} else {
			// show error message
			$("form .errors").html("<div><h5>Whoops, there was a problem.</h5><p>" + errorMessage + "</p></div>");
			$("form .errors").show();
			return false;
		}
	}   
    
	// Load the fonts (to deal with FOUT)
	try {
		Typekit.load({
	    	active: function() {
				// As soon as the fonts are active, fade in the example
	        	// Don't fade in browsers that don't do proper opacity, like IE
	        	if (jQuery.support.opacity) {
	          		$('nav').css('visibility', 'visible').hide().fadeIn();
	        	} else {
	          		$('nav').css('visibility', 'visible');
	        	}
	      	},
	      	inactive: function() {
		        // If the fonts are inactive, just show the example
		        // You can apply fallback styles using the wf-inactive class in your CSS
		        $('nav').css('visibility', 'visible');
			}
		})
    } catch(e) {}
})();    

function QueryString(key) {
   	var value = null;
   	for (var i = 0; i < QueryString.keys.length; i++) {
   		if (QueryString.keys[i] == key) {
   			value = QueryString.values[i];
   			break;
   		}
   	}
   	if (value == null) {
   		return '';
   	}
   	else {
   		return value;
   	}
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse() {
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	for (var i = 0; i < pairs.length; i++) {

		var pos = pairs[i].indexOf('=');

		if (pos >= 0) {
			var argname = pairs[i].substring(0, pos);
			var value = pairs[i].substring(pos + 1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;
		}
	}
}

QueryString_Parse();
     

