/* ADICIONAR LISTENNERS */
var menuAtual, acePosicao = null;
var arrMenus = new Array();

overMenu = function() {
	for (a = 0; a < arrMenus.length; a++) { /* ITERAR ARRAY DE MENUS PARA SETAR OS LISTENNERS */
		var sfEls = document.getElementById(arrMenus[a].id).getElementsByTagName('A');
		for (var i=0; i<sfEls.length; i++) { /* IDENTIFICAR LINKS ACESSADOS PELO EVENTO FOCUS */
				sfEls[i].onfocus=function() {
					acePosicao = String(this.id.replace('link-',''));
				}
		}
		var sfEls = document.getElementById(arrMenus[a].id).getElementsByTagName('LI');
		for (var i=0; i<sfEls.length; i++) {
				sfEls[i].onmouseover=function() { /* EXECUTAR HOVER PARA IEs | CALCULAR POSICIONAMENTOS */
					var submenu = (this.id.indexOf('item-raiz-') >= 0) ? document.getElementById(String(this.id.replace('item-raiz','submenu'))) : submenu = document.getElementById(String(this.id.replace('item','submenu'))) ;

					if (submenu) {
						if (versaoIE() == 6 || versaoIE() == 7) { /* APLICAR CLASSE HOVER PARA  (IE6&7) */
							submenu.className += 'hover';
						}
					}
				}
				sfEls[i].onmouseout=function() { /* REMOVER HOVER PARA IEs | CALCULAR POSICIONAMENTOS */
					var submenu = (this.id.indexOf('item-raiz-') >= 0) ? document.getElementById(String(this.id.replace('item-raiz','submenu'))) : submenu = document.getElementById(String(this.id.replace('item','submenu')));

					if (submenu) { /* APLICAR CLASSE HOVER PARA  (IE6&7) */
						submenu.className = submenu.className.replace('hover', '');
					}
				}
		}
	}
}
if (typeof window.addEventListener != 'undefined') {
	window.addEventListener('load', overMenu, false);
} else if (typeof document.addEventListener != 'undefined') {
	document.addEventListener('load', overMenu, false);
} else if (typeof window.attachEvent != 'undefined') {
	window.attachEvent('onload', overMenu);
}

function versaoIE() { /* RETORNAR A VERSAO EM USO DOS NAVEGADORES INTERNET EXPLORER */
	var versao;
	if (navigator.userAgent.indexOf('MSIE')>0) { //BROWSERS IE
		versao = navigator.userAgent.substring((navigator.userAgent.indexOf('MSIE')+4), navigator.userAgent.length);
		return versao.substring(0, versao.indexOf('.'));
	} else {
		return null;
	}
}

function setMenuatual(obj) { menuAtual = obj;	}

function getWindowSize(opt) { /* RETORNAR O TAMANHO DA JANELA ATUAL | OPT: 'width' ou 'height' */
	var mywidth = 0, myheight = 0;
	if( typeof( window.innerWidth ) == 'number' ) { //OUTROS NAVEGADORES
		mywidth = window.innerWidth;
		myheight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6
		mywidth = document.documentElement.clientWidth;
		myheight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4
		mywidth = document.body.clientWidth;
		myheight = document.body.clientHeight;
	}
	return eval('my' + opt.toLowerCase());
}

function ajustarItensMenu() {
	for (j = 0; j < arrMenus.length; j++) {
		var arrUL = arrMenus[j].getElementsByTagName('UL');
		for (i = 0; i < arrUL.length; i++) {
			var tObjPai = arrUL[i].offsetParent;
			while (tObjPai.offsetParent.className.indexOf('item-raiz') >= 0) {
				tObjPai = tObjPai.offsetParent;
			}

			var tempLeft  = "";
			if (navigator.userAgent.indexOf('MSIE')>0) { //BROWSERS IE
				tempLeft = (tObjPai.offsetLeft - ((tObjPai.offsetParent.offsetWidth - 954)/2));
				if (tempLeft > 0) tempLeft = "-" + tempLeft;
			} else {
				tempLeft = '-' + (tObjPai.offsetLeft - ((tObjPai.offsetParent.offsetWidth - 954)/2))  + 'px';
			}
			//arrUL[i].style.left = '-' + (tObjPai.offsetLeft - ((tObjPai.offsetParent.offsetWidth - 954)/2))  + 'px';
			arrUL[i].style.left = tempLeft;
			arrUL[i].style.width = '954px';
		}
	}
}

/* iniciar objetos do(s) menu(s) */
window.onload = function() {
	var arrUL = document.getElementsByTagName('UL');
	for (i = 0; i < arrUL.length; i++) { 
		/* MONTAR ARRAY COM MENUS DINAMICOS DA PAGINA (com classe menu-h ou menu-v) */
		if (arrUL[i].className == 'menu-h') {
			arrMenus.push(arrUL[i]);
			/* SETAR LISTENER DE IDENTIFICAÇÃO DO(S) MENU(S) */
			arrUL[i].onmouseover = function () { setMenuatual(this) }
		}
		/* SETAR Z-INDEX DECRESCENTE SOMENTE PARA ITENS RAIZ (CORREÇÃO IE) */
		var arrLI = arrUL[i].getElementsByTagName('LI');
		for (j = 0; j < arrLI.length; j++)
			if (arrLI[j].offsetParent)
				if (arrLI[j].offsetParent.id == arrUL[i].id)
					arrLI[j].style.zIndex = (arrLI.length - j);
//		if (!arrUL[i]) {
//		var arrLI = arrUL[i].getElementsByTagName('LI');
//			for (j = 0; j < arrLI.length; j++)
//				if (arrLI[j].offsetParent)
//					if (arrLI[j].offsetParent.id == arrUL[i].id)
//						arrLI[j].style.zIndex = (arrLI.length - j);
//		}
	}
	ajustarItensMenu();
}
window.onresize += function() { ajustarItensMenu(); }
