﻿$(function () {
    $('#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 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;
    }
}

