/*
*  Arquiivo de funções Javascript
*  Autor: Guilherme Zanetta Simoni
*  Criação: 08/10/2008
*  Última modificação: 15/10/2008
*/

/*
*  Função que vai ser executada quando a  página for carregada.
*
*/
document.observe("dom:loaded", function() {
		
	var Attributes;
	var pathName;
	var link;	
	if (typeof(parent.location.search.substr(1)) == "undefined"){
		Attributes = location.search.substr(1).toQueryParams();
		pathName = location.pathname;
	}
	else {
		Attributes = parent.location.search.substr(1).toQueryParams();
		pathName = parent.location.pathname;
	}
	
	
	$$("li.nivel-1").each(
		function (item){
			
			item.show();
			
			if (hasSublevels(item)){
				if (item.identify() == Attributes.uid){
					item.down('a').addClassName("hasSublevelOpen");
				}
				else{
					item.down('a').addClassName("hasSublevel");
				}			
			}
					
			var i = 0; 
			var nivel = 1;
			var abre = false; 
			var fim = false;
			
			// Verificando se o link clicado é de nivel 1
			link = item.down('a').href.substring(0,item.down('a').href.indexOf("?"));
			var indice = link.indexOf(pathName);
			
			if (indice > 0 || item.identify() == Attributes.uid){
				item.down('a').addClassName("isActiveTrue");
				expandNexts(item);
			}

			else {
			
				while ((typeof(item.next("li",i)) != "undefined")&& !(item.next("li",i).hasClassName('nivel-1'))){
					link = item.next("li",i).down('a').href;
					var indice = link.indexOf(pathName);
					
					if (indice > 0 || item.next("li",i).identify() == Attributes.uid){
					//if (item.next("li",i).identify() == Attributes.uid){
						item.next("li",i).style.display = 'block';
						item.next("li",i).down('a').addClassName("selectedSubMenu");
						item.down('a').addClassName("isActiveTrue");
						expandNexts(item);
						//expandPrevious(item.next('li', i));
					}
			
					i++;
				}
			}
			
		}
	);
	
	
	//retiraPrimeiroBackgroundMenu_topo();
	
	
	
	
});


//function retiraPrimeiroBackgroundMenu_topo(){
	//$("menu_topo").down("li").addClassName("primeiro");
//}


function isMyChild(item,uid){

	var isChild = false;
	var fim = false;
	var i = 0;
	
	while (((typeof(item.next("li",i)) != "undefined") 
	      && !(item.next("li",i).hasClassName('nivel-1')))
		  || (fim)){
							
		var myUid = item.next("li",i).identify();
						 
		if (myUid == uid)  {       
			fim = true;
			isChild = true;
		}
               
	
		i++;
	}
	
	return isChild;
	
}

function expandPrevious(item){

	var i = 0;

	while (((typeof(item.previous("li",i)) != "undefined") 
	      && !(item.previous("li",i).hasClassName('nivel-1')))){
	
		item.show();
		i++;
	}

}

function expandNexts(item){

	var i = 0;

	while (((typeof(item.next("li",i)) != "undefined") 
	      && !(item.next("li",i).hasClassName('nivel-1')))){
		item.next("li",i).style.display = 'block';
		if (hasSublevels(item.next("li",i))){
			item.next("li",i).down('a').addClassName("hasSublevelOpen2");
		}
		
		item.show();
		i++;
	}

}

function hasSublevels(item){

	var hasSublevels = false;
	
	if ((typeof(item.next("li",0)) != "undefined")){ 
	    
		i = item.className.substring(6,7);
		j = item.next("li", 0).className.substring(6,7);
		
		if (i<j){
			hasSublevels = true;
		}
	}
	
	
	return hasSublevels;

}


function expandChildMenus(nivel1Pai){

	var i = 1;
	var fim = false;
	
	//alert(nivelPai.inspect());
	while (((typeof(nivel1Pai.next("li",i)) != "undefined") 
		&& !(nivel1Pai.next("li",i).hasClassName('nivel-1')))
		|| (fim)){
		
		if (hasSublevels(nivel1Pai.next('li',i))){
			nivel1Pai.next('li',i).addClassName("hasSublevel");
		}
		
		nivel1Pai.next('li',i).show();
		i++;
	}
	return true;
}

/**
* Função: showChildMenus()
* Valores de entrada: item | elemento html de nivel 2 para cima no menu
*  			
* Valor de retorno: ---
*/

function showChildMenus(item){

// pegando o primeiro item anteior da classe de menu Nivel-1
	var nivel1Pai = item.previous("li.nivel-1");
	// contador e sinalizador
	var i = 1; var fim = false;
		
	// percorrendo todos os elementos entre o primeiro item anterior nivel-1 e o próximo item nivel-1
	while (!fim){
		nivel1Pai.next('li',i).show();
		if (nivel1Pai.next('li', i+1) === nivel1Pai.next('li.nivel-1')){
			fim = true;
		}
		
		i++;
	}

}

/**
* Função: showMenuEmpresa()
* Valores de entrada: idMenuVoce | identificador da aba de menu "Para Você"
*  			    idMenuEmpresa | idenficador da aba de menu "Para sua Empresa"
* Valor de retorno: ---
*/
function showMenuEmpresa(idMenuVoce, idMenuEmpresa){
	document.getElementById('menu_ativo').className = 'contemfloat menu_lateral empresa';
	document.getElementById(idMenuVoce).style.display = 'none';
	document.getElementById(idMenuEmpresa).style.display = '';
	pageTracker.__trackEvent("Produtos","Empresa", idMenuVoce+"_"+idMenuEmpresa);
}

function showMenuProdutosEmpresa(idMenuVoce, idMenuEmpresa){
	document.getElementById('menu_ativo').className = 'contemfloat empresa voce ativo';
	document.getElementById(idMenuVoce).style.display = 'none';
	document.getElementById(idMenuEmpresa).style.display = '';
	pageTracker.__trackEvent("Produtos","Empresa", idMenuVoce+"_"+idMenuEmpresa);
}

/**
* Função: showMenuVoce()
* Valores de entrada: idMenuVoce | identificador da aba de menu "Para Você"
*  			    idMenuEmpresa | idenficador da aba de menu "Para sua Empresa"
* Valor de retorno: ---
*/
function showMenuVoce(idMenuVoce, idMenuEmpresa){

	document.getElementById('menu_ativo').className = 'contemfloat menu_lateral voce';
	document.getElementById(idMenuVoce).style.display = '';
	document.getElementById(idMenuEmpresa).style.display = 'none';

}

function showMenuProdutosVoce(idMenuVoce, idMenuEmpresa){

	document.getElementById('menu_ativo').className = 'contemfloat voce ativo';
	document.getElementById(idMenuVoce).style.display = '';
	document.getElementById(idMenuEmpresa).style.display = 'none';

}

function getDocHeight(doc) {
  var docHt = 0;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
		if (doc.body.scrollHeight) docHt = doc.body.scrollHeight;
  }
  return docHt;
}
function setIframeHeight() {
	var iframeName = "framecentral"; 
	var iframeWin = window.frames[iframeName];
	var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
	if ( iframeEl && iframeWin ) {
		//iframeEl.style.height = "auto";// helps resize (for some) if new doc shorter than previous  
		var docHt = getDocHeight(iframeWin.document);
		// need to add to height to be sure it will all show
		if(docHt < 352){docHt=322;}
		if (docHt){ iframeEl.style.height = docHt + 30 + "px";}
	}
}