var initState = false    //placeholder to indicate initial state is current
var hideInt = 500        //sets timeout length for hiding of menus - ADJUST LENGTH OF MENU DISPLAY HERE!
var TID;

function refreshMenu() {
	$("div#drop1").css('visibility', 'hidden');
	$("div#drop2").css('visibility', 'hidden');
	$("div#drop3").css('visibility', 'hidden');
	$("div#drop4").css('visibility', 'hidden');
}


function showMenu(menuID) {
	if (initState) {
		clearTimeout(TID)
	} else {
		initState = true;
	}
  
	refreshMenu();  
  
	for (var i = 1; i <= 4; i++) {
		if (menuID == "" + i) {
			$("div#drop" + i).css('visibility', 'visible');
		} else {
			$("div#drop" + i).css('visibility', 'hidden');
		} 				                          
	}
}

function closeMe(){
	TID = window.setTimeout( "refreshMenu();", hideInt );
}

var IFControl = null;

$(document).ready(function(){
	$("div#drop1").css('visibility', 'hidden');
	$("div#drop2").css('visibility', 'hidden');
	$("div#drop3").css('visibility', 'hidden');
	$("div#drop4").css('visibility', 'hidden');
		
	// main nav roll-over
	$("div#header ol li#in-stock").hover(function(){ showMenu(1); }, function(){ closeMe(); });
	$("div#header ol li#aftersales").hover(function(){ showMenu(2); }, function(){ closeMe(); });
	$("div#header ol li#new-vehicles").hover(function(){ showMenu(3); }, function(){ closeMe(); });
	$("div#header ol li#company").hover(function(){ showMenu(4); }, function(){ closeMe(); });
		
	// dropdown rollover
	$("div#drop1").hover(function(){ showMenu(1); }, function(){ closeMe(); });
	$("div#drop2").hover(function(){ showMenu(2); }, function(){ closeMe(); });
	$("div#drop3").hover(function(){ showMenu(3); }, function(){ closeMe(); });
	$("div#drop4").hover(function(){ showMenu(4); }, function(){ closeMe(); });
	
	// "bug" fix/hack with side by side divs where one div is "longer" than the other
	if ($("#content_form").height() > $("#content_lhs").height()) {
		var minHeight = $("#content_form").height();
		$("#content_lhs").css( { "minHeight": minHeight + "px" } )
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			$("#content_lhs").css( { "height": minHeight + "px" } ) // IE6 only
		};
	};
	if ($("#content_form").height() < $("#content_lhs").height()) {
		var minHeight = $("#content_lhs").height();
		$("#content_form").css( { "minHeight": minHeight + "px" } )
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			$("#content_form").css( { "height": minHeight + "px" } ) // IE6 only
		};
	};
	
	if ($(".UsedVehiclesRHS").height() > $(".UsedVehiclesLHS").height()) {
		var minHeight = $(".UsedVehiclesRHS").height();
		$(".UsedVehiclesLHS").css( { "minHeight": minHeight + "px" } )
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			$(".UsedVehiclesLHS").css( { "height": minHeight + "px" } ) // IE6 only
		};
	};
	if ($(".UsedVehiclesRHS").height() < $(".UsedVehiclesLHS").height()) {
		var minHeight = $(".UsedVehiclesLHS").height();
		$(".UsedVehiclesRHS").css( { "minHeight": minHeight + "px" } )
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			$(".UsedVehiclesRHS").css( { "height": minHeight + "px" } ) // IE6 only
		};
	};

	$("#submit_comment").click(function(event){
		var showAlert = '';
		if ($("#cName").val() == '') { showAlert += "- Your Name\n"; };
		if ($("#cEmail").val() == '') { showAlert += "- Your Email Address\n"; };
		if ($("#cComment").val() == '') { showAlert += "- Your Comment\n"; };
		if ($("#cSecurity").val() == '') { showAlert += "- Security Code"; };
		if (showAlert != '') {
			alert("Your comment could not be submitted. Please check the following:\n" + showAlert);
			return false;
		} else {
			// check captcha code via AJAX request
			$.ajax({
				type: "POST",
				url: "/performax/includes/security_check.asp",
				data: "cSecurity=" + $("#cSecurity").val(),
				success: checkCaptchaReturn
			});
			return false;
		};
	});

});


function checkCaptchaReturn (responseXML) {
    // 'responseXML' is the XML document returned by the server; we use
    // jQuery to extract the content of the message node from the XML doc

	var captchaStatus = $('status', responseXML).text();
	var captchaMessage = $('message', responseXML).text();
	switch (captchaStatus) {
		case "ok":
//	  		alert(captchaMessage);
			$('#commentForm').submit();
	  		break;
		case "error":
			$("#cSecurity").val('');
			$("img#security").attr("src", "includes/security.asp?random=" + new Date().getTime());
	  		alert(captchaMessage);
	  		break;
		default:
	  		alert(captchaMessage);
	}
}

