var speed = 800;
var menuFadeIn = 350;
var menuFadeOut = 350;
var style = 'easeInOutExpo';
var totalWidth = 0;
var reducedWidth = 2;

(function($) {	
	$(document).ready(function(){
		try{
		   var dLeft = $('.nav li.current').offset().left;
		   var dWidth = $('.nav li.current').width() - reducedWidth;
		   var dTop = $('.nav li.current').offset().top;
		}catch (e){
		   var dLeft = $('.nav li:first').offset().left;
		   var dWidth = $('.nav li:first').width() - reducedWidth;
		   var dTop = $('.nav li:first').offset().top;		   
		}
		
		$('#box').css({left: dLeft});
		$('#box').css({top: dTop});
		$('#box .head').css({width: dWidth});
	
		$(".nav > li").hover(function(){
				var position = $(this).offset().left;
				var width = $(this).width() - reducedWidth; 
				$('#box').stop().animate({left: position},{duration:speed, easing: style});	
				$('#box .head').stop().animate({width:width},{duration:speed, easing: style});
			},	
			function(){}
		);
	
		function textWidth(text){
			var calc = '<span style="display:none">' + text + '</span>';
			$('body').append(calc);
			var width = $('body').find('span:last').width();
			$('body').find('span:last').remove();
			return width;
		};
		
		$('ul.nav ul').each(function(){     
			var Sum = 0;
			$(this).children('li').each(function(i, e){
				var tamanho = 0;
				tamanho = textWidth($(e).children('a').text());
				if (Sum < tamanho)
					Sum = tamanho;
			});
			$(this).children('li').each(function(i, e){
				$(e).width(Sum+45);
			});
		});
	
	
		$(".submenu").hover(
			function(){
				$(this).find('li').fadeIn(menuFadeIn); 
				$('li li li').css({display:"none"});
			},
			function(){  
				$(this).find('li').fadeOut(menuFadeIn); 
			}
		);  
			
		$(".submenu2").hover(
			function(){
				$(this).find('li').fadeIn(menuFadeIn);  
				$('li li li li').css({display:"none"});	
			},
			function(){  
				$(this).find('li').fadeOut(menuFadeOut); 
			}
		); 
		$(".submenu3").hover(
			function(){
				$(this).find('li').fadeIn(menuFadeIn);  
			},
			function(){  
				$(this).find('li').fadeOut(menuFadeOut); 
			}
		); 
	
	});

})($);

