/* Path for this file: /content/dam/nissan/codepack/nissan/components/pages/layout/mainlayout.js/jcr:content/renditions/original */
// these were pulled from main JS file into mainlayout.js
/*
jQuery(document).ready(function() {
    if(jQuery("#frameBg").size()>0){
        //jQuery("#frameBg").height(jQuery("#frameContent").height()+20);
    }
    jQuery("#frameContent").click(function(){
        //jQuery("#frameContent").html(jQuery("#frameContent").html()+"<br /><br />");
        //resizeMe(jQuery("#frameContent").height());
    });    
});
function resizeMe(hgt){
    if(jQuery("#frameBg").size()>0){
        jQuery("#frameBg").height(hgt+20);
        if(jQuery("#frameContent").height() + 50 > jQuery("#pageBg").height()){
            //jQuery("#pageBg").height(jQuery("#pageBg").height()+20);
        }
    }
}
*/

jQuery(document).ready(function() {
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) === 6) jQuery('#frameBg + #frameContent').addClass('frameContentHasBg');
 
    if(jQuery("#frameContent").height() + 50 > jQuery("#pageBg").height()){         
            jQuery("#pageBg").css("margin-bottom","50px");
        }
    
});


// Variables ShowAllText & HideAllText must be set on page
var OpenedImageSrc = "/content/dam/nissan/common/icons/box-minus.gif";
var ClosedImageSrc = "/content/dam/nissan/common/icons/box-plus.gif";

jQuery(document).ready(function () {
    jQuery('.accordionGroup .tAccordionTitleBar').toggle(
        function() {
            openAccordion(jQuery('#' + jQuery(this).parent().attr("id")));
        }, function() {
            closeAccordion(jQuery('#' + jQuery(this).parent().attr("id")));
    });
});

function openAccordion(item) { 
    switchClasses(jQuery(item), "closed", "opened");
    jQuery(".accordionBackground", item).css("background-image", 
        jQuery(".accordionBackground", item).css("background-image").replace(".gif", "_on.gif")
    );
    jQuery(".accordionBox", item).attr("src", OpenedImageSrc);
    jQuery(".accordionTitle", item).css("color", "#FFFFFF");
    
    checkState("opened", HideAllText, OpenedImageSrc);
    updateBackground('#frameBg', '#frameContent');
    //soaHeightAdjust();
}

function closeAccordion(item) {
    switchClasses(jQuery(item), "opened", "closed");
    jQuery(".accordionBackground", item).css("background-image", 
        jQuery(".accordionBackground", item).css("background-image").replace("_on.gif", ".gif")
    );
    jQuery(".accordionBox", item).attr("src", ClosedImageSrc);
    jQuery(".accordionTitle", item).css("color", "#858585");
    
    checkState("closed", ShowAllText, ClosedImageSrc);
    updateBackground('#frameBg', '#frameContent');
    //soaHeightAdjust();
}

function toggleAll(id) {
    // showAll image is handled in checkState() 
    if (jQuery(id).html() == ShowAllText) {
        jQuery(id).html(HideAllText);
        jQuery('.accordionGroup.closed, .accordionGroup.closed-oversized').each(function(){ 
            jQuery(".tAccordionTitleBar", this).click(); 
        });
    } else {
        jQuery(id).html(ShowAllText);
        jQuery('.accordionGroup.opened, .accordionGroup.opened-oversized').each(function(){ 
            jQuery(".tAccordionTitleBar", this).click(); 
        });
    }
}

function checkState(state, html, img) {
    // Check whether all the accordionGroups have the given state including oversized.
    if (jQuery('.accordionGroup').size() == jQuery('.accordionGroup.' + state + ', .accordionGroup.' + state + '-oversized').size()) {
        jQuery('#showAll').html(html);
        jQuery("#showAll").parent().find('img:eq(0)').attr("src", img);
    }
}

function updateBackground(backgroundId, foregroundId) {
    jQuery(backgroundId).height(jQuery(foregroundId).height() + 20);
}

function switchClasses(element, oldClass, newClass) {
    if (element.hasClass(oldClass)) { 
        element.removeClass(oldClass).addClass(newClass);
    }
    if (element.hasClass(oldClass + "-oversized")) {
        element.removeClass(oldClass+ "-oversized").addClass(newClass+ "-oversized");
    }
}

