// ###########################################################
// Country Selector
// ###########################################################
$(document).ready(function () {

    $(".dropdown dt a").click(function (e) {
        $(".dropdown dd ul").toggle('fast');
        e.preventDefault();
    });

    $(".dropdown dd ul li a").click(function () {
        $(".dropdown dd ul").slideUp('fast');
    });

    $(document).bind('click', function (e) {
        var $clicked = $(e.target);
        if (!$clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").slideUp('fast');
    });
});

// ###########################################################
// Header
// ###########################################################
function buildHeaderNav(nav, navWidth) {

    // Add Multiple SiteMapNodes joined by '--'
    // Build a combined ChildMenus object
    // navData is built by SiteHeader.ascx
    var navs = nav.split('--');
    var childMenus = getChildMenus(navs);

    $('#drop-nav-container').hide();

    $('#drop-nav').empty();

    $(".mainnav-click").css("color", "#FDB813");

    $("#" + nav).css("color", "#ffffff");

    // Work with the first in the array
    var name = navData[navs[0]].Name;
    name = strtoupper(name);
    var url = navData[navs[0]].Url;

    var navPOP = '';
    if (!$.isEmptyObject(childMenus)) {
        navPOP +=
		'<div id="mainnav-title-ov">' +
        //'<div class="sprite main-nav-left lfloat"></div>' +
			'<div class="main-nav-center auto-width"><a href="' + url + '" style="width:' + navWidth + 'px">' + name + '</a></div>' +
        //'<div class="sprite main-nav-right lfloat"></div>' +
        //'<br class="clear">' +
		'</div>';

        navPOP +=
		'<div id="drop-down" class="auto-width">' +
			'<div id="mainnav-container" class="dots">' +
				'<ul id="mainnav">';

        // Loop thorugh main level cats
        $.each(childMenus, function (index, childMenu) {

            if (!childMenu.Hidden)
                navPOP += '<li><a href="' + childMenu.Url + '" class="subnav-click" id="' + index + '">' + childMenu.Name + '</a></li>';

        });

        navPOP +=
				'</ul>' +
			'</div>' +
			'<div id="drop-down-sub"></div>' +
			'<br class="clear">' +
		'</div>';
    }
    else {
        navPOP +=
		'<div id="mainnav-title-ov">' +
			'<div class="main-nav-center auto-width"><a href="' + url + '" style="width:' + navWidth + 'px">' + name + '</a></div>' +
		'</div>';
    }

    $('#drop-nav').html(navPOP);

    $('#mainnav-container').removeClass('dots');


    //$("#drop-nav").css({ //FE: caused fade issues in IE7/8
    $("#drop-nav-container").css({
        "left": ($("#" + nav).position().left - 13) + "px",
        "top": ($("#" + nav).position().top - 11) + "px"
    });

    $('#drop-nav-container').fadeIn(400);

    $('.subnav-click').mouseover(function () {
        var id = $(this).attr('id');
        // Look for Top-level Sub-Menu Ids ex. $_ROW_EN_WHERETOBUY which are part of another top-level Menu
        var regEx = new RegExp(/^\$(.*)$/);
        var subIds = regEx.exec(id);
        if (subIds && subIds.length > 0)
            buildHeaderSubNav(subIds[1]);
        else
            buildHeaderSubNav(navs[0], $(this).attr('id'));
    });

    /* FE drop-nav-container covers adjacent nav items blocking mouseover. Mouseleave on drop-nav instead 
    $('#drop-nav-container').mouseleave(function() { 
    $('#drop-nav-container').hide();
    $(".mainnav-click").css("color","#FDB813");
    });
    */

    $('#drop-nav').mouseleave(function () {
        $('#drop-nav-container').hide();
        $(".mainnav-click").css("color", "#FDB813");
    });

}

function buildHeaderSubNav(parent, nav) {

    $('#drop-down-sub').empty();
    // For Top-Level Sub-Navs 'nav' is un-defined
    var childMenus = (nav) ? navData[parent].ChildMenus[nav].ChildMenus : navData[parent].ChildMenus;
    var navPOPsub = '';
    if (!$.isEmptyObject(childMenus)) {
        navPOPsub += '<ul id="subnav">';

        $.each(childMenus, function (index, childMenu) {

            if (!childMenu.Hidden)
                navPOPsub += '<li><a href="' + childMenu.Url + '">' + childMenu.Name + '</a></li>';

        });

        navPOPsub += '</ul>';
    }
    $('#mainnav-container').addClass('dots');
    $('#drop-down-sub').html(navPOPsub);
    $('#drop-down-sub').show();
}

/*
Header Startup Scripts
*/
$(function () {

    $('.mainnav-click').mouseenter(function () {
        buildHeaderNav($(this).attr('id'), $(this).width());
    });

});

$(function () {
    // attach search click event
    $('#searchButton').click(function () {
        // set the action to include the search info
        var action = $(this).val();
        if (action.indexOf('?', 0) > 0)
            action += '&';
        else
            action += '?';

        var searchText = $('#searchText').val();
        $('#searchText').val('');

        if (location.href.indexOf(action, 0) < 0)
            location.href = action + 'search=' + searchText;
        else
            query(searchText);

        return false;
    });
    // catch enter key and submit form
    $('#searchText').keydown(function (event) {
        if (event.keyCode == '13') {
            $('#searchButton').click();
            return false;
        }
        else {
            return true;
        }
    });

    $(".defaultText").focus(function () {
        //$('#search-drop').show();
        //$('#search-drop').css('opacity', 0).animate({ opacity: '1', 'top': '22px' }, 100);

        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }

    });

    $(".defaultText").blur(function () {

        if ($(this).val() == "") {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }

    });
    /*
    $('#search').mouseleave(function () {
    //$('#search-drop').css('opacity', 1).animate({ opacity: '0', 'top': '-44px' }, 100);
    //$('#search-drop').hide();
    $(".defaultText").blur();
    });
    */
    $(".defaultText").blur();
});

