$(document).ready(function(){

/* [START] top nav menus */
	var menuDelay = 1;
	$("#nav-top a").each(
		function(i) {
			
			var menuNum = menuNumber(i,$("#nav-top a").size());
			
			$(this).hover(
			function(){
				$(this).addClass("nav-on-" + menuNum).prev().addClass("noline");
				$(".nav-top-menu:eq(" + i + ")").css({"visibility" : "visible"});
			},
			function(){
				$(this).removeClass("nav-on-" + menuNum).prev().removeClass("noline");
				$(".nav-top-menu:eq(" + i + ")").oneTime(menuDelay,"hidemenu",function(){
					$(this).css({"visibility" : "hidden"});
				});
			}
		);
	});

	$(".nav-top-menu").each(
		function(i){	
			
			var menuNum = menuNumber(i,$("#nav-top a").size());
				
			$(this).hover(
			function(){
				$(this).css({"visibility" : "visible"}).stopTime("hidemenu");
				$("#nav-top a:eq(" + i + ")").addClass("nav-on-" + menuNum).prev().addClass("noline");
			},
			function(){
				$(this).css({"visibility" : "hidden"});
				$("#nav-top a:eq(" + i + ")").removeClass("nav-on-" + menuNum).prev().removeClass("noline");
			}
		);
	});
	
	// This was added to allow the sale menu to be hidden. 
	function menuNumber(menuIndex,totalNumberOfMenus)
	{
		var num = menuIndex;
		if ( totalNumberOfMenus == 4 && menuIndex > 1 )
		{
			num = menuIndex + 1;
		}
		
		return num;
	}
/* [END] top nav menus */


	//Show/ Hide the vehicles in the right nav
	$("#nav-right-vehicles").hide();
	$.cookie("mod","0");
	$("#vehicle-nav-link").click(function(){
		$("#nav-right-vehicles").slideToggle("slow");
		plusMinus("#modelPlus","mod");
		//alert($("#hidMod").val());
		return false;
	});
	
	
	//Show/ Hide the accesory categories in the right nav
	$("#nav-right-accessories").hide();
	$.cookie("acc","0");
	$("#accessories").click(function(){
		$("#nav-right-accessories").slideToggle("slow");
		plusMinus("#accPlus","acc");
		return false;
		
	});
	
	
	//Remove bottom border from last right nav link
	$("#nav-right a:last").css({"border-bottom" : "0", "margin-bottom" : "3px"});
	
	//function to switch the plus minue image in the right nav based on image id and cookie name
	function plusMinus(img,cookieName)
	{
		var c = $.cookie(cookieName);
		if (c == 0) {
			$(img).attr({src: "/design/accplus-minus.gif"});
			$.cookie(cookieName,"1");}
		else {
			$(img).attr({src: "/design/accplus-plus.gif"});
			$.cookie(cookieName,"0");}
	}
	
	
	//Remove the right border from the last column in a top nav drop down menu
	$(".nav-top-menu").each(function(i){
		$(".nav-top-menu:eq("+i+") div:last").css({"border-right" : "none"});
	});
	
	
	//Add click to the about menu to slide out the more info window.
	$("#about-menu-extended").hide();
	
	$("#about-menu").click(function(){
		$("#about-menu-extended").slideToggle("slow");
		//$("#about-menu").hide();
		//$("#about-menu-close").show();
		//alert($("#about-menu").html());
		if ($("#about-menu").html() == "About Us")
		{
			$("#about-menu").html("Close").removeClass("about-menu").addClass("about-menu-close");
		}
		else
		{
			$("#about-menu").html("About Us").removeClass("about-menu-close").addClass("about-menu");
		}
	});
	
	
	/*
	$("#about-menu-close").click(function(){
		$("#about-menu-extended").slideToggle("slow");
		$("#about-menu").show();
		$("#about-menu-close").hide();
	});
	
	$("#about-menu-close").hide();
	*/
	
	
	
	//Popular items link in the footer. show/hide the menu.
	$("#popular-items").hide();
	$("#popular-items div:last").css({"border-right":"0"});
	$("#pop-search").click(function(){
		$("#popular-items").show();
		$("#footer-vehicles").hide();
		return false;
	});
	$("#popular-items a:last").click(function(){
		$("#popular-items").hide();
		return false;
	});
	
	
	//Vehicle Home Page menu link in the footer. show/hide the menu.
	$("#footer-vehicles").hide();
	$("#footer-vehicles div:last").css({"border-right":"0"});
	$("#footer-vehicle-link").click(function(){
		$("#footer-vehicles").show();
		$("#popular-items").hide();
		return false;
	});
	$("#footer-vehicles a:last").click(function(){
		$("#footer-vehicles").hide();
		return false;
	});
	
	
	//function to hide elements when the body is clicked
	$("body").click(function(){
		$("#popular-items").hide();
		$("#footer-vehicles").hide();
	});

});




/* [START] New Window function*/
function newWindow(winURL) {
  window.open(winURL,"","width=350,height=250,scrollbars=no,toolbars=no,sizeable=no")
}
function ConfirmMsg(msg) {
  document.MM_returnValue = confirm(msg);
}
/* [END] New Window function*/


/*
a function to remove "enter you email" text from a field and replace it if the user doesn't enter anything
f = the field to remove the text from
t = the default text displayed in the field
r = 1 to remove the text, 0 to replace it with the default value
*/

function formTextRemove(f,t,r)
{
	if (r == 1){
		if (f.value == t){
			f.value = '';}
	}
	else if (r == 0){
		if (f.value == ''){
			f.value = t;}
	}
}

/*Form validation for the sign-up for savings form*/
function checkSavingsForm()
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(document.getElementById("email").value)){
		alert("Please enter a valid email address");document.getElementById("email").focus(); return false;}
	if (document.getElementById("fname").value == "") {
		alert("Please enter your First Name."); document.getElementById("fname").focus(); return false;}
	if (document.getElementById("lname").value == "") {
		alert("Please enter your Last Name."); document.getElementById("lname").focus(); return false;}
	if (document.getElementById("address1").value == "") {
		alert("Please enter an address "); document.getElementById("address1").focus(); return false;}
	if (document.getElementById("city").value == "") {
		alert("Please enter a city "); document.getElementById("city").focus(); return false;}
	if (document.getElementById("state").value == "") {
		alert("Please select a state/province "); document.getElementById("state").focus(); return false;}
	if (document.getElementById("zip").value == "") {
		alert("Please enter your postal code"); document.getElementById("zip").focus(); return false;}
	if (document.getElementById("country").value == "") {
		alert("Please select your country"); document.getElementById("country").focus(); return false;}
	if (document.getElementById("modelname").value == "") {
		alert("Please enter your Vehicle Model"); document.getElementById("modelname").focus(); return false;}
	if (document.getElementById("modelyear").value == "") {
		alert("Please enter your Vehicle Year"); document.getElementById("modelyear").focus(); return false;}
}