document.observe("dom:loaded", function() {
	
	$$(".menu-sup-link-1").each(function(el) {
		if (el.parentNode.children[2].className == "menu-sup-agr-1"){
			if (el.parentNode.children[2].children.length > 0){
				el.href = "#";
			}
		}
		
	});
	/*$$(".link-1").each(function(el) {
		el.href = "#";
	}); */
});

 function subMenu(div){
        if(div.childNodes[3].className != "menu-sup-img-1"){
                div.childNodes[3].style.display = "";
        }
        if (div.childNodes[5]) {
                div.childNodes[5].style.display = "";
        }
}

function unSubMenu(div){
        if(div.childNodes[3].className != "menu-sup-img-1"){
                div.childNodes[3].style.display = "none";
        }
        if (div.childNodes[5]) {
                div.childNodes[5].style.display = "none";
        }
}


/*function subMenu(div){
	div.childNodes[3].style.display = "";
}

function unSubMenu(div){
	div.childNodes[3].style.display = "none";
} */


/* ############################################################  */

function processMenuInterno(){
	var menuContainer=document.getElementById('menu-lateral');
	
	//primeiro esconde tudo
	var divs=menuContainer.getElementsByTagName('div');
	for(var i=0;i<divs.length;i++){
		if(divs[i].className=='clagr-2'){
			divs[i].style.display='none';
		}
		if(divs[i].className=='clagr-1'){
			divs[i].style.display='none';
		}
	}
	var path=window.location.pathname;
	
	var links=menuContainer.getElementsByTagName('a');
	var theNode=null;
	theNode=getMenuNode(path,links);
	
	if(theNode==null){
		var parts=path.split('/');
		var lastPart=parts[parts.length-1];
		var idx=path.indexOf(lastPart);
		var upperPath=path.substring(0,idx-1)+'.html';
		theNode=getMenuNode(upperPath,links);
	
	}
	
	if(theNode==null){
		return;
	}
	//ajusta a font para bold do primeiro link
	//theNode.childNodes[0].style.fontWeight='bold';
	
	var childs=theNode.childNodes;
	//theNode.parentNode.style.display='';
	
	for(var i=0;i<childs.length;i++){
		if(childs[i].className=='clagr-1'){
			childs[i].style.display='';
		}
		if(childs[i].className=='clagr-2'){
			theNode.parentNode.style.display='';
			childs[i].style.display='';
		}
	}	
}

function getMenuNode(path,links){
	var theNode=null;
	for(var i=0;i<links.length;i++){
		if(links[i].href.indexOf(path)>-1 && links[i].parentNode.id.indexOf('div-')>-1){
			if(links[i].parentNode.className=='nivel-1'){
				theNode=links[i].parentNode;
				links[i].style.fontWeight='bold';
			}
			if(links[i].parentNode.className=='nivel-2'){
				theNode=links[i].parentNode;
				links[i].style.fontWeight='bold';
			}
		    if(links[i].parentNode.className=='nivel-3'){
				theNode=links[i].parentNode.parentNode.parentNode;
				links[i].style.fontWeight='bold';
			}
		    if(links[i].parentNode.className=='nivel-4'){
				theNode=links[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
				links[i].style.cssText = 'font-weight:bold;';
			}					
		}
	}
	return theNode;
}


/* ################################################################################ */

var intervalo;
var cache = null;
var divCont;
var cacheSelect;
var selecionado;

function abreConteudo(div,selec){
	if(intervalo == null){
		if (!intervalo){
		    selecionado = selec
			divCont = document.getElementById(div);
			intervalo = window.setTimeout('AF()',150);
		}
	}
}

function fechaConteudo(){
	clearTimeout(intervalo);
	intervalo = null;
}

function AF (){
	if (cache == null){
		cache = document.getElementById('PortalsIntranet');
		cacheSelect = document.getElementById('menuDefault');
	}
	cache.style.display = "none";
	cache = divCont;
	divCont.style.display = "block";
	
	cacheSelect.className = "menuHomeContOut";
	selecionado.className = "menuHomeContOver";
	cacheSelect = selecionado;
	
	clearTimeout(intervalo);
	intervalo = null;
}

/* ################################################################################ */

document.observe("dom:loaded", function() {
    var url = "/json/Getuser";
	new Ajax.Request(url, {
		method: "post",
		onSuccess: function(transport) {
			//TODO mudar para responseJSON
			var user = transport.responseText.evalJSON();
			if (user && !user.error) {
				$("header_logoout").show();
				$("username").update(user.name);
				$("username").show();
			} else {
				$("header_login").show();
				$("header_cadastre").show();
			}
		}
	});
	
});

/* Validacao Formulario */

function mascaraTelefone(ob){
	v = ob.value;
	v=v.replace(/\D/g,"")                 //Remove tudo o que n�o � d�gito
	v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca par�nteses em volta dos dois primeiros d�gitos
	v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca h�fen entre o quarto e o quinto d�gitos
	ob.value = v;
}

function mascaraData(obj){
	var v = obj.value;
	v=v.replace(/\D/g,"");
	v=v.replace(/^(\d{4})(\d)/,"$1/$2");
	v=v.replace(/^(\d{4})\/(\d{2})(\d)/,"$1/$2/$3");
	obj.value = v;
}

/* FIM Validacao Formulario */

/* ### LOGOUT ### */

function logout() {
    var url = "/live/logout";
	new Ajax.Request(url, {
		//TODO mudar para post
		method: "get",
		onSuccess: function(transport) {
			location.pathname = "/portal";
		}
	});
}

/* ### FIM LOGOUT ### */

/* ##function para o CAPCHA##*/

function refreshCapcha() {
	document.getElementById('idCaptchaValue').value = '';
	document.getElementById('idCaptcha').src = ('/navita-portal-2.0-forms/servlet/captcha?' + Math.random());
	return false;
}

/* ## FIM function para o CAPCHA##*/