// ###########################################################
// Footer
// ###########################################################
function buildSiteFooterNav(nav) {

    // Add Multiple SiteMapNodes joined by '--'
    // Build a combined ChildMenus object
    // navData is built by SiteHeader.ascx
    var navs = nav.split('--');
    var childMenus = getChildMenus(navs);

    // Work with the first in the array 
    var name = navData[navs[0]].Name;
    name = strtoupper(name);
    var url = navData[navs[0]].Url;

    var navPOP = '';
    if (!$.isEmptyObject(childMenus)) {
        // Loop thorugh main level cats
        $.each(childMenus, function (index, childMenu) {

            if (!childMenu.Hidden)
                navPOP += '<li><a href="' + childMenu.Url + '" id="' + index + '">' + childMenu.Name + '</a></li>';

        });
    }
    $('ul#nav li#' + nav + ' ul').html(navPOP);
}

function buildHomeFooterNav(nav) {

    // Add Multiple SiteMapNodes joined by '--'
    // Build a combined ChildMenus object
    // navData is built by SiteHeader.ascx
    var navs = nav.split('--');
    var childMenus = getChildMenus(navs);

    // Work with the first in the array 
    var name = navData[navs[0]].Name;
    name = strtoupper(name);
    var url = navData[navs[0]].Url;

    var navPOP = '';
    if (!$.isEmptyObject(childMenus)) {
        // Loop thorugh main level cats
        $.each(childMenus, function (index, childMenu) {

            if (!childMenu.Hidden)
                navPOP += '<li class="footer-subnav-li"><a href="' + childMenu.Url + '" id="' + index + '">' + childMenu.Name + '</a></li>';

        });
    }
    $('ul.footer-nav-ul li#' + nav).after(navPOP);
}

/*
Footer Startup Scripts
*/
$(function () {
    //footer nav mouseover
    $('#nav li').mouseenter(function () {
        $(this).children('ul').hide();
        $(this).children('ul').fadeIn(400);
    });
    $('#nav li').mouseleave(function () {
        $(this).children('ul').hide();
    });
    $('#nav li ul').mouseleave(function () {
        $(this).hide(); ;
    });
});

// ####################################################################
// Common
// ####################################################################
function getChildMenus(navs) {

    var childMenus = navData[navs[0]].ChildMenus;
    for (var i = 1; i < navs.length; i++) {
        childMenus["$" + navs[i]] = navData[navs[i]];
    }
    return childMenus;
}
