var menuopened = false;
var closetimer;
var timeout	= 1000;
var hoverMenu = false;
var isHover = false;

function closeMenus()
{
			if (menuopened)
			{
				$("#"+menuopened).parent().find("a:first").css("background", "none");
				if ($("#"+menuopened).parent().hasClass("active"))
				{
				$("#"+menuopened).parent().find("a:first").css("background", bghover);
				}

				$("#"+menuopened).hide();
				if(closetimer)
				{
					window.clearTimeout(closetimer);
					closetimer = null;
				}				
			}	
	
}

function makeRolloverMenu()
{
//	 var bghover = "url(site/medcel/imagens/bgSmoothMenu.jpg)";
	 var bghover = "";

	
	$.each($(".menu > li"), function() {
									 
		if (!$(this).find("ul.submenu").attr("class"))
		{			
			$(this).find("a:first").mouseover(function()
			{
				$("#"+menuopened).parent().find("a:first").css("background", "none");
				
				if ($("#"+menuopened).parent().hasClass("active"))
				{
					$("#"+menuopened).parent().find("a:first").css("background", "url(site/medcel/imagens/menubg.png) no-repeat");
				}
				
				$("#"+menuopened).hide();
			});
		}		
		
	});

	$.each($(".menu > li > ul"), function() {

		$(this).hide();
		var pos = $(this).parent().offset();
		pos['left'] -= 50;
		$(this).css({"left": (pos.left) + "px"});
		
		if ($.browser.msie && $.browser.version < 8)
		{
		pos['top'] += 15;
		$(this).css({"top": (pos.top) + "px"});			
		}
		
		var i = 0;
		
		$.each($(this).find("li"), function() {
			$(this).css("top", i*20+"px");
			i++;
		});
										  
		
		$(this).parent().find("a:first").mouseover(function()
		{

			if (menuopened)
			{
				$("#"+menuopened).parent().find("a:first").css("background", "none");
				if ($("#"+menuopened).parent().hasClass("active"))
				{
				$("#"+menuopened).parent().find("a:first").css("background", bghover);
				}

				$("#"+menuopened).hide();			
			}
			
			$(this).css("background", bghover);
			hoverMenu  = false;
			$(this).parent().find("ul").show();
			$(this).parent().find("ul").mouseover(function() {
														  
				hoverMenu  = true;
														  
			});
			
			$(this).parent().find("ul > li").mouseover(function() {
														  
				hoverMenu  = true;
				if(closetimer)
				{
					window.clearTimeout(closetimer);
					closetimer = null;
				}															  
			});
			
			menuopened = $(this).parent().find("ul").attr("id");
		});

	});

									 
	$(".menu > li > ul").mouseout(function() {
			
			$('#menuHome > li').stop();
			/*
			tem-se que parar o evento mouse-out (func. abaixo)
			senão a func. closeMenus disparada (in setTimeout)
			atrapalha o procedimento desta func.
			
			(victor)
			*/
			if (hoverMenu)
			{					 
				if(closetimer)
				{
					window.clearTimeout(closetimer);
					closetimer = null;
				}				
				closetimer = window.setTimeout(closeMenus, timeout);	
			}											 
	});
	
	$('#menuHome > li').mouseout(function(){
		
		if(closetimer)
		{
			window.clearTimeout(closetimer);
			closetimer = null;
		}				
		closetimer = window.setTimeout(closeMenus, timeout);	
										  
	});

}



$(function(){

	makeRolloverMenu();
});
	

