/* REMOVE WHEN FIX IS IN expand/collapse CSE 'narrow by' sections  - this must be after the ajax further down*/

function FnMinMax(ul,h) {
    var sublist = document.getElementById(ul);
    if (sublist.className == "block") {
        sublist.className = "hide";
        sublist.style.display = 'none';
        sublist.parentNode.className = 'closed';
        //h.style.backgroundImage = "url(/contents/images/estores/buttons/maxi.gif)";
    } 
    else if (sublist.className == "hide") {
        sublist.className = "block";
        sublist.style.display = 'block';
        sublist.parentNode.className = '';
        //h.style.backgroundImage = "url(/contents/images/estores/buttons/mini.gif)";
    }
}


/* show/hide CSE price comparison tabs */
function FnGenTabOn(on,off1,off2,tabon,taboff1,taboff2) {
    document.getElementById(on).className = "csetab_content" + " csecontenton";
    document.getElementById(off1).className = "csetab_content" + " csecontentoff";
    document.getElementById(off2).className = "csetab_content" + " csecontentoff";
    document.getElementById(tabon).className = "csetab_generic" + " csetabgenon";
    document.getElementById(taboff1).className = "csetab_generic" + " csetabgenoff";
    document.getElementById(taboff2).className = "csetab_generic" + " csetabgenoff";
}

/*cse form val */
function fnValSearch(field){
    if (document.getElementById(field).value === '') {
      alert("Please enter a search term"); 
        return false; 
    }
}

//Narrow by nested 'Show all' click function
function cseNBShowAllFn() {
    $(this).parent().parent().children().show().filter(".cse_nbshowall").hide();
    return false;
}

function cseTabsSHFn() {
    var strTab = "";
    var intTab = $(this).attr("href").length; // get clicked item's href length
    strTab = $(this).attr("href").substring(7,intTab); // get tab number from href
    //alert(strTab);
    tabContainers.hide().filter("#csecontenttab" + strTab).show(); // Hide all tab content except for active tab
    
    $('ul.cse_tabs li').removeClass('csetabgenon').addClass('csetabgenoff'); // Apply OFF class to all tab list items and remove ON class
    $("#csegenerictab" + strTab).removeClass('csetabgenoff').addClass('csetabgenon'); // Apply ON class to active tab list item and remove OFF class
    return false;
}
        

