window.addEvents({
	'domready': function(){
		/* URLTEMPLATE */
		
		/*
		var estilos = document.getElementByTagName('link');
		alert('_'+estilos.length)
		for(f=0;f<estilos.length;f++)
		{h
			
		}
		
		try{
			cssEls = $(document.head).getElements('link[rel=stylesheet]');
			URLTEMPLATE = cssEls[0].href.substring(0,cssEls[0].href.lastIndexOf('/'));
			URLTEMPLATE = URLTEMPLATE.substring(0,URLTEMPLATE.lastIndexOf('/')+1);
			URLBASE = URLTEMPLATE.substring(0,URLTEMPLATE.lastIndexOf('/'));
			URLBASE = URLBASE.substring(0,URLBASE.lastIndexOf('/'));
			URLBASE = URLBASE.substring(0,URLBASE.lastIndexOf('/')+1);		
		}catch(e){}
		
		*/
		
		// oculta COMUNIDADE RENDIMENTO, se nao tiver nada
		divs = document.getElementsByTagName('div');
		c = null;
		for(i=0;i<divs.length;i++)
		{
			if(divs[i].className.indexOf('comunidaderendimento')!=-1) c = divs[i];
		}
		if(c && c.getElementsByTagName('div').length<1) c.style.display = 'none';
		
//		// config geral
//		URLTEMPLATE = 'http://intranet.rendimento.com.br/templates/estruturas/must/';
//		URLBASE = 'http://intranet.rendimento.com.br/';
//		
//		URLTEMPLATE = 'http://172.25.1.93/templates/estruturas/must/';
//		URLBASE = 'http://172.25.1.93/';
		/* BOXES EXPANSIVEIS */
		try{
			if(meio = $('meio'))
			{
				meio.getElements('h4').each(function(item, index){
					if(item.className == 'abrefecha')
					{
						item.fx = new Fx.Slide(item.getNext("div"), {
						    duration: 'long',
						    transition: Fx.Transitions.Pow.easeOut
						});
						
						item.innerHTML = '<a href="javascript:void(0)" title="">' + item.innerHTML + '</a>';
						
						item.addEvents({
							'click': function(){
								this.fx.toggle();
								this.className = this.fx.open? 'fechado': 'abrefecha';
							}
						});
					}
				});
			}
		}catch(err){}
		
		/* BUSCA */
		try{
			if(formBusca = $('buscaPortalMini'))
			{
				$('buscapessoas').addEvents({
					'click': function(){
						$('buscaconteudos').erase('class');
						this.className = 'atual';
						this.getParent('ul').className = '';
						$('buscaPortalMini').action = ACTIONBUSCAPESSOAS;
					}
				});
				
				$('buscaconteudos').addEvents({
					'click': function(){
						$('buscapessoas').erase('class');
						this.className = 'atual';
						this.getParent('ul').className = 'abaspesquisarinvertidas';
						$('buscaPortalMini').action = ACTIONBUSCA;
					}
				});
				/*
				var COD_GEMP = new Element('input', {
				    'type': 'hidden',
				    'name': 'COD_GEMP',
				    'value': ''
				});
				
				var COD_UNID = new Element('input', {
				    'type': 'hidden',
				    'name': 'COD_UNID',
				    'value': ''
				});
				
				var BUSCAR_CONTEUDO = new Element('input', {
				    'type': 'hidden',
				    'name': 'BUSCAR_CONTEUDO',
				    'value': ''
				});
				
				var COD_DEPT = new Element('input', {
				    'type': 'hidden',
				    'name': 'COD_DEPT',
				    'value': ''
				});
				
				var acao_colaboradores = new Element('input', {
				    'type': 'hidden',
				    'name': 'acao_colaboradores',
				    'value': 'colaboradores'
				});
	
				formBusca.adopt(COD_GEMP,COD_UNID,BUSCAR_CONTEUDO,COD_DEPT,acao_colaboradores);
				*/
				$('buscapessoas').fireEvent('click');
			}
		}catch(err){}
		
		/* CORES DO PORTAL */
		var tema = false;
		corPortal = function(id)
		{
			if(tema) tema.dispose();
			new Asset.css(URLTEMPLATE + 'css/-' + id + '.css', {id: 'tema'});
			tema = $('tema');
			$('coresPortal').getElements('a').each(function(item){
				if(item.id == id) item.className = 'temaselecionado';
				else item.erase('class');
			});
			Cookie.write('tema', id, {duration: 365});
		}
		if(coresportal = $('coresPortal'))
		{
			coresportal.getElements('a').addEvent('click', function(){
				corPortal(this.id);
			});
			
			corPortal((ultimoTema = Cookie.read('tema'))? ultimoTema: 'coresportalazul');
		}
		
		/* LOGOUT */
		if(e = $('btSair')) PosicionaLogout(e);
		
		/* COLABORADORES */
		if(document.getElementById('alphab'))
		{
			var arrAlpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
			var strHTML = '<ul>';
			arrAlpha.each(function(letra){
				strHTML += '<li><a href="javascript:ClicouLetraColaborador(\'' + letra + '\')" title="' + letra + '">' + letra + '</a></li>';
			});
			document.getElementById('alphab').innerHTML = strHTML + '</ul>';
		}
		
		////////////////////////////////////////
	},
	'resize': function(){
		if(e = $('btSair')) PosicionaLogout(e);
	}
});