function change() {

var Nstate = new Array()
Nstate[0] = "DE";Nstate[1] = "IL";Nstate[2] = "IN";Nstate[3] = "KY";Nstate[4] = "MD";
Nstate[5] = "MN";Nstate[6] = "OH";Nstate[7] = "WI";

var Wstate = new Array()
Wstate[0] = "AK";Wstate[1] = "AZ";Wstate[2] = "CA";Wstate[3] = "CO";Wstate[4] = "HI";Wstate[5] = "IA";
Wstate[6] = "ID";Wstate[7] = "KS";Wstate[8] = "MI";Wstate[9] = "MO";Wstate[10] = "MT";Wstate[11] = "ND";
Wstate[12] = "NE";Wstate[13] = "NM";Wstate[14] = "NV";Wstate[15] = "OR";Wstate[16] = "SD";Wstate[17] = "UT";
Wstate[18] = "WA";Wstate[19] = "WY";

var Sstate = new Array()
Sstate[0] = "AL";Sstate[1] = "AR";Sstate[2] = "DC";Sstate[3] = "FL";Sstate[4] = "GA";Sstate[5] = "LA"; 
Sstate[6] = "MS";Sstate[7] = "NC";Sstate[8] = "OK";Sstate[9] = "SC";Sstate[10] = "TN";Sstate[11] = "TX";
Sstate[12] = "VA";Sstate[13] = "WV";

var SEstate = new Array()
SEstate[0] = "CT";SEstate[1] = "MA";SEstate[2] = "ME";SEstate[3] = "NH";SEstate[4] = "NJ";SEstate[5] = "NY";
SEstate[6] = "PA";SEstate[7] = "RI";SEstate[8] = "VT";


var Nlength = Nstate.length;
var Wlength = Wstate.length;
var Slength = Sstate.length;
var SElength = SEstate.length;

match1 = document.addychange.g.options[document.addychange.g.selectedIndex].value;

// To check which page we are on

var urlName = "" + window.location;
var p = urlName.indexOf("purchase");    // positive if we are on the purchase page, if not found value is negative 
var l = urlName.indexOf("lease");    // positive if we are on the lease page, if not found value is negative

Nissan = "<b>NMAC</b>";

if (p>0)    // Execute conditions for the purchase page
{
    //Purchase Address
    PNaddy = Nissan + "<br>P.O. Box 9001132<br>Louisville, KY 40290-1132";
    //IL, IN, KY, MD, MN, OH, WI
    PWaddy = Nissan + "<br>P.O. Box 78132<br>Phoenix, AZ 85062-8132";
    //AK, AZ, CA, CO HI, IA, ID, KS, MI, MO, MT, ND, NE, NM, NV, OR, SD, UT, WA, WY
    PSaddy = Nissan + "<br>P.O.Box 49360<br>San Jose, CA 95161-9360";
    //AL, AR, DC, FL, GA, LA, MS, NC, OK, SC, TN, TX, VA, WV
    PSEaddy = Nissan + "<br>P.O. Box 17275<br>Baltimore, MD 21297-1275";
    //CT, MA, ME, NH, NJ, NY, PA, RI, VT  
}
else if (l>0)    // Execute conditions or the lease page
{
   //Purchase Address
    PNaddy = Nissan + "<br>P.O. Box 9001133<br>Louisville, KY 40290-1133";
    // DE, IL, IN, KY, MD, MN, OH, WI
    PWaddy = Nissan + "<br>P.O. Box 78133<br>Phoenix, AZ 85062-8133";
    //AK, AZ, CA, CO HI, IA, ID, KS, MI, MO, MT, ND, NE, NM, NV, OR, SD, UT, WA, WY
    PSaddy = Nissan + "<br>P.O.Box 49045<br>San Jose, CA 95161-9045 ";
    //AL, AR, DC, FL, GA, LA, MS, NC, OK, SC, TN, TX, VA, WV
    PSEaddy = Nissan + "<br>P.O. Box 17123<br>Baltimore, MD 21297-1123";
    //CT, MA, ME, NH, NJ, NY, PA, RI, VT       
}
else 
{
    //Purchase Address
    PNaddy = Nissan + "<br>P.O. Box 9001132<br>Louisville, KY 40290-1132";
    //DC, DE, IL, IN, KY, MD, MN, OH, WI
    PWaddy = Nissan + "<br>P.O. Box 78132<br>Phoenix, AZ 85062-8132";
    //AK, AZ, CA, CO HI, IA, ID, KS, MI, MO, MT, ND, NE, NM, NV, OR, SD, UT, WA, WY
    PSaddy = Nissan + "<br>P.O.Box 49360<br>San Jose, CA 95161-9360";     
    //AL, AR, FL, GA, LA, MS, NC, OK, SC, TN, TX, VA, WV     
    PSEaddy = Nissan + "<br>P.O. Box 17275<br>Baltimore, MD 21297-1275";     
    //CT, MA, ME, NH, NJ, NY, PA, RI, VT    
}


for (i=0; i<Nlength; i++)
{
    //alert(Nstate[i]);

    if (Nstate[i] == match1)
    {
        document.getElementById("showhide").style.display = "block";
        document.getElementById("corpaddy").innerHTML = PNaddy;
    }

    for (j=0; j<Wlength; j++)
    {
        if (Wstate[j] == match1)
        {
            document.getElementById("showhide").style.display = "block";
            document.getElementById("corpaddy").innerHTML = PWaddy;
        }

        for(k=0; k<Slength; k++)
        {
            if(Sstate[k] == match1)
            {
                document.getElementById("showhide").style.display = "block";
                document.getElementById("corpaddy").innerHTML = PSaddy;
            }

            for (l=0; l<SElength; l++)
            {
                if(SEstate[l] == match1)
                {
                    document.getElementById("showhide").style.display = "block";
                    document.getElementById("corpaddy").innerHTML = PSEaddy;
                }
            }

        }

    }

}

}

jQuery(document).ready(
function()
{
  jQuery('.tSectionTitleShowAll > a').css('text-decoration', 'none');
  return;
});

