$(document).ready(function() {
    //determinamos que li tiene sub menu y agregamos un ico indicativo
    $.each($("ul#topnav li > .sub"), function(index, sub){
        //$(sub).parent().find('a > span').prepend("<div class='mm-iconsub'></div>");
        //$(sub).parent().find('strong > span').prepend("<div class='mm-iconsub'></div>");
    });

    function megaHoverOver(){
        $(this).find(".sub").stop().fadeTo('fast', 1).show();
            
        //Calculate width of all ul's
        (function($) { 
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() {					
                    rowWidth += $(this).width() + parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right")); 
                });	
            };
        })(jQuery); 
        
        //Calculate Height of all ul's
        (function($) { 
            jQuery.fn.calcMaxHeight = function() {
                maxHeight = 0;
                //Calculate row
                $(this).find("ul").each(function() {					
                    height = $(this).height() + parseInt($(this).css("padding-top")) + parseInt($(this).css("padding-bottom")); 
                    if (maxHeight < height){
                        maxHeight = height;
                    }
                });	
            };
        })(jQuery); 
        
        if ( $(this).find(".row").length > 0 ) { //If row exists...
            var biggestRow = {width : 0, height:0};
            //Calculate each row
            $(this).find(".row").each(function() {							   
                $(this).calcSubWidth();
                $(this).calcMaxHeight();
                //Find biggest row
                if(rowWidth > biggestRow.width) {
                    biggestRow.width = rowWidth;
                }                
            });
            //Set width           
            $(this).find(".sub").css({'width' :biggestRow.width});
            //Set Height
            $(this).find(".sub ul").css({'height' :maxHeight});
            $(this).find(".sub ul:first").addClass('first');
            $(this).find(".row:last").css({'margin':'0'});
            
        } else { //If row does not exist...
            $(this).calcMaxHeight();
            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({'width' : rowWidth});
            //Set Height
            $(this).find(".sub ul").css({'height' : maxHeight});
            $(this).find(".sub ul:first").addClass('first');
            
        }
    }
    
    function megaHoverOut(){ 
        $(this).find(".sub").stop().fadeTo('fast', 0, function() {$(this).hide(); });
    }
    
    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: 100, // number = milliseconds delay before onMouseOut    
         out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#topnav li .sub").css({'opacity':'0'});
    $("ul#topnav li").hoverIntent(config);

    $("ul#topnav a").hover(
        function () {
            $(this).addClass("hover");
        },
        function () {
            $(this).removeClass("hover");
        }
    );
    
    //set current li 
    var current_li = $("ul#topnav li a[href='"+currentPage+"']").parent();
    if ($(current_li)){
        if ($(current_li).parent('#topnav').is('ul')){
            $(current_li).addClass('current');
        }else{
            current_li = $(current_li).parent().parent().parent();
            $(current_li).addClass('current');
        }
    }
    
    //preload img in ccs, and show menu
    var imgs = [
        '/images/tabs_left.gif',
        '/images/tabs_right.gif',
        '/images/tabs_left_blue.gif',
        '/images/tabs_right_blue.gif',
        '/images/sunrise.png',
        '/images/navlist_arrow.png',
        '/images/sol.png',
        '/images/sol-hover.png'
    ];
    
    $.each(imgs,function(i, e) {
        $(new Image()).load(function() {
            if (i == $(imgs).length-1) {
                $('#topnav').animate({"top": "0px"}, 500, function(){
                    $('#topnav').css('z-index', '999');
                });
            }
        }).attr('src',this);
    });
});
