$(document).ready(function() {
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
		//Calcula los uls
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//calcula registrooo
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		if ( $(this).find(".row").length > 0 ) { //si el registro existe
			var biggestRow = 0;	
			//calcula cada registro
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//busca registro mas grande
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//establece width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
		} else { //si no existeee....
			$(this).calcSubWidth();
			//establece Width
			$(this).find(".sub").css({'width' : rowWidth});
		}
	}
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
	var config = {    
		 sensitivity: 2,     
		 interval: 100,     
		 over: megaHoverOver,   
		 timeout: 200,   
		 out: megaHoverOut   
	};
	$("ul#topnav li .sub").css({'opacity':'.5'});
	$("ul#topnav li").hoverIntent(config);
});

