$(document).ready(function()
{
	//Coloca iframes
	$('a.fRS').iframe();

	//Faz resize do texto do body
	resTxt("stat");

	//Faz resize da largura do body
	resWidth("stat");

	//campo de pesquisa e de subscrição da newsletter
	$("#cEm").bind("focus", function() { var el=$(this); if (el.val()=='Indique o e-mail') el.val(""); })
			 .bind("blur", function() { var el=$(this); if (el.val()=='') el.val("Indique o e-mail"); });
	$("#fNe").bind("submit", function(e)
	{ 
		var el=$("#fNe #cEm");
		if (el.val() == "") { alert("O campo do e-mail deve ser preenchido."); return false; }
		else
		{
			/*var emailPat = /^(([^<>()[\]\\,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;*/
			var emailPat = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
			var matchArray = el.val().match(emailPat);
			if (matchArray == null) { alert("Formato do e-mail incorrecto."); return false; }
		}
		return true;
	});
	$("#cPe").bind("focus", function() { var el=$(this); if (el.val()=='Indique a pesquisa') el.val(""); })
			 .bind("blur", function() { var el=$(this); if (el.val()=='') el.val("Indique a pesquisa"); });
	$("#fPe").bind("submit", function(e)
	{
		var el=$("#fPe #cPe");
		if (el.val() == "" || el.val() == "Pesquisa" || el.val() == "Search")
		{
			if (cid == "6") alert("Please fill the search field.");
			else alert("O campo de pesquisa deve ser preenchido.");
			return false;
		}
		else if (el.val().length < 3 )
		{
			if (cid == "6") alert("The search word should be at leats size 3.");
			else alert("Por favor, pesquise com 3 ou mais caracteres.");
			return false;
		}
		return true;
	});

	//Target=_blank para links que têm rel="external"
	$("a[@rel=external]").bind("click", function(){ window.open($(this).get(0).href); return false; });

	//Target=_blank para as forms que têm rel="external"
	$("form[@rel=external]").bind("submit", function(){ $(this).get(0).target='_blank'; return true; });

	//Comportamentos para vários links
	function handler1(e)//este devia estar feito por oracle
	{
		if ((e.keyCode==13 && e.type=="keypress") || e.type=="click") window.open(url_portal+'wsdwrec0.sugerir?p_sub='+cid+'&p_url=' + rep_url(),'CIEJD','resizable=0, toolbar=0, status=0,menubar=0,width=190, height=240');
		return (e.keyCode==9)?true:false;
	};
	$("#lSu").bind("click", handler1)
			 .bind("keypress", handler1);
	function handler2(e)//este devia estar feito por oracle
	{
		if ((e.keyCode==13 && e.type=="keypress") || e.type=="click") window.open(url_portal+'wsdwrec0.recomendar?p_sub='+cid+'&p_url=' + rep_url(),'CIEJD','resizable=0, toolbar=0, status=0,menubar=0,width=190, height=350');
		return (e.keyCode==9)?true:false;
	};
	$("#lRe").bind("click", handler2)
			 .bind("keypress", handler2);
	function handler3(e)//este devia estar feito por oracle
	{
		if ((e.keyCode==13 && e.type=="keypress") || e.type=="click")
		{
			var path = rep_url().substring(rep_url().lastIndexOf('/') + 1);
			if ((path.indexOf('esquecime') > -1) || (path.indexOf('wsdiuti0') > -1) || (path.indexOf('wsdwpes9') > -1)) path = 'wsdwhom0.inicio';
			window.location = url_portal+"wsdiuti0.pagina_login?p_sub="+cid+"&p_path=" + path;
		}
		return (e.keyCode==9)?true:false;
	};
	$("#lLo").bind("click", handler3)
			 .bind("keypress", handler3);

	//Botões: Aumento diminuição de letra
	function handler4(e)//este devia estar feito por oracle
	{
		if ((e.keyCode==13 && e.type=="keypress") || e.type=="click") resTxt("less");

		return (e.keyCode==9)?true:false;
	};
	$("#bLd").bind("click", handler4)
			 .bind("keypress", handler4);
	function handler5(e)//este devia estar feito por oracle
	{
		if ((e.keyCode==13 && e.type=="keypress") || e.type=="click") resTxt("more");
		return (e.keyCode==9)?true:false;
	};
	$("#bLa").bind("click", handler5)
			 .bind("keypress", handler5);

	//Botões: Resize
	function handler6(e)//este devia estar feito por oracle
	{
		if ((e.keyCode==13 && e.type=="keypress") || e.type=="click") resWidth("normal");
		return (e.keyCode==9)?true:false;
	};
	$("#bAl, #bEn").bind("click", handler6)
				   .bind("keypress", handler6);

	//Activação simultanea de links
	var arrClass=new Array();

	$("a").each(function()
	{
		var a=$(this).get(0).className;
		if (a.search(/ml/)!=-1 && indexOf(a, arrClass)==false)
		{
			arrClass.push(a);
			$("."+a).bind("mouseover", function (e)
			{
				var el=$(this).get(0); 
				//prevenie link in images
				while (el.tagName.toLowerCase()!="a")
					el=el.parentNode;
				var arrAllLinks=$('.'+el.className);
				for(var i=0; i<arrAllLinks.length; i++)
				{
					//alert(arrAllLinks[i].className);
					if (e.type=="mouseover") arrAllLinks.eq(i).css({ textDecoration: 'underline' });
					else if (e.type=="mouseout") arrAllLinks.eq(i).css({ textDecoration: 'none' });
				}
			}).bind("mouseout", function (e)
			{
				var el=$(this).get(0); 
				//prevenie link in images
				while (el.tagName.toLowerCase()!="a")
					el=el.parentNode;
				var arrAllLinks=$('.'+el.className);
				for(var i=0; i<arrAllLinks.length; i++)
				{
					//alert(arrAllLinks[i].className);
					if (e.type=="mouseover") arrAllLinks.eq(i).css({ textDecoration: 'underline' });
					else if (e.type=="mouseout") arrAllLinks.eq(i).css({ textDecoration: 'none' });
				}
			});
		}
		else if (a.search(/image-seta/)!=-1 && indexOf(a, arrClass)==false)
		{
			$("."+a).bind("click", function (e)
			{
				var el=$(this).get(0); 
				var myId=el.id;
				var temp = myId.split('-');
				if (el.className=="image-seta-cima")
				{
					el.className="image-seta-baixo";
					$("#menu-"+temp[1]).css({ display: 'none' });
				}
				else
				{
					el.className="image-seta-cima";
					$("#menu-"+temp[1]).css({ display: 'block' });
				}
			});
		}
	});

	$("#nmu li").hover(
		function()
		{ $(this).addClass('iehover'); },
		function()
		{ $(this).removeClass('iehover'); }
	);
});