// JavaScript Document
/* -- Sunset Ford --- */
/* --------------- Functions that need to run after the document loads -------------------*/

$(document).ready(function(){	
	$.get("/navigation-file.html", function(data){
		loadNav(data);
	});
	
	setupNav();
	


// Detailed inventory full name validation

		$('#Form2').unbind('submit');
		$('#Form2').submit(function() {
			var goodToGo = true;
			var errorStr = ""
			
			var fullName = document.getElementById("Text1").value;
			fullName = jQuery.trim(fullName);
			
			if (!fullName.match(" ")){
			goodToGo = false;
			errorStr = errorStr + "Please enter your full name.\n";
			}
			
			if (!goodToGo){
				alert(errorStr);
			}
			return goodToGo;
			//return false;
		});

	// quick nav functionality
	$("#locations-button").parent().click(function(event){
		event.preventDefault();
		if($("#locations-list").is(":hidden")) {
			$("#locations-list").slideDown(350);
		}
	},function(){
		if($("#locations-list").is(":visible")) {
		   $("#locations-list").slideUp(100);
		}
   });

	// share this functionality on Model Landing pages

   $("#share-this-link").click(function(event){
										
     $("#stwrapper").css({'visibility':'visible', 'top':'250px','left':'500px'});
     $("#stwrapper iframe").css({'visibility':'visible'});
		event.preventDefault();
		event.stopPropagation();  
	});
   
	// dealer info dropdown functionality
	$("#dealer-info").hover(function(){
		if($(this).children("#locations-list").is(":hidden")) {
			$(this).children("#locations-list").slideDown(350);
		}
	},function(){
		if($(this).children("#locations-list").is(":visible")) {
			$(this).children("#locations-list").slideUp(100);
		}
   });

	// extended nav functionality

	$(".secondary-pages #extended-nav .nav-level1 ul").hide();

	$(".secondary-pages #extended-nav").hover(function(){
		if($("#extended-nav li > ul > li").is(":hidden")) {
			$("#extended-nav li > ul").slideDown(350);
		}
	},function(){
		$("#extended-nav li > ul").hide();
   });
	 
	// end extended nav 

	// collapse VIP and search area 
	
     $("#vip-expanded").hide();
     $("#search-expanded").hide();


	// options nav bar functionality

   $("#options-nav .nav-items .button-item").mouseenter(function(event){
     $(this).siblings().children(".sub-nav-items").fadeOut(500);
     $(this).children(".sub-nav-items").fadeIn(500);
   });

	// VIP coupon functionality

   $("#vip-coupon").mouseover(function(event){
     $("#vip-expanded").slideDown(300);
   });
   
   $("#vip-coupon").mouseleave(function(event){
     $("#vip-expanded").fadeOut(300);
   });
   
	$("#vip-coupon select").mouseleave(function(event) { event.stopPropagation(); });
	$("#vip-coupon input").mouseleave(function(event) {
		event.stopPropagation();
	});
	
	// VIP show message after form submit
	
	$("#vip-expanded .errors").each( function () {
		if ($(this).html() != '') {
			$(this).parents("#vip-expanded").show();
		}
	});
	$("#vip-expanded .success").each( function () {
		if ($(this).html() != '') {
			$(this).parents("#vip-expanded").show();
		}
	});
	$("#vip-expanded .close-item").click(function(event){
		$("#vip-expanded").fadeOut(100);
   });
	
	
	// search bar functionality

   $("#search-bar").mouseover(function(event){
		$("#search-expanded").stop(false,true).fadeIn(100);
   });
   
    $("#search-bar").mouseleave(function(event){
		$("#search-expanded").stop(false,true).fadeOut(50);
   });
	  
	$("#search-bar select").mouseleave(function(event) { event.stopPropagation(); });
	$("#search-bar input").mouseleave(function(event) {
		event.stopPropagation();
	});

// Load Images for rotation
	if($("body").is("#main")) {
		$.get("/home-image-rotation.html", function(data){
			setupRotation(data);
		});
	}
	
	// open up any top menu items where there are erros in the form
	$(".nav-expanded .errors").each( function () {
		if ($(this).html() != '') {
			$(this).parents(".nav-expanded").show();
		}
	});
	
	$(".nav-expanded .success").each( function () {
		if ($(this).html() != '') {
			$(this).parents(".nav-expanded").show();
		}
	});

});

/* ---------- START EXTRA FUNCTIONS --------- */

