//SET Global Variable //
var flashMajorVer = deconcept.SWFObjectUtil.getPlayerVersion().major;
var biPageType;
flashMajorVer > 0 ? biPageType = "Flash" : biPageType = "HTML";
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);

// SET Initial Bi Tag //
	function localZip()
	{
		var e =document.getElementById("localZip").value;
		var link = "http://local.nissanusa.com/zip.aspx?regionalZipCode="+e+"&vehicle=null&intcmp=Regional.Promo.Homepage.Home.P3"
		location.href=link;
	}
	function locateDealer()
	{
		var e =document.getElementById("zipCode").value;
		if(flashMajorVer < 0){
			var link ="/apps/dealerlocator/?zipCode="+e+"&tool=home.locator&tool=HTML";
		}else{
			var link ="/apps/dealerlocator/?zipCode="+e+"&tool=home.locator&tool=Flash"
		}
		location.href=link;
	}	
	function locateDealerEs()
	{
		var e =document.getElementById("zipCode").value;
		if(flashMajorVer < 0){
			var link ="/apps/espanol/dealerlocator/?zipCode="+e+"&tool=home.locator&tool=HTML";
		}else{
			var link ="/apps/espanol/dealerlocator/?zipCode="+e+"&tool=home.locator&tool=Flash"
		}
		location.href=link;
	}
	function salesZip()
	{
		var e =document.getElementById("salesZip").value;
		var link = "http://local.nissanusa.com/zip.aspx?regionalZipCode="+e+"&vehicle=null&next=home.regional_vlp.saleseventpfa.button1."		
		window.open(link);
	}
	function salesZipEs()
	{
		var e =document.getElementById("salesZip").value;
		var link = "http://local.nissanusa.com/espanol/zip.aspx?regionalZipCode="+e+"&vehicle=null&next=home.regional_vlp.saleseventpfa.button1."
		window.open(link);
	}
	
	jQuery(document).ready(function ($) {

	    /*
	    * SLIDE SHOW with CALLBACKS
	    */
	    $('#home.english #banner-window').slideshow({
	        speed: 1200,
	        timeout: 6000,
	        type: 'sequence',
	        autoRotate: true,
	        maxRotateCount: 1, //auto rotate slide show once and stop
	        pauseOnHover: true, //don't pause slideshow when mouse is over it
	        slideDesc: 'banner-desc',
	        slideShowNav: 'banner-menu',
	        slideChangeStart: function (args) { onSlideChangeStart(args); },
	        slideChangeEnd: function (args) { onSlideChangeEnd(args); }
	    });

	    // SET Initial Bi Tag //
	    var initTagName = jQuery('.slide_0').attr('name');
	    crmEvent1({ "num": "1", "placement": "Auto", "name": initTagName, "pageType": biPageType, "flashHTML": biPageType });

	    function onSlideChangeStart(args) {

	        var slideID = args.slideID;
	        var slidePlacement = args.cause;       //comes in as 'Click' or 'Auto'
	        var slideName = args.tagName;     //name of the slide
	        var count = args.count;
	    }

	    function onSlideChangeEnd(args) { }

	    // END SLIDE SHOW


	    // hover icons	
	    $("#mainBuild a,#mainQuote a,#mainValue a,#buildNissan a,#getTrade a,#buildFrontier a,#learnJuke a,#exploreLeaf a,#salesButton,#getYourTrade a,#driveJuke a,#promoOne a,#promoTwo a").blend({ speed: 300 });

	    // mouseover promos
	    $('#promoBlocks').children().hover(function () {
	        $(this).siblings().find("a").stop().fadeTo(350, 0.65);
	    }, function () {
	        $(this).siblings().find("a").stop().fadeTo(350, 1);
	    });

	    // add arrows for secondary list spans
	    $('#secList h4').prepend('<img class="arrow-rotate" src="/hp/images/arrow-red-rotate.png" />');

	    // slide span text		
	    $('#secList').children().hover(function () {
	        $(this).stop().find("span").animate({ paddingLeft: '14px' }, { duration: 90, easing: 'easeOutQuad' });
	        $(this).find("img").animate({ opacity: "show" }, "normal");
	    }, function () {
	        $(this).stop().find("img").animate({ opacity: "hide" }, 120);
	        $(this).find("span").animate({ paddingLeft: '0px' }, { duration: 200, easing: 'easeInSine' });
	    });

	    // display local offers zip code field
	    $('#secOne:not(.zip-field) a').css({ backgroundPosition: "0 0" }).toggle(function () {
	        $(this).stop().animate(
			  { backgroundPosition: "(0 -5px)" },
			  { duration: 300 });
	        $(this).find("img,span").animate({ opacity: "hide" }, 200);
	        $('#localZipForm').animate({ opacity: "show" }, 500);
	        $('#secOne').addClass('zip-field');
	    }, function () {
	        $(this).animate(
			{ backgroundPosition: "(0 0)" },
			{ duration: 300 });
	        $('#localZipForm').animate({ opacity: "hide" }, 200);
	        $(this).find("img,span").animate({ opacity: "show" }, 500);
	        $('#secOne').removeClass('zip-field');
	    });

	    // remove broken links
	    $('img').error(function () {
	        $(this).remove();
	    });

	    // external sites in a new window
	    $("a[rel=external]").attr('target', '_blank');

	    // enter zipcode	
	    $("#zipCode,#localZip").focus(function () {
	        if ($(this).val() == "Enter ZIP") {
	            $(this).val("");
	            $(this).attr('maxlength', 5);
	        }
	    });
	    $("#zipCode,#localZip").blur(function () {
	        if ($(this).val() == "") {
	            $(this).attr('maxlength', 9);
	            $(this).val("Enter ZIP");
	        }
	    });
		
		$("#salesZip").attr('value', 'ZIP');
		
		$("#salesZip").focus(function () {
	        if ($(this).val() == "ZIP") {
	            $(this).val("");
	        }
	    });
	    $("#salesZip").blur(function () {
	        if ($(this).val() == "") {
	            $(this).val("ZIP");
	        }
	    });		
		
		// enter key takes you to Sales event
		$('#salesZip').bind('keypress', function(e) {
        	if(e.keyCode==13){
                return false; 
        	}
		});

	    // swap flash-only links for alert message
	    if (biPageType == "HTML") {
	        $('.no-flash').attr({
	            'href': '#',
	            'rel': 'popup-flash-install',
	            'class': 'poplight?w=400'
	        })
	    }

	    // When you click on a link with class of poplight and the href starts with a # 
	    $('a[class*=poplight]').click(function () {
	        var popID = $(this).attr('rel'); //Get Popup Name
	        var popURL = $(this).attr('href'); //Get Popup href to define size
	        var popW = $(this).attr('class');

	        // Pull Query & Variables from href URL
	        var query = popW.split('?');
	        var dim = query[1].split('&');
	        var popWidth = dim[0].split('=')[1]; //Gets the first query string value

	        // Fade in the Popup and add close button
	        $('#' + popID).fadeIn().css({ 'width': Number(popWidth) });

	        // Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
	        var popMargTop = ($('#' + popID).height() + 80) / 2;
	        var popMargLeft = ($('#' + popID).width() + 80) / 2;

	        // Apply Margin to Popup
	        $('#' + popID).css({
	            'margin-top': -popMargTop,
	            'margin-left': -popMargLeft
	        });

	        // Fade in Background
	        var fadeHeight = $(document).height();

	        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.

	        // Check for IE6
	        if ($.browser.msie && parseInt($.browser.version) <= 6) {
	            $('#fade').css({ 'height': fadeHeight });
	        }

	        // Check for Apple products
	        if (agentID) {
	            $('#fade').css({ 'height': fadeHeight });
	        }

	        $('#fade').css({ 'filter': 'alpha(opacity=20)' }).fadeIn(); //Fade in the fade layer 		
	        return false;
	    });

	    // Close Popups and Fade Layer
	    $('a.close, #fade').live('click', function () { //When clicking on the close or fade layer...
	        $('#fade , .popup-block').fadeOut(); //fade them both out
	        $('#fade').remove();
	        return false;
	    });

	    //alert(isFlashRedirect);
	    var isFlashRedirect = getQueryParam("flashredirect");
	    if (isFlashRedirect == "true") {
	        popFade(isFlashRedirect);
	    }

	    // MouserOver Global Nav Fade
	    function popFade(e) {
	        var fadeHeight = $(document).height();
	        var ee = e;
	        var ipad = navigator && navigator.platform && navigator.platform.match(/^(iPad|iPod|iPhone)$/) ? ipad = "true" : ipad = "false";
	        if (ee == "true") {
	            if (ipad == "false") {
	                $('#popup-flash-install').fadeIn().css({ 'width': '400px' });
	                var popMargTop = ($('#popup-flash-install').height() + 80) / 2;
	                var popMargLeft = ($('#popup-flash-install').width() + 80) / 2;
	                // Apply Margin to Popup
	                $('#popup-flash-install').css({
	                    'margin-top': -popMargTop,
	                    'margin-left': -popMargLeft
	                });
	            } else {
	                $('#popup-flash-ipad').fadeIn().css({ 'width': '400px' });
	                var popMargTop = ($('#popup-flash-ipad').height() + 80) / 2;
	                var popMargLeft = ($('#popup-flash-ipad').width() + 80) / 2;
	                // Apply Margin to Popup
	                $('#popup-flash-ipad').css({
	                    'margin-top': -popMargTop,
	                    'margin-left': -popMargLeft
	                });

	            }
	        }

	        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
	        if ($.browser.msie && parseInt($.browser.version) <= 6) {
	            $('#fade').css({ 'height': fadeHeight });
	        }
	        $('#fade').css({ 'filter': 'alpha(opacity=20)', 'z-index': '500' }).fadeIn(); //Fade in the fade layer 		
	        jQuery('#movFlashGlobalNav').css({ "z-index": "25000" });


	    }


	    jQuery('#movFlashGlobalNav').mouseover(function () {
	        popFade();
	    });
	    jQuery('#movFlashGlobalNav').mouseout(function () {
	        $('#fade').fadeOut(500); $('#fade').remove();
	    });
	    // background clickable zipcdoe
	    $('#banner-desc h1').mouseover(function () {
	        var e = getCookie('ZipCode');
		});		

	});


