/*  SEMI AUTOMATIC ACCORDIAN SCRIPT   */

var strShow = "";
var strHide ="";
var strLangShow = "SHOW ALL ";
var strLangHide = "HIDE ALL ";

if(!location.href.match("specifications")){
    if (location.href.match("espanol") || location.href.match("/es/")){
        strLangHide = "TODOS ";
        strLangShow = "TODOS ";
    } else {
        strShow = "GENUINE NISSAN ACCESSORIES";
        strHide = "GENUINE NISSAN ACCESSORIES";
    }
}else{
    if (location.href.match("espanol") || location.href.match("/es/")){
        strLangHide = "ESCONDER TODAS LAS ";
        strLangShow = "MOSTRAR TODAS LAS ";
        strShow = "ESPECIFICACIONES";
        strHide = strShow;
    } else {
        strShow = "SPECIFICATIONS";
        strHide = strShow;    
    }
}


function activate(){

    jQuery(".tMainTable td(:last-child)>p:contains('LEGEND'):not(:has(table))").each(function(){    
        jQuery(this).nextAll().css('padding-top', '6px');
    });

    var slideComplete = true;
    var bgDiv = jQuery("#tabSoaFrameBg");
    var contentDiv = jQuery("#tabSoaFrameContent");
  
    /* initially hide the specs details */ 
    jQuery("div.tSpecsContainer").hide();
    /* however enable the first DIV */ 
    jQuery("div.tSpecsContainer:first").show();
    if (location.href.match("specifications") || location.href.match("accessories")) {
             soaHeightAdjust()
    }
    //if user hits show all specs button fire tracking
    jQuery('#showAllButton').click(function(e){
        e.preventDefault();
        try{
          crmEvent3();
        }catch(e){}
    });
    /* collapsible div toggler -- common for all */ 
    jQuery("div.tAccordionTitleBar").click(function(e){  
        e.preventDefault(); 
        var nextDiv = jQuery(this).next('div');
        crmNum = jQuery(nextDiv).attr("id").toString();
        znum = parseFloat(crmNum.substring(crmNum.indexOf("Cnt")+3,crmNum.length))+3;
        eval('crmEvent' + znum + '()'); 
        if(jQuery(nextDiv).is(":hidden")){
            slideComplete = false;
            // slide down
            nextDiv.slideDown("slow" ,  function() {  slideComplete = true; });
            // execute this function as long as slideComplete is false 
                var interval = setInterval(function(){
                    if(slideComplete){ 
                        // stop repeat execution of this function                    
                        clearInterval(interval); 
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                            soaHeightAdjust()
                        }
                    } else {
                    
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                            soaHeightAdjust()
                        }
                    }
                }, 22);
            checkToggle();      
          
            /* change the color and text */ 
            paintActive(this);
            }else{
            slideComplete = false;
            
            nextDiv.slideUp("slow" , function() { slideComplete = true; });
             var interval = setInterval(function(){
                    if(slideComplete){ 
                        // stop repeat execution of this function                    
                        clearInterval(interval); 
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                            soaHeightAdjust()
                        }
                    } else {
                    
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                            soaHeightAdjust()
                        }
                    }
                }, 22);
            checkToggle();
            
            /* change the color and text */ 
            paintInactive(this);
           
        }
        
    });

    /* event handler for handling the show all and close all link */ 
    jQuery("div.tSectionTitleShowAll>a").click(function(){ 
        var linkRel = jQuery(this).attr("rel");
        var labelText;
        if(linkRel == "open"){
            slideComplete = false;
            // slide down
            jQuery("div.tSpecsContainer").slideDown("slow" ,  function() {  slideComplete = true; });
            // execute this function as long as slideComplete is false 
                var interval = setInterval(function(){
                    if(slideComplete){ 
                        // stop repeat execution of this function                    
                        clearInterval(interval); 
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                            soaHeightAdjust();
                            var t2 = setTimeout("soaHeightAdjust()",1000);
                        }
                    } else { 
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                             soaHeightAdjust();
                        }
                    }
                }, 22);        
          //  jQuery("div.tSpecsContainer").slideDown("slow");
            jQuery(this).attr("rel" , "close");
            labelText= strLangHide + strHide; //jQuery(this).attr("hideAllText");          
            jQuery("#showAll").text(labelText); 
            jQuery("#accBox_0").attr("src" , "/content/dam/nissan/docroot/img/specs-options/box_minus.gif");
            paintActive("div.tAccordionTitleBar");
        }else{                                    
             slideComplete = false;
            // slide down
            jQuery("div.tSpecsContainer").slideUp("slow" ,  function() {  slideComplete = true; });
            // execute this function as long as slideComplete is false 
                var interval = setInterval(function(){
                    if(slideComplete){ 
                        // stop repeat execution of this function                    
                        clearInterval(interval); 
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                             soaHeightAdjust();
                             var t2 = setTimeout("soaHeightAdjust()", 255);
                        }
                    } else {
                        bgDiv.height(contentDiv.height() + 10);
                        if (location.href.match("specifications") || location.href.match("accessories")) {
                            soaHeightAdjust()
                        }
                    }
                }, 22);
           // jQuery("div.tSpecsContainer").slideUp("slow");
            jQuery(this).attr("rel" , "open");
            labelText= strLangShow + strShow; //jQuery(this).attr("showAllText");          
            jQuery("#showAll").text(labelText); 
            jQuery("#accBox_0").attr("src" , "/content/dam/nissan/docroot/img/specs-options/box_white_plus.gif");
            paintInactive("div.tAccordionTitleBar");
            soaHeightAdjust();
        }
    });
}


