﻿$(document).ready(function() {
    // Bottom Rounded Corners When on Homepage
    if (location.pathname == "/") {
        $('#menu').css({"border-radius":"5px","-moz-border-radius":"5px","-webkit-border-radius":"5px","-khtml-border-radius":"5px"});
    }

    // Menu items on the right swing in
    var $subs = $('.sub');
    var sublen = $subs.length - 1;
    for (var i = sublen; i > sublen - 3; i--) {
        $subs.eq(i).addClass("right");
    }

    //On Hover Over
    function megaHoverOver() {
        // Keep category highlighted while menu is shown
        if ($(this).children("a:first-child").hasClass("selected")) {
            $(this).children("a:first-child").addClass("overSubMenuSelected");
        }
        else {
            $(this).children("a:first-child").addClass("overSubMenu");
        }

        // Determine sub or subsites category
        var menuClass = ".sub";
        if ($(this).find(menuClass).length) {
            menuClass = ".sub";
        }
        else {
            menuClass = ".subsites";
            $(this).find(menuClass).css({ "right": "0" }); //Find sub and fade it in
        }

        $(this).find(menuClass).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
                $(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(menuClass).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(menuClass).css({ 'width': rowWidth }); //Set Width

        }
    }

    //On Hover Out
    function megaHoverOut() {
        // Remove category highlight on menu hide
        $(this).children("a:first-child").removeClass("overSubMenuSelected").removeClass("overSubMenu");

        if ($(this).find(".sub").length) {
            $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
                $(this).hide();  //after fading, hide it
            });
        }
        else {
            $(this).find(".subsites").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
                $(this).hide();  //after fading, hide it
            });
        }
    }

    var config = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 0, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 100, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("#menu li .sub").css({ 'opacity': '0' });
    $("#menu li .subsites").css({ 'opacity': '0' });
    $("#menu > li").hoverIntent(config);
});
