﻿/* Cart/ShoppingBag Page
============================== */
// Question CVV Panel show on hover
$('#cvvQuestionMark').hover(function () { $('#insurancePanel').removeAttr("filter").fadeIn(500) }, function () { $('#insurancePanel').fadeOut(500, function () { $(this).removeAttr("filter") }); });
function SetCheckboxStyle() { $('.checkBoxS1').ezMark({ checkboxCls: 'checkBoxS1Unchecked', checkedCls: 'checkBoxS1Checked' }); }
function ConfirmRemove(productDisplayName) { return confirm("You have selected to remove the following from your shopping bag: \n\n- " + productDisplayName + " \n\nSelect \"OK\" to remove it or \"Cancel\" to ignore this action."); }

/* Validation functions
============================== */
function resetFirstFire() { firstFire = true };

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
function disableSelection(target)
{
    if (typeof target.onselectstart != "undefined") //IE route
        target.onselectstart = function () { return false }
    else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
        target.style.MozUserSelect = "none"
    else //All other route (ie: Opera)
        target.onmousedown = function () { return false }
    target.style.cursor = "pointer"
}

/* Checkout page
============================== */
function bindRadioButtonStyles(){
    $('#radNo').ezMark({ radioCls: 'radBtnS1Unselected', selectedCls: 'radBtnS1Selected'});
    $('#radYes').ezMark({radioCls: 'radBtnS1Unselected', selectedCls: 'radBtnS1Selected'});
}

/* Product Page
============================== */
function addToCartState(counterID,buttonID)
{ var counter = document.getElementById(counterID); if (counter.value > 0) { $("#" + buttonID).removeClass("buttonS5AddToBag").addClass("buttonS5AddToBagInactive"); } else { $("#" + buttonID).addClass("buttonS5AddToBag").removeClass("buttonS5AddToBagInactive") }; }

/* Master Page Search
============================== */
// validate the text in the search text box.
function validateSearchText(textboxId) {
    var txt = document.getElementById(textboxId).value;

    // trim any white space from the start or the end of the string.
    var trimText = txt.replace(/^\s*/, "").replace(/\s*$/, "");

    if (trimText != "") {
        return true;
    };
    return false;
};

/* Menu
===================================*/
function InitialiseNagivationMenu(){
    var subMenuNo = 1;

    $('.navMenuSub').each(function(){
        // Remove the ul tag
        var $cont = $(this).find('ul').contents(); $(this).find('ul').replaceWith($cont);
        // Variables
        var columnNo = 1; var currentColHeight = 0; var prevWrapped = false;
        var subH = 30 /* Sub category header height */; 
        var liH = 23 /* Sub category height */; 
        var maxColH = 234 /* Maximum column height */
        // Loop through all li elements inside the sub menu and generate the columns.
        $(this).find('li').each(function(){
            if( $(this).hasClass('subMenuHeader')) { 
                currentColHeight = currentColHeight + subH;
                if (currentColHeight + liH > maxColH ) {
                    currentColHeight = 0;
                    // Generate column
                    $('.sub' + subMenuNo + 'col' + columnNo).wrapAll('<div class="column"><ul></ul></div>');
                    columnNo++;
                } else { prevWrapped = false };
                $(this).addClass('sub' + subMenuNo + 'col' + columnNo); 
            } 
            else { 
                currentColHeight = currentColHeight + liH; 
                $(this).addClass('sub' + subMenuNo + 'col' + columnNo); 
                if(currentColHeight + liH > maxColH || currentColHeight + subH > maxColH ){
                    currentColHeight = 0;
                    // Generate column
                    $('.sub' + subMenuNo + 'col' + columnNo).wrapAll('<div class="column"><ul></ul></div>');
                    columnNo++;
                    prevWrapped = true;
                } else { prevWrapped = false; };
            }

        });

        // Generate final column
        if(!prevWrapped) { $(this).find('.sub' + subMenuNo + 'col' + columnNo).wrapAll('<div class="column"><ul></ul></div>'); };

        //	Adjust width of the sub nav menu
        AdjustSubMenuStyling($(this));

        subMenuNo++;
    });

    // Once the menu has been rearranged, allow hover to display.
    $('.navMenuTop li').hover(
        function () { $(this).children('.navMenuSubWrapper').css("display", "block").stop().fadeTo('fast', 1); },
        function () { $(this).children('.navMenuSubWrapper').stop().fadeTo('fast', 0, function () { $(this).css("display", "none") }); 
    });
    // Navigation menu sub items hover effect
    $('.navMenuSub li').not('.subMenuHeader').hover(function () { $(this).addClass('navMenuSubItemHover'); }, function () { $(this).removeClass('navMenuSubItemHover'); });
}

function AdjustSubMenuStyling(subMenu){
    var width = 0;
    var $subMenuWrapper;
    
    subMenu.css("width", function (value){
        var $noOfColumns = subMenu.children('.column').size();
        // Add one extra pixel for each vertical line break between the columns
        var $extraPixels = $noOfColumns - 1;
        // Changes the width by the number of columns multiplied by the column width.		
        width = ($noOfColumns * 165) + $extraPixels;
        return width;
    });

    // Adjust shadow width/height
    $subMenuWrapper = subMenu.parent();
    $subMenuWrapper.width($subMenuWrapper.width() + 9).find('.menuShadowBtmC').css("width",width - 7);
    $subMenuWrapper.find('.menuShadowRight').height($subMenuWrapper.height() - 9);
}