/* function to change color of the active tabs */
function paintActive(parentDiv){
    /* modify the class */ 
    var titleDiv = jQuery(parentDiv).find("div.tSectionTitle");

    jQuery(titleDiv).attr("style" , "background-image: url(/content/dam/nissan/docroot/img/specs-options/bar_bkgd_on.gif);color:#FFF;");
    var myimg = jQuery(titleDiv).find('img');
    myimg.attr("src" , "/content/dam/nissan/docroot/img/specs-options/box_minus.gif");

    jQuery(titleDiv).find('span.tSectionTitleTxt').attr("style" , "color: rgb(255, 255, 255)");
}

/* function to change color of inactive tabs */ 
function paintInactive(parentDiv){
    /* modify the class */ 
    var titleDiv = jQuery(parentDiv).find("div.tSectionTitle");

    jQuery(titleDiv).attr("style" , "background-image: url(/content/dam/nissan/docroot/img/specs-options/bar_bkgd.gif)");

    var myimg = jQuery(titleDiv).find('img');
    myimg.attr("src" , "/content/dam/nissan/docroot/img/specs-options/box_plus.gif");

    jQuery(titleDiv).find('span.tSectionTitleTxt').attr("style" , "color: rgb(133, 133, 133)");  
}

function checkToggle(){

    /* check if all the links are open or closed */ 
    if(jQuery("div.tSpecsContainer:last").is(":hidden") && jQuery("div.tSpecsContainer:second").is(":hidden")  &&  jQuery("div.tSpecsContainer:first").is(":hidden") ){
        jQuery("div.tSectionTitleShowAll>a").attr("rel" , "open");
        jQuery("#showAll").text(strLangShow +strShow);
        jQuery("#accBox_0").attr("src" , "/content/dam/nissan/docroot/img/specs-options/box_white_plus.gif");
    }else if(jQuery("div.tSpecsContainer:last").is(":visible") && jQuery("div.tSpecsContainer:second").is(":visible")  &&  jQuery("div.tSpecsContainer:first").is(":visible") ){
        jQuery("div.tSectionTitleShowAll>a").attr("rel" , "close");
        jQuery("#showAll").text(strLangHide+strShow);
        jQuery("#accBox_0").attr("src" , "/content/dam/nissan/docroot/img/specs-options/box_minus.gif");
    }else{
        /* remain silent */
    }
}
