var menuActivo = '';
var submenuActivo = false;
////////////
//
//declarar las alturas aqui!!!!!!!!
///////////
var HeightBtnp1 = "45px";
var HeightBtnp2 = "165px";
var HeightBtnp3 = "45px";
var HeightBtnp4 = "45px";

$(document).ready(function() {
	function megaHoverOver(){
    var id= $(this).attr("class"); 
    
    if (menuActivo != '') {
    	$(".sub."+menuActivo).stop().fadeTo('fast', 1).slideUp("medium");
    }
    menuActivo = id;
    submenuActivo = true;
    
    $(".sub."+id).stop().fadeTo('fast', 1).slideDown("medium"); 
    	switch (menuActivo) {
	    	case "btnp1":	  height = HeightBtnp1;	break; 
	    	case "btnp2":	  height = HeightBtnp2;	break; 
	    	case "btnp3":	  height = HeightBtnp3;	break; 
	    	case "btnp4":	  height = HeightBtnp4;	break;  
			case "btnp1_es":  height = HeightBtnp1;	break; 
			case "btnp2_es":  height = HeightBtnp2;	break; 
			case "btnp3_es":  height = HeightBtnp3;	break; 
			case "btnp4_es":  height = HeightBtnp4;	break;  
		}		
    	$(".sub."+menuActivo).css("height", height);
    
    (function($) {
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            $(this).find("ul").each(function() { 
                rowWidth += $(this).width(); 
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { 

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	
            $(this).calcSubWidth(); 
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".row:last").css({'margin':'0'});  

    } else { 

        $(this).calcSubWidth(); 

    }
}
function megaHoverOut(){
	if (menuActivo != '' && submenuActivo == false) {
    	
    	switch (menuActivo) {
	    	case "btnp1":	height = HeightBtnp1 ;	break; 
	    	case "btnp2":	height = HeightBtnp2;	break; 
	    	case "btnp3":	height = HeightBtnp3;	break; 
	    	case "btnp4":	height = HeightBtnp4;	break;
			case "btnp1_es":	height = HeightBtnp1 ;	break; 
			case "btnp2_es":	height = HeightBtnp2;	break; 
			case "btnp3_es":	height = HeightBtnp3;	break; 
			case "btnp4_es":	height = HeightBtnp4;	break;
		}		
    	$(".sub."+menuActivo).css("height", height);
    }
}

$(".sub").hover(
		function() { submenuActivo = true;},
		function() { 
		submenuActivo = false;
		$(".sub").slideUp("slow");
		
		
		}
);


var config = {
     sensitivity: 2, 
     interval: 100, 
     over: megaHoverOver, 
     timeout: 500, 
     out: megaHoverOut 
};

$("ul#topnav li .sub").css({'opacity':'1'}); 
$("ul#topnav li").hoverIntent(config);


});