/* GALERIA */
SelecionaGaleria = function(item)
{
	var filhosdegrandes = document.getElementById('grandes').childNodes;
	for(i=0;i<filhosdegrandes.length;i++)
	{
		var p = filhosdegrandes[i];
		if(p.tagName == 'P')
		{
			if(p.id == 'galGrande_' + item)
			{
				p.style.display = '';
				
				var filhosdepequenas = document.getElementById('pequenas').childNodes;
				for(z=0;z<filhosdepequenas.length;z++)
				{
					var pp = filhosdepequenas[z];
					if(pp.tagName == 'P')
					{
						if(pp.id == 'galPequena_' + item)
						{
							pp.style.display = 'none';
						}
						else
						{
							pp.style.display = '';
						}
					}
				}
			}
			else
			{
				p.style.display = 'none';
			}
		}
	}
}

/* LOGOUT */
function PosicionaLogout(elemento)
{
	var coordTitLogin = $('titLogin').getCoordinates();
	elemento.setStyles({
		position: 'absolute',
		left:coordTitLogin.right - 66,
		top:coordTitLogin.top
	});
}

/* INSERE FLASH */
function putFlashI4(fileName, width, height, divName, bgColor)
{
	var buffer = ''
		+'<object type="application/x-shockwave-flash" data="'+fileName+'" width="'+width+'" height="'+height+'"'+(bgColor?' bgcolor="#'+bgColor+'"':'')+'>'
		+'<param name="movie" value="'+fileName+'" />'
		+(!bgColor?'<param name="wmode" value="transparent" />':'')
		+'<param name="menu" value="false" />'
		+'<param name="quality" value="best" />'
		+'</object>';
	if(divName)
	{
		var oDiv = '';
		if(oDiv = document.getElementById(divName))
		{
			oDiv.innerHTML = buffer;
		}
	}
	else
	{
		document.write(buffer);
	}
}

/* FAZ CALCULOS PARA LARGURA MINIMA */
function MinWidthCalc()
{
	try{
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == "number" )
		{//Non-IE
				myWidth = window.innerWidth - 17;
				myHeight = window.innerHeight;
		}
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{//IE 6+ in "standards compliant mode"
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		if(myWidth<1007)
		{
			document.getElementsByTagName("body")[0].className = "pequenodemais";
			//document.getElementById("miolo").style.width = (1007 - 400) + 'px';
		}
		else
		{
			document.getElementsByTagName("body")[0].className = "";
			//document.getElementById("miolo").style.width = (myWidth - 400) + 'px';
		}
	}catch(err){}
}

