/* */

/*
	NOTE -:
	Am using $(window).load() for the image resizer because the javaScript needs to check
	image widths and heights. But, because webkit browsers (Chrome and Safari) loads
	JavaScript concurrently, rather than after the images & CSS, I need to wait
	until the pages are totally loaded.
*/
$(window).load(function() {

   	$(".carousel").jCarouselLite({
        btnNext: ".new_car_next",
        btnPrev: ".new_car_prev",
		visible: 3,
		auto: 3000,
	    speed: 400

    });
   
	// =========================================== Stretch the sidebar on the static pages all the way down
	if(jQuery("#sidebar_inner").height() < jQuery("#sidebar_right").height()) { jQuery("#sidebar_inner").height(jQuery("#sidebar_right").height()) } 
	
	// *------------------------------------------------------------------------------------------------------- *
	// 	Shrink images down to fit within their wrappers.
	// 	USAGE: 	The parameters of this function is a string list - ala jQuery - of
	// 			the IDs and classes of the various wrappers surrounding the images you wish to shrink
	adjustThumbs(".details_thumbImg, .cma_image_thumb, .home_carofweek_img");
	// *------------------------------------------------------------------------------------------------------- *
});

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

$(document).ready( function() {

	// =========================================== CODE FOR DROP-DOWNS IN PRIMARY NAVIGATION
	jQuery(".dropdownMenu").bgiframe();
	$(".primaryNav li").hover(
		function()
		{ 
			$(this).find(".dropdownMenu").parent().addClass("dropdownParentHover");
			$(this).find(".dropdownMenu").show();
			$(this).find(".dropdownMenu").hover(
				function()
				{
					$(this).find("li").hover(
						function() { $(this).find(".rangeDropDown").css({"display" : "block"}); },
						function() { $(this).find(".rangeDropDown").css({"display":  "none"}); }
					);
					$(this).find(".rangeDropDown a").hover(
						function(){ $(this).prev("img").css({"display" : "block"}); },
						function(){ $(this).prev("img").css({"display" : "none"}); }
					);
				},
				function(){ /*$(this).parent().removeClass("dropdownParentHover"); $(this).hide();*/}
			); 
		},
		function()
		{ 
			$(this).removeClass("dropdownParentHover"); 
			$(this).find(".dropdownMenu, .rangeDropDown, img").hide();
		}
	);
	
	$(document).pngFix(); 
	
	//sifr for logo
	if(typeof sIFR == "function"){

		// This is the preferred "named argument" syntax
		/*sIFR.replaceElement( named(
						   {
							   	sSelector:".header h1", sFlashSrc:"flash/Template2/nissanAGExtdMedium.swf", 
							   	sColor:"#b4142f", sCase:"upper", sWmode:"transparent"
							}));
		sIFR.replaceElement( named(
						   {
							   	sSelector:".header h2", sFlashSrc:"flash/Template2/nissanAGExtdLight.swf", 
							   	sColor:"#333333", sCase:"upper", sWmode:"transparent"
							}));
		sIFR.replaceElement( named(
							{
								sSelector:".maincontent h1 span", sFlashSrc:"flash/Template2/nissanAGExtdMedium.swf", 
								sColor:"#ffffff", sCase:"upper", sWmode:"transparent"
							}));
		sIFR.replaceElement( named(
							{
								sSelector:".foot_affiliates h1", sFlashSrc:"flash/Template2/nissanAGExtdMedium.swf", 
								sColor:"#808080", sCase:"upper", sWmode:"transparent"
							}));
		sIFR.replaceElement( named(
							{
								sSelector:".foot_affiliates h2", sFlashSrc:"flash/Template2/nissanAGExtdLight.swf", 
								sColor:"#808080", sCase:"upper", sWmode:"transparent"
							}));*/
	};
	
	$(".home_tabs").tabs();
	
   	// Assign Zebra Cols to Car Details
	var i = 0;
	jQuery(".itemDetails tr, .maincontent #sidebar_right .inner_content table tr, .news_headline").each( function(i){
		i % 2 == 0 ? jQuery(this).addClass("zebra_row_0") : jQuery(this).addClass("zebra_row_1");
		i++;
	});
	
	
	// remove right margin for last li
	//$(".primaryNav li:last").css({marginRight: '0'});
	jQuery(".listItem").each( function(i){ i % 2 == 0 ? jQuery(this).addClass("margin10px") : jQuery(this).addClass("margin0px"); });
	jQuery(".details_thumbImg").each( function(i){ i % 2 == 0 ? jQuery(this).addClass("margin5px") : jQuery(this).addClass("margin0px"); });
	
	// Jquery Lightbox
	jQuery(".lightbox, .lightbox a").lightBox();
	
	// put classes on links to file types
	if(document.getElementById("maincontent"))
	{
		var dls = document.getElementById("maincontent").getElementsByTagName("a");
		var link_class = "dlicon ";
		for(var x = 0; x < dls.length; x++)
		{
			var parsedObject = xtractFile(dls[x].href);
			if(parsedObject.extension) { dls[x].className = dls[x].className + " " + link_class + parsedObject.extension.substring(1, parsedObject.extension.length).toLowerCase(); }
		} // end for
	}// end if(getElement)

	// =========================================== SLIDERS FOR THE USED CAR SEARCH
	var getDate = new Date();
	var getYear = getDate.getFullYear() + 1;
	
	var currentYearFrom = querySt("YearFrom");
	var currentYearTo = querySt("YearTo");
	var currentPriceFrom = querySt("PriceFrom");
	var currentPriceTo = querySt("PriceTo");
	
	if (currentYearFrom==undefined){
	    currentYearFrom=1985;
	}
	if (currentYearTo==undefined){
	    currentYearTo=getYear;
	}
	if (currentPriceFrom==undefined){
	    currentPriceFrom=1000;
	}
	if (currentPriceTo==undefined){
	    currentPriceTo=50000;
	}
	
	//jQuery(".slider_handle_to").text("'" + getYear.toString().slice(2));
	
	jQuery(".slider-year").slider({
		range: true,
		animate: false,
		min: 1980, max: getYear,
		values: [currentYearFrom, currentYearTo],
		slide: function(event, ui) {
			jQuery(".tooltip_current_year").show();
			jQuery(this).mousemove(function(e){
				jQuery(".tooltip_current_year").text(ui.value);
				jQuery(".tooltip_current_year").css({'left' : jQuery(ui.handle).css("left"), 'margin-left' : -( jQuery(".tooltip_current_year").width() /2)});
			});
			jQuery(".slider_year_from").val(jQuery(".slider-year").slider("values", 0));
			jQuery(".slider_year_to").val(jQuery(".slider-year").slider("values", 1));
		},
		stop: function(event, ui) {/* jQuery(".tooltip_current_year").hide(); */
		    jQuery(".tooltip_current_year").blur(function(){
		            jQuery(this).hide();
		        }
		    );
		}
	});
	jQuery(".slider_year_from").val(currentYearFrom);
	jQuery(".slider_year_to").val(getYear);

	jQuery(".slider-price").slider({
		range: true,
		animate: true, 
		step:500,
		min: 500, max: 99999,
		values: [currentPriceFrom, currentPriceTo],
		slide: function(event, ui) {
			jQuery(".tooltip_current_price").show();
			jQuery(this).mousemove(function(e){
				jQuery(".tooltip_current_price").text(ui.value);
				jQuery(".tooltip_current_price").css({'left' : jQuery(ui.handle).css("left"), 'margin-left' : -( jQuery(".tooltip_current_price").width()/2)});
			});
			jQuery(".slider_price_from").val(jQuery(".slider-price").slider("values", 0));
			jQuery(".slider_price_to").val(jQuery(".slider-price").slider("values", 1));
		},
		stop: function(event, ui) { /*jQuery(".tooltip_current_price").hide(); */}
		
	});
	jQuery(".slider_price_from").val(currentPriceFrom);
	jQuery(".slider_price_to").val(currentPriceTo);


	// =========================================== KEYBOARD EVENTS
	$(document).keypress( function (e) {
		if(e.which == 120 && jQuery(".overlay").css("visibility") == "visible" )
		{
			jQuery(".overlay").css( { "visibility" : "hidden" } ); 
		}
	});


}); // END jQuery(document).ready

