function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
	$(this).find('ul:first').css({visibility: "visible",display: "none"}).animate({
    height: ['toggle']
  }, 450);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}
 
function menu_interno(){
$(" #nav_interno ul ").css({display: "none"}); // Opera Fix
$(" #nav_interno li").hover(function(){
	$(this).find('ul:first').css({visibility: "visible",display: "none"}).animate({
    height: ['toggle']
    
  }, 450);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}

 $(document).ready(function(){					
	mainmenu();
	menu_interno();
	
	
	$("#cx_distritos").bind("change", function(){
		$.post("/geral.php?modulo=abrir_concelhos",
		{
			distrito_id:	$("#cx_distritos").val()
		},
		function(data)
		{
			$("#caixa_concelho").html(data.data);
		},"json");
		return false;
	});
	
		$("#morada").fancybox({
		'autoScale'			: false,
		'href'				: '/resources/templates/morada.html',
		'titlePosition'		: 'outside',
		'type'				: 'iframe',
		'width'				: 850,
		'height'			: 454,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});

	function isInt(x)
	{
		var y=parseInt(x); 
		if (isNaN(y)) return false; 
		return x==y && x.toString()==y.toString(); 
	} 
	
	$("#bt_pesquisa_mapa").click(function () 
	{
		if($("#cx_concelhos").val())
		{
			window.location = "http://www.ravt.pt/agencias/concelho/"+$("#cx_concelhos").val();
		}
		else if($("#cx_distritos").val())
		{
			window.location = "http://www.ravt.pt/agencias/distrito/"+$("#cx_distritos").val();
		}
	});
	
	$(".bt_menu_interno").click(function () 
	{
		var id = parseInt($(this).attr('rel'));
		id = id * 1;
		if(isInt(id)==true)
		{
			$.post("/geral.php?modulo=internos",
			{
				id: $(this).attr('rel')
			},
			function(data)
			{
				$("#area_reservada").html(data);
			});		
		}
	});

	$(".bt_logout").click(function () 
	{
		$.post("/geral.php?modulo=logout",
		{},
		function(data)
		{
			if(data.status=="ok")
			{
				location.href="/";
			}
			else			
			{
				$("#mensagem_erro_login").fadeIn(100).html(data.msg).delay(5000).fadeOut(500);
			}
		},"json");
	});
	
	$("#bt_home").click(function () 
	{
		location.href="/";	
	})

	$("#bt_pesquisa").click(function () 
	{
		$.post("/geral.php?modulo=pesquisa",
		{
			pesquisa: $("#pesquisa").val()
		},
		function(data)
		{
			$("#corpo_site").html(data);
		});
	})	
	
	$("#bt_login").click(function () 
	{
		$.post("/geral.php?modulo=login",
		{
			login: $("#login").val(),
			senha: $("#password").val()
		},
		function(data)
		{
			if(data.status=="ok")
			{
				location.href="/internas";
			}
			else			
			{
				$("#mensagem_erro_login").fadeIn(100).html(data.msg).delay(5000).fadeOut(500);
			}
		},"json");
	})
	
	$('.cx_input').keypress(function(event) 
	{  
		var id = $(this).attr('id');
		if(id == "login" || id == "password")
		{
			if (event.which == '13') 
			{
				$.post("/geral.php?modulo=login",
				{
					login: $("#login").val(),
					senha: $("#password").val()
				},
				function(data)
				{
					if(data.status=="ok")
					{
						location.href="/internas";
					}
					else			
					{
						$("#mensagem_erro_login").fadeIn(100).html(data.msg).delay(5000).fadeOut(500);
					}
				},"json");
			}
		}
		else
		{
			if (event.which == '13') 
			{    
				$.post("/geral.php?modulo=pesquisa",
				{
					pesquisa: $("#pesquisa").val()
				},
				function(data)
				{
					$("#corpo_site").html(data);
				});
			}	
		}
	});
});