/* ENCONTRA A POSICAO DO OBJETO */
function FindPos(obj)
{
	var curleft = curtop = 0;
	if(obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

/* ROTINA DE INICIALIZACAO DO MENU * /
function MenuInitialize(obj)
{
	var filhos = obj.childNodes;
	for(var i=0; i<filhos.length; i++)
	{
		var item = filhos[i];
		if(item.tagName == "UL")
		{
			var pos = FindPos(item.parentNode);
			//alert(item.parentNode.tagName + ' ' + pos);
			item.style.left = (pos[0] + 156) + 'px';
			item.style.top = pos[1] + 'px';
			item.style.width = '156px';
		}
		if(item.tagName == "LI")
		{
			item.onmouseover=function(){this.className = 'over';MenuInitialize(this)}
			item.onmouseout=function(){this.className = '';}
		}
		MenuInitialize(item);
	}
}

/* ROTINA AO REDIMENSIONAR A PAGINA */
function OnResize()
{
	MinWidthCalc();
	//MenuInitialize(document.getElementById('menuniveis'));
}

/* ROTINA AO INICIALIZAR A PAGINA */
function Initialize()
{
	window.onresize = OnResize;
	MinWidthCalc();
	addMenuEvents('lMenu');
	calcMaskHeight();
	verBoxEnquete();
	//MenuInitialize(document.getElementById('menuniveis'));
}


//oculta ENQUETES, se nao tiver nenhuma
function verBoxEnquete() {
	var temEnquete = false;
	if (document.getElementById('divBoxEnquete')) {
		var boxEnquete = document.getElementById('divBoxEnquete');
		var divs = boxEnquete.getElementsByTagName('div');
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className == 'cms_enquetes') temEnquete = true;
		}
		if(!temEnquete){
			boxEnquete.style.display = 'none';
		}
	}
}


function findObjPosX(divName) {
	var divParent = '';
    var offsetLeft = 0;
    if (document.getElementById(divName)) {
		divParent = document.getElementById(divName);
	    while (divParent) {
	        offsetLeft += divParent.offsetLeft;
	        divParent = divParent.offsetParent;
	    }
	}
	return offsetLeft;
}

function findObjPosY(divName) {
	var divParent = '';
    var offsetTop = 0;
    if (document.getElementById(divName)) {
		divParent = document.getElementById(divName);
	    while (divParent) {
	        offsetTop += divParent.offsetTop;
	        divParent = divParent.offsetParent;
	    }
	}
	return offsetTop;
}

function hideSubMenus(iNotHide,iNivel) {
	if (!iNivel) {
		var aSubMenus = document.getElementById('lContainerMenu').getElementsByTagName('div');
		for (var i = 0; i < aSubMenus.length; i++) {
			if (aSubMenus[i].id != ('subMenu'+iNotHide) && aSubMenus[i].id.match(/^subMenu.+/)) {
				aSubMenus[i].style.visibility = 'hidden';
			}
		}
	} else {
		for (var iIndiceNivel = iNivel; iIndiceNivel < aNiveisMenu.length; iIndiceNivel++) {
			for (iIndiceEsconder = 0; iIndiceEsconder < aNiveisMenu[iIndiceNivel].length; iIndiceEsconder++) {
				document.getElementById('subMenu'+aNiveisMenu[iIndiceNivel][iIndiceEsconder]).style.visibility = 'hidden';
			}
		}
	}
}

function getMenuId(oLink) {
	var idMenu = '';
	if (oLink.id) {
//		oLink.id = oLink.id.replace(/|.+$/,'');
		if (oLink.id.match(/^menu[0-9]+$/)) {
			idMenu = oLink.id.replace(/^menu/,'');
		} else {
			idMenu = oLink.id.replace(/^lSubMenu/,'');
		}
	}
	return idMenu;
}

var aNiveisMenu = new Array();
var iIdxEndPoint = 0;

var menuStartPos = 0;
var menuEndPos = 0;

