var menuopened;
var closetimer;
var timeout	= 500;
var hoverMenu = 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 = "";
									 
	$(".menu > li > ul").mouseout(function() {
			if (hoverMenu) {					 
				if(closetimer)
				{
					window.clearTimeout(closetimer);
					closetimer = null;
				}				
				closetimer = window.setTimeout(closeMenus, timeout);	
			}
											 
	});

	$.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");
		});


		

	});

	$.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();
			});
		}		

	});


}

$(document).ready(function() {$("#areaAlunoLogin").slideUp();	 makeRolloverMenu(); });

