﻿// create selector and constant if undefined
if (typeof (oSel) == "undefined") oSel = {};
if (typeof (oConst) == "undefined") oConst = {};

$.extend(oConst, {
    MegaMenu: {
        NumCols: 3,
        Width: 660
    }
});
$.extend(oSel, {
    topLevelLi : '#navWrap>ul>li'
});

function showMM(el) {
    $(el).find('.megaMenu').slideDown('fast');
}

function hideMM(el) {
    $(el).find('.megaMenu').slideUp('fast');
}

$(function () {

    /**********************\
    * Create the Mega Menu *
    \**********************/

    // for each top level link
    $(oSel.topLevelLi).each(function (i, li) {
        // create the mega menu selector based on the class of the top level LI
        var sMegaMenuId = '#' + $(li).attr('class');

        // if a mega menu element was found
        if (sMegaMenuId != '#')
            if ($(sMegaMenuId).length == 1) {
                var oSubUl = $(">ul", li).remove();

                // now get a count of the links we need to work with
                var iNumLinks = $('>li', oSubUl).length,
            iNumLiPerCol = parseInt(iNumLinks / oConst.MegaMenu.NumCols),
            iNumLiPerColRemainder = iNumLinks % oConst.MegaMenu.NumCols;

                // create the mega menu
                var oMegaMenu = $('<div class="megaMenu">');
                // add the custom megamenu content
                $(oMegaMenu).append($(sMegaMenuId).remove());
                //$(oMegaMenu).append('<img class="imgTop" src="/images/nav-bg-mega-menu-top.png" />');

                // build out the columns of LIs
                for (var i = 0; i < oConst.MegaMenu.NumCols; i++) {
                    // make a new column and set its styling
                    var oCol = $('<ul>');
                    $(oCol).css({
                        width: parseInt(oConst.MegaMenu.Width / oConst.MegaMenu.NumCols) + 'px'
                    });

                    // add the links to the new ul
                    for (var j = 0; j < iNumLiPerCol; j++)
                        $(oCol).append($('li', oSubUl).eq(0).remove());

                    // if we need to add a few additional links to the first few cols
                    if (iNumLiPerColRemainder > 0) {
                        $(oCol).append($('li', oSubUl).eq(0).remove())
                        iNumLiPerColRemainder--;
                    }

                    // add the new column
                    $(oMegaMenu).append(oCol);
                }

                // add a clearing div
                // add a clearing div
                $(oMegaMenu).append($('<div class="clr">'))
                //.append($(sMegaMenuId).remove())
                    .append($('<div class="clr">'));

                // add the bottom image
                //$(oMegaMenu).append('<img class="imgBottom" src="/images/nav-bg-mega-menu-btm.png" />');

                // add the menu back to the LI
                $(li).append(oMegaMenu);
            }



    });

    /**************************\
    * END Create the Mega Menu *
    \**************************/


    $('#navWrap ul').superfish();    // Enable Superfish Menus with HoverIntent delay
    $('#sidebar .secondaryNav ul').superfish();    // Enable Superfish Menus with HoverIntent delay


    // add first and last classes to the appropriate LI's
    $('#navWrap ul li:first-child, .nav li:first-child').addClass('first');
    $('#navWrap ul li:last-child, .nav li:last-child').addClass('last');
    $('.secondaryNav li ul').each(function () {
        $(this).siblings("a:first").addClass("sf-with-ul");
    });

    // Add hoverIntent to <li>s with megaMenu and have megaMenus slide down/up
    // Using .each() to put the hoverIntent call out of the reach of maintenance pages which do not include hoverIntent.js
    var config = {
        sensitivity: 3, // If the mouse travels fewer than this number of pixels between polling intervals, then the "over" function will be called
        interval: 150, // number = milliseconds between reading/comparing mouse coordinates
        timeout: 250, // number = milliseconds delay before onMouseOut    
        over: function () { showMM(this) },
        out: function () { hideMM(this) }
    };

    $('#navWrap .megaMenu').parent().each(function () {
        $(this).hoverIntent(config);
    });

    // add first and last classes to the appropriate LI's
    $('.breadcrumbWrap ul li:first-child').addClass('first');
    $('.breadcrumbWrap ul li:last-child').addClass('last');

    // remove the a tag from the last li if it exists
    var liLast = $('.breadcrumbWrap ul li:last-child');

    if ($('a', liLast).length == 1)
        liLast.html('<span style="display:none;">A tag removed by /js/mojoJs/navAndSearch.js</span>' + $('a', liLast).html());

    $('#formSteps li:last').addClass('last').wrapInner('<strong></strong>');

    $('legend').wrapInner('<h5></h5>');


    /****************************\
    * BEGIN Search Field Setup *
    \****************************/

    // remove default text in text fields on click and restore when empty on blur
    $('#siteSearch .txtField').focus(function () {
        clearField($(this)[0]);
        $(this).keypress(function (e) {
            return detectEnter(e);
        });
    }
    ).blur(function () {
        revertField($(this)[0]);
    }
    );

    $('#siteSearch .searchBtn').focus(function () {
        $(this).keypress(function (e) {
            return detectEnter(e);
        });
    });
    $('#siteSearch .searchBtn').click(function (e) {
        runSearch();
        return false;
    });
    /**************************\
    * END Search Field Setup *
    \**************************/

    /* 
    **** Implement as needed ****
    
    $('#brandProductSubCats').superfish();    // Enable Superfish Menus with HoverIntent delay
    $('h1 + h2').addClass("firstH2");

    // For search on keypress 'Enter'
    $("#query2").keypress(function(e)
    {
    if (e.which == 13) searchPico();
    });
    */
});

function getQueryString() {
    var result = {}, queryString = location.search.substring(1),
      re = /([^&=]+)=([^&]*)/g, m;

    while (m = re.exec(queryString)) {
        result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }

    return result;
}

function runSearch() {
    var strURL = '/search/?q=';
    var strSearch = $('#txtSearch').val();
    if (strSearch == null) {
        strSearch = $('#siteSearch .txtField').val();
    }
    if (strSearch != '') {
        strURL += strSearch;
        // go to search
        window.location = strURL;
    }
}


// run search on enter
function detectEnter(e) 
{
    var keynum;

    if (window.event) // IE
        keynum = e.keyCode;
    else if (e.which) // Netscape/Firefox/Opera
        keynum = e.which;

    if (keynum == 13)
        runSearch();

    return keynum != 13;
}

function clearField(fieldName) {
    if (fieldName.defaultValue == fieldName.value) {
        fieldName.value = "";
    }
}

function revertField(fieldName) {
    if (fieldName.value == "") {
        fieldName.value = fieldName.defaultValue;
    }
}