function addMenuEvents(menuContainer,iNivel) {
	//alert('addMenuEvents: '+iNivel);
	
	var oDummySubImage = '';
	if (document.getElementById('dummyMenuSub')) {
		oDummySubImage = document.getElementById('dummyMenuSub').src;
	}
	
	if (document.getElementById(menuContainer)) {
		if (!iNivel) {
			iNivel = 0;
		}
		var aLinksMenus = document.getElementById(menuContainer).getElementsByTagName('a');
		if (aLinksMenus.length > 0) {
			
			if (document.getElementById('dummyMenuStart')) {
				menuStartPos = findObjPosY('dummyMenuStart');
			}
			if (document.getElementById('dummyMenuEnd')) {
				menuEndPos = findObjPosY('dummyMenuEnd');
			}

			var idMenu;
			for (var i = 0; i < aLinksMenus.length; i++) {
				//alert(aLinksMenus[i].id+'l:'+iNivel);
				//aLinksMenus[i].innerHTML += ':'+iNivel;
				aLinksMenus[i].onmouseover = function () {
					//this.innerHTML += (':'+iNivel);
					//alert('link '+iNivel);
					if (menuStartPos > 0) {
						if (!document.getElementById('__dummyStartMenu')) {
							var oDummyStart = document.createElement('img');
							oDummyStart.src = document.getElementById('lMascara').getElementsByTagName('img')[0].src;
							oDummyStart.id = '__dummyStartMenu';
							oDummyStart.style.position = 'absolute';
							oDummyStart.style.zIndex = '10';
							oDummyStart.onmouseover = function () {
								//alert('dummy 1');
								hideSubMenus();
								hideMask();
							}
							document.body.appendChild(oDummyStart);
						}
						document.getElementById('__dummyStartMenu').style.visibility = 'visible';
						document.getElementById('__dummyStartMenu').style.width = '500px';
						document.getElementById('__dummyStartMenu').style.height = '300px';
						document.getElementById('__dummyStartMenu').style.left = '0px';
						document.getElementById('__dummyStartMenu').style.top = (menuStartPos-300)+'px';
					}

					if (menuEndPos > 0) {
						if (!document.getElementById('__dummyEndMenu')) {
							var oDummyEnd = document.createElement('img');
							oDummyEnd.src = document.getElementById('lMascara').getElementsByTagName('img')[0].src;
							oDummyEnd.id = '__dummyEndMenu';
							oDummyEnd.style.position = 'absolute';
							oDummyEnd.style.zIndex = '10';
							oDummyEnd.onmouseover = function () {
								//alert('dummy 2');
								hideSubMenus();
								hideMask();
							}
							document.body.appendChild(oDummyEnd);
						}
						document.getElementById('__dummyEndMenu').style.visibility = 'visible';
						document.getElementById('__dummyEndMenu').style.width = '500px';
						document.getElementById('__dummyEndMenu').style.height = '100px';
						document.getElementById('__dummyEndMenu').style.left = '0px';
						document.getElementById('__dummyEndMenu').style.top = menuEndPos+'px';
					}
					
					//alert(iNivel);
					if (iNivel == 0) {
						hideSubMenus();
					} else {
						hideSubMenus('',iNivel);
					}
					
					var idMenu = getMenuId(this);
					var oSubMenu = false;
					if (oSubMenu = document.getElementById('subMenu'+idMenu)) {
						
						var aImgsSubMenu = oSubMenu.getElementsByTagName('img');
						var bHaveEndPoint = false;
						if (aImgsSubMenu.length > 0) {
							if (aImgsSubMenu[(aImgsSubMenu.length-1)].id.match(/^imageEndPoint___/)) {
								bHaveEndPoint = true;
							}
						}
						
						if (bHaveEndPoint == false) {
							iIdxEndPoint++;
							oSubMenu.style.left = '0px';
							oSubMenu.style.top = '0px';
							oNewEndPointImage = document.createElement('img');
							oNewEndPointImage.style.display = 'none';
							oNewEndPointImage.style.height = '1px';
							oNewEndPointImage.style.width = '1px';
							oNewEndPointImage.id = 'imageEndPoint___'+iIdxEndPoint;
							oSubMenu.appendChild(oNewEndPointImage);
							oNewEndPointImage.style.display = 'block';
							oSubMenu.style.height = (findObjPosY(oNewEndPointImage.id)-0)+'px';
							oNewEndPointImage.style.display = 'none';
						}
						
						var iDeslocamentoX = 0;
						iDeslocamentoX = (findObjPosX(this.id)+176);
						
						var iPosicaoYMenu = findObjPosY(this.id);
						var iFimAreaVisivel = parseFloat(document.documentElement.scrollTop)+parseFloat(document.documentElement.clientHeight);
						var iFimSubMenu = parseFloat(iPosicaoYMenu) + parseFloat(oSubMenu.style.height);
												
						var iDeslocamentoY = 0;
						if (iFimAreaVisivel < iFimSubMenu) {
							var iPosicaoSubMenu = oSubMenu.style.height;
							if (iPosicaoSubMenu.match(/px$/)) {
								iPosicaoSubMenu = iPosicaoSubMenu.replace(/px$/,'');
							}
							iDeslocamentoY = parseFloat(iFimAreaVisivel) - parseFloat(iPosicaoSubMenu);
						} else {
							iDeslocamentoY = iPosicaoYMenu;
						}

						showSubMenu(oSubMenu, iDeslocamentoY, iDeslocamentoX);
					}
				}
				idMenu = getMenuId(aLinksMenus[i]);
				if (document.getElementById('subMenu'+idMenu)) {
					if (!aNiveisMenu[iNivel]) {
						aNiveisMenu[iNivel] = new Array();
					}
					aNiveisMenu[iNivel][aNiveisMenu[iNivel].length] = idMenu;

					/* Muda a imagem de Bkg do P quando houver submenus */
					if (aLinksMenus[i].parentNode.nodeName.toLowerCase() == 'p' || aLinksMenus[i].parentNode.nodeName.toLowerCase() == 'li') {
						if (oDummySubImage != '') {
							aLinksMenus[i].parentNode.style.backgroundImage = 'url(\''+oDummySubImage+'\')';
							aLinksMenus[i].parentNode.style.backgroundPosition = '0';
						}
					}
					addMenuEvents('subMenu'+idMenu,(iNivel+1));
				}
			}
		}
	}
}