function loadNav (data) {
	$('#nav-shop-new').html($(data).children('#nav-shop-new').html());
	$('#nav-shop-used').html($(data).children('#nav-shop-used').html());
	
	// Show the exterior / interior images of each vehicle as it is hovered over.
	$(".model-item a").hover( function() {
		$(this).parent().siblings(".model-desc").show();
	}, function() {
		$(this).parent().siblings(".model-desc").hide();
	});
	
	// Hide the drop down menu options when cars are being hovered over.
	$("#new-vehicle-list .expanded-primary > ul").hover( function() {
		$("#new-vehicle-list .expanded-secondary .nav-item").hide();
	}, function() {
		$("#new-vehicle-list .expanded-secondary .nav-item").show();
	});
	
	// close-X  dropdown functionality
	$(".nav-expanded .close-item").click(function(event){
		$(".nav-expanded").fadeOut(100);
   });
	
}

var navHideDelay = new Array();

function setupNav () {
// primary nav functionality
	navDelay = 150;
	
	// prevent the main site menu from being clicked on.
	$('#nav-page-list > a').click(function (data) {
		data.preventDefault();
	});
	
	$("#nav-bar select").mouseleave(function(event) { event.stopPropagation(); });
	$("#nav-bar input").mouseleave(function(event) {
		event.stopPropagation();
	});
	
	$("#nav-bar").mouseenter( function(){
		navHover = true;
	});
	
	$("#nav-bar").mouseleave( function(){
		navHover = false;
		
		if(navHideDelay[0]) {clearTimeout(navHideDelay[0]);}
		// Prevent the menu from hiding to quickly
		navHideDelay[0] = setTimeout(function() {
			if(navHover != true) {$("#nav-bar > li > .nav-expanded").not(":hidden").fadeOut(navDelay);}
		}, navDelay);
	});
	
	$("#nav-bar > li").mouseenter(function(){
			if ($(this).siblings().children(".nav-expanded").not(":hidden").length == 0) {
				$(this).children(".nav-expanded").slideDown(navDelay);				
			} else {
				$(this).siblings().children(".nav-expanded").stop(true, true).hide();
				$(this).children(".nav-expanded").show();
			}
	});
}

function setupRotation (data) {

	//default values
	itemClass = "nav-item";
	itemFirstClass = "first-item";
	itemLastClass = "last-item";
	timerLen = 8000; // equals 3 secs
	elementID = "banner-counter";
	
	$('#featured-content').append('<ul id="banner-counter"></ul>');	

	//parse data
	itemArray = data.split(",");
	itemArray.splice(itemArray.length - 1, 1); // remove the extra data
	itemArrayLen = itemArray.length;
	// data format: pagetitle,imageurl,linkurl

	//load images
	for (x = 1; x < itemArrayLen; x = x + 3) {		
		//image
		tmpValue = itemArray[x];
		itemArray[x] = new Image();
		itemArray[x].src = tmpValue;
	}
	
	//create html for the gallery nav
	for (x = 0; x < (itemArrayLen/3); x++) {
		
		if (x == 0) {
			itemTmpClass = itemClass + ' ' + itemFirstClass;
		} else if (x == itemArrayLen - 1) {
			itemTmpClass = itemClass + ' ' + itemLastClass;
		} else {
			itemTmpClass = itemClass;
		}
			
		$('#' + elementID).append('<li class="' + itemTmpClass + '"><a>' + (x+1) + '</a></li>');	
	}
	
	// When the gallery nav is clicked switch banner
	$('#' + elementID + ' > ' + '.' + itemClass).click(function () {
		// Clear the old timer. Switch to the clicked item, and restart the timer.
		clearTimeout(bannerTimer);
		switchActive(this);
		bannerTimer = setTimeout("advanceActive()", timerLen);		
	});
	
	switchActive();
	bannerTimer = setTimeout("advanceActive()", timerLen);
}

function activateEl(elementRef) {
	$(elementRef).addClass("active");
	$(elementRef).siblings().removeClass("active");
}

function switchActive (elementRef) {
	
	// Find the next element or use the passed element reference
	if (elementRef == null) {
		object = $('#' + elementID + ' > ' + '.active').next('.' + itemClass);
	} else {
		object = elementRef;
	}
	
	// If at the end then start over
	if ($(object).length == 0) {
		//alert("object is null");
		object = $('#' + elementID + ' > ' + '.' + itemClass + ':first-child');
	}

	// Find the element index
	var itemIndex = $('#' + elementID + ' > ' + '.' + itemClass).index(object);
	
	// Set the image, title, and url live
	$("#mainbanner-link > span").text(itemArray[(itemIndex * 3)]);
	$("#container").css("background-image", 'url(' + itemArray[(itemIndex * 3) + 1].src + ')');
	$("#mainbanner-link").attr('href', itemArray[(itemIndex * 3) + 2]);

	activateEl(object);
}

function advanceActive () {
	switchActive();
	bannerTimer = setTimeout("advanceActive()", timerLen);		
}