// 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                                                             *
    * -- create a div on master page with an ID corresponding to the #navWrap li class *
    \**********************************************************************************/

    // 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').replace(' first','').replace(' last','');

        // 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 *
    \**************************/



    // 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

    $('#navWrap .megaMenu').each(function () {
		var oMegaMenuParent = $(this).parent();
        $(oMegaMenuParent).hoverIntent({
        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(oMegaMenuParent) },
			out: function () { hideMM(oMegaMenuParent) }
		});
    });
});