function showSubMenu(oSubMenu,iPosY,iPosX) {
	//alert('show sub');
	oSubMenu.style.top  = (iPosY-1)+'px';
	oSubMenu.style.left = iPosX+'px';
	oSubMenu.style.visibility = 'visible';
	oSubMenu.onmouseover = function () {
		//alert('show sub OVR');
		showMask(oSubMenu);
	}
}

function showMask(oSubMenu) {
	//alert('show mask');
	document.getElementById('lMascara').style.visibility = 'visible';
	document.getElementById('lMascara').style.display = 'block';
	document.getElementById('lMascara').onmouseover = function () {
		//alert('show mask OVR');
		hideMask();
		hideSubMenus();
	}
}
function hideMask() {
	document.getElementById('lMascara').style.display = 'none';
	document.getElementById('lMascara').style.visibility = 'hidden';
	if (document.getElementById('__dummyStartMenu')) {
		document.getElementById('__dummyStartMenu').style.visibility = 'hidden';
	}
	if (document.getElementById('__dummyEndMenu')) {
		document.getElementById('__dummyEndMenu').style.visibility = 'hidden';
	}
}

function calcMaskHeight() {
	var iEndPage = findObjPosY('fimmeio');
	if (iEndPage) {
		var oDivMask = document.getElementById('lMascara');
		oDivMask.style.height = iEndPage+'px';
		oDivMask.getElementsByTagName('img')[0].style.height = iEndPage+'px';
	}
}

var primeiravez = false;
function recarregaPage(){
    if(!primeiravez){
        jQuery('#cboxClose').live('click', function(){
            jQuery.ajax({
                url: window.location.href.toString(),
                type: 'POST',
                success: function(data){
                    //jQuery.colorbox({html:data});
                    jQuery('#enccorporativo').remove();
                    jQuery('#mioloin').prepend(data);
                    primeiravez = true;
                }
            });
            //document.location = window.location.href;
        });
    }
}