function showOverlay(e)
{
	jQuery(e).children(".overlayBG").bgiframe(); 						// Fix IE6 problem with input boxes
	jQuery(e).children(".overlayBG").height(jQuery(document).height());		// Set the overlay background to height of the page area
	jQuery(e).children(".overlayBG").fadeTo("fast", 0.8);					// Fade out the overlay background
	jQuery(e).css({"visibility" : "visible"}); 
	var indent = (jQuery(document).width() - jQuery(e).children(".popup_body").width()) / 2;
	jQuery(e).children(".popup_body").css( { "left" : indent } );
	jQuery("html, body").animate( { scrollTop: 0 }, "normal");
}

/* ----------------------------------- CALL THIS FUNCTION WHEN CANCELLING THE POPUP ----------------------------------- */
function closeOverlay(e)
{
	//jQuery(".overlay").fadeTo("fast", 0);
	jQuery(".overlay").css({"visibility" : "hidden"});
}

// Adjust various images based on the wrappers containing them. 
// Call function as follows: adjustThumbs(strArgs);
// Where strArgs is a string list of the classes / IDs of the wrappers
function adjustThumbs(strArgs)
{
	var imgWrapperWidth, imgWidth, widthDiff;
	var imgWrapperHeight, imgHeight, heightDiff;
	
	jQuery(strArgs).each(function() 
	{
		imgWrapperWidth = jQuery(this).width();
		imgWidth = jQuery(this).find("img").width();
		imgWrapperHeight = jQuery(this).height();
		imgHeight = jQuery(this).find("img").height();
		
		if(imgWidth > imgWrapperWidth || imgHeight > imgWrapperHeight)
		{
			var widthProp = imgWidth / imgWrapperWidth;
			var heightProp = imgHeight / imgWrapperHeight;
			
			if(widthProp > 1 && ((1 / widthProp) * imgHeight) > imgWrapperHeight)
			{
				jQuery(this).find("img").css({"width" : parseInt((1 / widthProp) * imgWidth), "height" : parseInt((1 / widthProp) * imgHeight) });
			}
			
			if(heightProp > 1 && ((1 / heightProp) * imgWidth) > imgWrapperWidth)
			{
				jQuery(this).find("img").css({"width" : parseInt((1 / heightProp) * imgWidth), "height" : parseInt((1 / heightProp) * imgHeight) });
			}
			
			widthDiff = jQuery(this).find("img").width() - imgWrapperWidth;
			heightDiff = jQuery(this).find("img").height() - imgWrapperHeight;
			jQuery(this).find("img").css({"margin-left" : parseInt(-(widthDiff / 2)) });
			jQuery(this).find("img").css({"margin-top" : parseInt(-(heightDiff / 2)) });
		}
		else
		{
			jQuery(this).find("img").css({"margin-left" : parseInt((imgWrapperWidth - imgWidth) / 2), "margin-top" : parseInt((imgWrapperHeight - imgHeight) / 2) });
		}
		
	});
}
function showHide(x)
{
	jQuery(x).toggle("normal", function(){
		if(jQuery("#sidebar_inner").height() < jQuery("#sidebar_right").height()) { jQuery("#sidebar_inner").height(jQuery("#sidebar_right").height()) } 						
	});
	
}