jQuery(document).ready(function($){
    
    // Toggle 'show more/less' for cse copy - added jan2010
    var p = $('.cseseo p');
    var l = p.length;
    var last = p.eq(l-1);
        
    if(l > 1){
        var pslice = p.slice(1);
        pslice.hide();
        last.after('<p id="moreseo"><a href="#">Read more&hellip;</a></p>');
    }
    
    $('#moreseo').toggle(
        function(){
            pslice.show();
            $('#moreseo a').text('...Show less');
         },
          function(){
            pslice.hide();
            $('#moreseo a').text('Read more...');
          }
    );
    
    
        
   //'Narrow By': Price range validation
   $('input#pricefrom, input#priceto').each(function(){
           $(this).after('<span class="priceerror" style="display: none;">Please enter a valid price</span>');
   });
    
   $('.accordionFilterList form').submit(function(){
       var error = 0;
       
       $('input#pricefrom, input#priceto').each(function(){
           var userprice = $(this).val().replace("\xA3",""); //pound sign
           
        if(!userprice.match(/[0-9]/ig) || !userprice){                   
               $(this).next('span.priceerror').show();
               error++;
           } else {
               $(this).next('span.priceerror').hide();
               $(this).val(userprice);
           }
       });
       
       if(error) {
           return false;
       }
   });
           
           
    //cse Product page 'more info' link shows tab2
    $('.selectedProduct a[href="#tab2"]').click(function(){
        $('.jsTabsContainer a[href="#tab2"]').click();
        return false;
    }); 
    
    
    
    // cseTabs toggle
    if ($(".cse_tabs").length > 0 ) {
    
        var tabContainers = $('div.csetab_content'); // Build array of tab items    
        
        //var strHref = window.location.href; // Check whether there is a reference to a tab in the url and activate appropriate tab
        //if ( strHref.indexOf("#") > -1 ){
            //var intTab = strHref.substr(strHref.indexOf("#"));    
            //var intTab = intTab.length;    
            //var strTab = strHref.substr(strHref.indexOf("#")).substring(7,intTab);
            
            //tabContainers.hide().filter("#csecontenttab" + strTab).show(); // Hide all tab content except for active tab
              
            //$('ul.cse_tabs li').removeClass('csetabgenon').addClass('csetabgenoff'); // Apply OFF class to all tab list items and remove ON class
              //$("#csegenerictab" + strTab).removeClass('csetabgenoff').addClass('csetabgenon'); // Apply ON class to active tab list item and remove OFF class
            //strTab = "";
        //}
        var strQuery = window.location.search.substring(1);
        if ( strQuery.indexOf("&") > -1 ){
            var strVars = strQuery.split("&");
            for (var i=0;i<strVars.length;i++) {
                var strTab = strVars[i].split("=");
                if (strTab[0] == "csetab") {
                    tabContainers.hide().filter("#csecontenttab" + strTab[1]).show(); // Hide all tab content except for active tab
                      
                    $('ul.cse_tabs li').removeClass('csetabgenon').addClass('csetabgenoff'); // Apply OFF class to all tab list items and remove ON class
                      $("#csegenerictab" + strTab[1]).removeClass('csetabgenoff').addClass('csetabgenon'); // Apply ON class to active tab list item and remove OFF class
                    strTab = "";
                }
            }
        }
        
        $('ul.cse_tabs a').click(cseTabsSHFn); // Call function cseTabsSHFn when this item clicked
        $('p.pri_comp_review a').click(cseTabsSHFn);
        $('p.pri_comp_info a').click(cseTabsSHFn);

    }
    

    // Inject rel="lightbox" attribute into "view larger image" anchor to trigger lightbox code 
    /*
    $(".pri_comp_itemimg > a").attr({rel: "lightbox"}).end();
    $('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel and apply lightbox
    */
    
    //reduce size of retailer and manufacturer narrow by items to display only 5 and inject "show all" option
    var nb = $(".cse_nb");
    if (nb.length > 0 ) {
        nb.find("#cse_nbretailer > li:gt(4)").hide().parent().append("<li class='cse_nbshowall'><a href='#'>Show all</a></li>");
        nb.find("#cse_nbmanufacturer > li:gt(4)").hide().parent().append("<li class='cse_nbshowall'><a href='#'>Show all</a></li>");        
    }
    
    //Accordion filter     
    //$('#accordionFilter > li:lt(9)').find('h3 + .accordionFilterList > li:gt(5)').hide().parent().append("<li class='cse_nbshowall'><a href='#'>Show all</a></li>"); //added apr2010 
    
    $(".cse_nbshowall > a").click(cseNBShowAllFn); 
    
    //Ajax call for 'narrow by' sub sections
    $("#filter h3:gt(8) > a").click(function(){
            
        var nbList = $(this).attr("href");
        var nbH = $(this).parent();
        var nbLi = nbH.parent();
        if(nbLi.hasClass('closed')){
                nbLi.removeClass('closed');
        } else {
                nbLi.addClass('closed');
        }
        
        $.ajax({
        url: nbList,
        type: 'POST',
        dataType: 'html',
            timeout: 5000,
            error: function(){
                alert('Error loading list');
            },
            success: function(html){
              
                var result = $(html).filter("div").html();
                var nbH2 = $(nbH).text().toLowerCase().replace(/^\s+|\s+$/g, ''); // get the h4's text, lower case it and trim it
          
                if(result === null){
                    result = 'No further info currently available';
                }
                
                $(nbH).replaceWith("" + result);
                
                //nbH2 = nbH2.replace(" ","_"); 
                // replace any spaces inside h4 text with _
                var space = " ";
                while (nbH2.indexOf(space)>-1) {
                    int = nbH2.indexOf(space);
                    nbH2 = "" + (nbH2.substring(0, int) + "_" + nbH2.substring((int + space.length), nbH2.length));
                }
                
                $("ul[id$=" + nbH2 + "]").children("li:gt(4)").hide().parent().append("<li class='cse_nbshowall'><a href='#'>Show all</a></li>"); // look for the list with id ending with h4 text and apply 'show all' link
                $(".cse_nbshowall > a").click(cseNBShowAllFn); //call cseNBShowAllFn function when 'show all' is clicked (this has to be repeated inside Ajax call because content is not loaded on page load)
            }
        }); 
        return false;
    })
    .end();        
    
    
    if ($("#retailerListStatic").length > 0 ) { 
        $("#retailerListStatic").removeAttr("id");
        $(".retListHover").hover(
          function() {
                $(this).next().show();
          }, 
          function() {
            $(this).next().hide();
          });
    }
    

    
});
