﻿$(document).ready(function() {
    function megaHoverOver() {
        menuDescription($(this).find(".sub").parent()[0]);
        $(this).find(".sub").stop().fadeTo('fast', 1).show();

        //Calculate width of all ul's
        (function($) {
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                ulCount = $(this).find("ul").length;
                i = 0;
                //Calculate row
                $(this).find("ul").each(function() {
                    //Set Width
                    if (i < 2) {
                        rowWidth += $(this).width();
                    }

                    //Set Height
                    if (ulCount == 1)
                        $(this).css({ 'height': '197px' });
                    else if (i < 2)
                        $(this).css({ 'height': '107px' });
                    else if (i >= 2)
                        $(this).css({ 'height': '80px' });
                    i++;
                });
            };
        })(jQuery);

        if ($(this).find(".row").length > 0) { //If row exists...

            var biggestRow = 0;
            //Calculate each row
            $(this).find(".row").each(function() {
                $(this).calcSubWidth();
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({ 'width': biggestRow });
            $(this).find(".row:last").css({ 'margin': '0' });

        } else { //If row does not exist...

            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({ 'width': rowWidth });

        }
    }

    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function() {
            $(this).hide();
        });
    }

    function menuDescription(ilDomElement) {
        var i;
        var j = 0;

        if (ilDomElement == null) return;
        var ulDomElement = ilDomElement.parentNode;
        var categoryElement = new Array();

        for (i = 0; i < ulDomElement.children.length; i++) {
            if (ulDomElement.children[i].className != "separator") {
                categoryElement[j] = ulDomElement.children[i];
                j++;
            }
        }

        for (i = 0; i < categoryElement.length; i++) {
            if (categoryElement[i] == ilDomElement)
                showCategoryDescription(i);

        }
    }

    function showCategoryDescription(row) {
        var i;
        var categoryDescription =
            document.getElementById("categoryDescription");

        if (categoryDescription != null) {
            for (i = 0; i < categoryDescription.children.length; i++)
                categoryDescription.children[i].style.display = "none";

            categoryDescription.children[row].style.display = "";
        }
    }

    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 75, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#topnav li .sub").css({ 'opacity': '0' });
    $("ul#topnav li").hoverIntent(config);

    /*
    $(".signin-required").click(function(e) {
    e.preventDefault();
    $.get("/SignInPolarModule.aspx/SignIn", { target: $(this).attr("href") }, function(data) {
    $.blockUI({ message: data,
    css: {
    top: '168px',
    border: 'none',
    padding: '0px',
    backgroundColor: '#fff',
    cursor: 'default'
    }
    });

            $('.close').click(function() {
    $.unblockUI();
    return false;
    });
    });
    });
    */
});

