﻿//Taken from http://www.sohtanaka.com/web-design/mega-drop-downs-w-css-jquery/
//Modified by Rafael Dominguez 31/May/2010
//Updated by Rafael Dominguez 19/Jul/2010 To add a call after hovering over/out

var callAfterMethods = false;

$(document).ready(function() {

    //On Hover Over
    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in

        (function($) {
            //Function to calculate total width of all ul's
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row's width
                $(this).find("ul").each(function() { //for each ul...
                    rowWidth += $(this).width(); //Add each ul's width together
                });
            };
        }
            )(jQuery);

        if ($(this).find(".row").length > 0) { //If row exists...

            var biggestRow = 0;

            $(this).find(".row").each(function() {	//for each row...
                $(this).calcSubWidth(); //Call function to calculate width of all ul's
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });

            $(this).find(".sub").css({ 'width': biggestRow }); //Set width
            $(this).find(".row:last").css({ 'margin': '0' });  //Kill last row's margin

        } else { //If row does not exist...

            $(this).calcSubWidth();  //Call function to calculate width of all ul's
            $(this).find(".sub").css({ 'width': rowWidth }); //Set Width

        }

        if (callAfterMethods)
            AfterMegaHoverOver();
    }

    //On Hover Out
    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it
        });

        if (callAfterMethods)
            AfterMegaHoverOut();
    }

    //Set custom configurations http://cherne.net/brian/resources/jquery.hoverIntent.html
    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 2, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 2, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };

    $("ul#topnav li .sub").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
    $("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

});

function AddSubItems(topMenu, subItem, details) {
    $(".tempItem").remove(); //remove whatever is there first.

    var _SubDynamic = "#" + topMenu + "SubDynamic";
    
    if (subItem == 'PCB') { //PC Pro BlackBerry
        if ((topMenu == 'Prod') || (topMenu == 'Supp')) {
            var obj = document.getElementById( topMenu + 'PCBh3');
            if (obj != null) {
                obj.style.fontWeight = 'bold';
            }
        }
    }
    else {
        if ((topMenu == 'Prod') || (topMenu == 'Supp')) {
            var obj = document.getElementById( topMenu + 'PCBh3');
            if (obj != null) {
                if(obj.style.fontWeight != 'normal') obj.style.fontWeight = 'normal';
            }        
        }  
    }

    if (details != "") {
        $(_SubDynamic).append("<li class='tempItem'  >" + details + "</li>");
    }
}

//The Caller for triggerEvent is generated by the TopMenuControl
function triggerEvent(_element, _type) {
    if ((_element[_type] || false) && typeof _element[_type] == 'function') {
        _element[_type](_element);
    }
}

//If an application wants to enable calls to AfterMegaHoverOver/AfterMegaHoverOut 
//It should call this method on page Load. 19/Jul/2010
function enableAfterMethods() {
    callAfterMethods = true;
}
