function temClasse (obj,classe) {
	var tem = false;

	if (obj && obj.className) {

		if (obj.className == classe) {
			tem = true;

		} else if (obj.className.indexOf(' ') > -1) {
			var arr = obj.className.split(' ');
			for (var i in arr) {
				if (arr[i] == classe) {
					tem = true;
					break;
				}
			}
		}

	}

	return tem;
}


/*****************************************************
* int posicaoX/Y (element obj)
* 	- determinação de posicionamento absoluto X/Y
******************************************************/
function posicaoX (obj) {
	if (obj.currentStyle)
		var tmp = obj.currentStyle;
	else
		var tmp = window.getComputedStyle(obj,null);

	if (tmp.position == 'absolute')
		return 0;


	var x = obj.offsetLeft;
	if (obj.offsetParent != null) x += posicaoX (obj.offsetParent);
	return x;
}
function posicaoY (obj) {
	if (obj.currentStyle)
		var tmp = obj.currentStyle;
	else
		var tmp = window.getComputedStyle(obj,null);

	if (tmp.position == 'absolute')
		return 0;

	var y = obj.offsetTop;
	if (obj.offsetParent != null) y += posicaoY (obj.offsetParent);
	return y;
}

/*****************************************************
* desvanece (element obj, int i, int f, int t, int fase)
* 	- efeito desvanecência com alfas
* 	- params:
* 		i: alfa inicial
* 		f: alfa final
* 		t: intervalo de tempo (> 0)
* 		fase: variação de alfa (> 0)
******************************************************/
function desvanece (obj, i, f, t, fase) {

	if (!t || t < 0) t = 25;
	if (!fase || fase < 0) fase = 15;

	var desvanecer;
	if (desvanecer = i > f) {
		var max = i;
		var min = f;
		var soma = -fase;
	} else {
		var max = f;
		var min = i;
		var soma = fase;
	}

	if (!obj.alfa) obj.alfa = desvanecer ? max : min;

	if (ie) tmp = obj.currentStyle;
	else tmp = window.getComputedStyle(obj,null);

	if (!desvanecer && (tmp.display == '' || tmp.display == 'none')) obj.style.display = 'block';

	obj.alfa += soma;
	if (obj.alfa < min) obj.alfa = min;
	else if (obj.alfa > max) obj.alfa = max;

	// internet explorer 8, não há pachorra para os anteriores...
	if (ie8) {
		obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+obj.alfa+")";
	// browser como deve de ser...
	} else obj.style.opacity = obj.alfa/100;

	clearTimeout (obj.ciclo);
	if (desvanecer && obj.alfa > min || !desvanecer && obj.alfa < max)
		obj.ciclo = setTimeout (function () { desvanece (obj,i,f,t,fase); }, t);

	if (desvanecer && obj.alfa <= 0) obj.style.display = 'none';
}



/**
 * Abrir/fechar menu. Colocação automática dependendo do estilo. Desvanecimento.
 *
 * @param DOMElement	menuitem
 * @return void
 */
function menu (menuitem) {

	var submenu = menuitem.submenu;

	if (menuitem.aberto ? 0 : 1) {

		menuitem.aberto = 1;

		if (ie) var tmp = menuitem.currentStyle.styleFloat;
		else var tmp = window.getComputedStyle (menuitem, null).cssFloat;
		if (tmp != 'none') {
			var x = posicaoX (menuitem);
			var y = posicaoY (menuitem) + menuitem.offsetHeight;
		} else {
			var x = posicaoX (menuitem) + menuitem.offsetWidth;
			var y = posicaoY (menuitem);
		}


		submenu.style.left = x + "px";
		submenu.style.top = y + "px";

		desvanece (submenu, 0, 100, 25, 15);

	} else {

		menuitem.aberto = 0;
		desvanece (submenu, 100, 0, 25, 15);
	}
}

/**
 * Preparar menu para responder aos movimentos do rato via JavaScript.
 */
function preparaMenu (id) {
	var menuPrincipal = document.getElementById(id);

	if (menuPrincipal.getElementsByClassName) {
		var menus = menuPrincipal.getElementsByClassName('menuPai');

	} else {
		var menus = [];

		var tmp = menuPrincipal.getElementsByTagName("ul");
		for (var i = 0; i < tmp.length; i++) {
			if (temClasse(tmp[i].parentNode, 'menuPai')) menus.push(tmp[i].parentNode);
		}
	}

	for (var i in menus) {

		var submenu = menus[i].firstChild;
		while (submenu && submenu.nodeName.toLowerCase() != 'ul') {
			submenu = submenu.nextSibling;
		}

		if (submenu && submenu.nodeName.toLowerCase() == 'ul' && temClasse(submenu, 'menu')) {

			// Não é necessário mover a div para fora. É tudo uma questão de overflow: visible do menu pai
// 			menu.appendChild(submenu);
			// Aplicar novo CSS
			submenu.className += " menuPopup";

			// Registar hierarquias para facilitar a abertura/fecho dos menus
			menus[i].submenu = submenu;
			if (temClasse (menus[i].parentNode, 'menu')) {
				submenu.menuPai = menus[i].parentNode;
			}

			// Granda tralha... tudo isto porque o IE decide tomar apenas o último valor de i e submenu...
			if (ie) {


				var tmp = function (obj) {return function (ev) {
					if (obj.aberto) return;

					var tmp = ev.fromElement;
					while (tmp && tmp != obj.submenu) {
						tmp = tmp.parentNode;
					}
					if (!tmp || tmp != obj.submenu)
						menu(obj);

					ev.cancelBubble = true;
				}};
				menus[i].attachEvent("onmouseover", tmp (menus[i]));


				var tmp = function (obj) {return function (ev) {
					if (!obj.aberto) return;

					var tmp = ev.toElement;
					while (tmp && tmp != obj && tmp != obj.parentNode) {
						tmp = tmp.parentNode;
					}
					if (!tmp || tmp != obj) {
						menu(obj);

						if (tmp != obj.parentNode) {
							tmp = obj.parentNode.parentNode;
							while (tmp.submenu) {
								menu(tmp);
								tmp = tmp.parentNode.parentNode;
							}
						}
					}

					ev.cancelBubble = true;
				}};
				menus[i].attachEvent("onmouseout", tmp (menus[i]));


			} else {
				menus[i].addEventListener("mouseover",
					function (ev) {
						if (this.aberto) return;

  						var tmp = ev.relatedTarget;
						while (tmp && tmp != this.submenu) {
							tmp = tmp.parentNode;
						}
						if (!tmp || tmp != this.submenu)
							menu(this);

						ev.stopPropagation();
					},
					true);
				menus[i].addEventListener("mouseout",
					function (ev) {
						if (!this.aberto) return;

						var tmp = ev.relatedTarget;
						while (tmp && tmp != this && tmp != this.parentNode) {
							tmp = tmp.parentNode;
						}
						if (!tmp || tmp != this) {
							menu(this);

							if (tmp != this.parentNode) {
								tmp = this.parentNode.parentNode;
								while (tmp.submenu) {
									menu(tmp);
									tmp = tmp.parentNode.parentNode;
								}
							}
						}
						ev.stopPropagation();
					},
					false);
			}
		}
	}
}



