
jQuery(function(){
  setHeightSubNav();
	$("#attorney-menu").hover(
		function(){
			clearTimer(attorneyTimerId);
			$("#attorney-sub").fadeIn("fast");	
		},
		function(){
			attorneyTimerId=window.setTimeout(function() {$("#attorney-sub").fadeOut("fast");}, timeoutInMillis);	  
	});
	
	$("#attorney-sub .drop-down li").hover(
		function(){
			clearTimer(attorneyTimerId);	
		},
		function(){
			attorneyTimerId=window.setTimeout(function() {$("#attorney-sub").fadeOut(100);}, timeoutInMillis);	  
	});
	
	$("#news-menu").hover(
		function(){
			clearTimer(newsTimerId);
			$("#news-sub").fadeIn("fast");	
		},
		function(){
			newsTimerId=window.setTimeout(function() {$("#news-sub").fadeOut("fast");}, timeoutInMillis);	  
	});
	
	$("#news-sub .drop-down li").hover(
		function(){
			clearTimer(newsTimerId);	
		},
		function(){
			newsTimerId=window.setTimeout(function() {$("#news-sub").fadeOut(100);}, timeoutInMillis);	  
	});
	
	$("#pub-menu").hover(
		function(){
		  clearTimer(resourcesTimerId);
			$("#pub-sub").fadeIn("fast");	
		},
		function(){
			resourcesTimerId=window.setTimeout(function() {$("#pub-sub").fadeOut("fast");}, timeoutInMillis);	  
	});
	
	$("#pub-sub .drop-down li").hover(
		function(){
			clearTimer(resourcesTimerId);	
		},
		function(){
			resourcesTimerId=window.setTimeout(function() {$("#pub-sub").fadeOut(100);}, timeoutInMillis);	  
	});
	
	$("#about-menu").hover(
		function(){
		  clearTimer(aboutTimerId);
			$("#about-sub").fadeIn("fast");	
		},
		function(){
			aboutTimerId=window.setTimeout(function() {$("#about-sub").fadeOut("fast");}, timeoutInMillis);	  
	});
	
	$("#about-sub .drop-down li").hover(
		function(){
			clearTimer(aboutTimerId);	
		},
		function(){
			aboutTimerId=window.setTimeout(function() {$("#about-sub").fadeOut(100);}, timeoutInMillis);	  
	});
	
	$("#practice-menu").hover(
		function(){
		    clearTimer(practiceTimerId);
			$("#practice-sub").fadeIn("fast");	
		},
		function(){
			practiceTimerId=window.setTimeout(function() {$("#practice-sub").fadeOut("fast");}, timeoutInMillis);	  
	});
	
	$("#practice-sub .drop-down li").hover(
		function(){
			clearTimer(practiceTimerId);	
		},
		function(){
			practiceTimerId=window.setTimeout(function() {$("#practice-sub").fadeOut(100);}, timeoutInMillis);	  
	});
	
});

var attorneyTimerId;
var resourcesTimerId;
var newsTimerId;
var aboutTimerId;
var practiceTimerId;
var timeoutInMillis = 350;

function clearTimer(timerId){
  window.clearTimeout(timerId);
}

function getTimerId(objId){
	var currentTimerId;
	if(objId=="attorney-menu"){
		currentTimerId=attorneyTimerId;
	} else if (objId=="news-menu"){
		currentTimerId=newsTimerId;
	}
	return currentTimerId;
}
	
function validate(){
	var result = true;
	$("input[type=text]").removeClass("error");
	if(!isValid("#clp-firstName")) {
		result=false;
	}
	if(!isValid("#clp-lastName")) {
		result=false;
	} 
	if(!isValid("#clp-email")) {
		result=false;
	}
	if(result){
		autofill();
	}
	checkCaptcha();
	return false;
}
	
function checkCaptcha(){
	var resp=$("input[name='recaptcha_response_field']").val();
	var challenge=$("input[name='recaptcha_challenge_field']").val();

	$.ajax({
	type: "POST",
	url: "validate.php",
	data: "recaptcha_response_field="+resp+"&recaptcha_challenge_field="+challenge,
	success: function(msg){
		if(msg.indexOf("true")>-1){
			$("form:first").submit();
		} else {
			$("input[name='recaptcha_response_field']").addClass("error");
			return false;
		}
	}
	}); 
}
function isValid(field){
	if($(field).val()==""){
		$(field).addClass("error");
		return false;
	} else{ return true; }
}
 	
function autofill(){
	var $inputs = $(":text");
	for(var x=0; x<$inputs.length; x++){
		if($($inputs[x]).val()==""){
			$($inputs[x]).val("N/A");
		}
		if($("input[name='recaptcha_response_field']").val()=="N/A"){
			$("input[name='recaptcha_response_field']").val("");
		}
	}
}
 	
function setHeightSubNav(){
	var height=$("#main-content").height();
	$(".secondary-nav").css('height',height+"px");
}
  
function changeBackground() {
	prevImg=bgImg;
	bgImg++;
	if(bgImg>=3){
	 	bgImg=0;
	}

	$("#slider").css("background","transparent url("+images[prevImg]+") left top no-repeat");
	// fade image
	$("#landing-page-image").animate({opacity: .12}, 500, function(){
		$("#landing-page-image").attr("src", images[bgImg]);
		$("#landing-page-image").animate({opacity: 1},700, "linear");
	});
}

function validateSearch(){
	if($("#q").val()==""){
		alert("Enter search term");
		return false;
	}
}
