/*****************************************************
* int posicaoX/Y (element obj)
* 	- determinação de posicionamento absoluto X/Y
******************************************************/
function posicaoX (obj) {
	var x;
	x = obj.offsetLeft;
	if (obj.offsetParent != null) x += posicaoX (obj.offsetParent);
	return x;
}
function posicaoY (obj) {
	var y;
	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;

	if (desvanecer = i > f) {
		max = i;
		min = f;
		soma = -fase;
	} else {
		max = f;
		min = i;
		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
	if (ie) obj.filters.alpha.opacity = obj.alfa;
	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 = '';
}

/*****************************************************
* menu (element menuitem, bool tem_submenu)
* 	- abertura/fecho de submenus com posicionamento
* 	- criação automática de frames para corrigir defeitos dos SELECT em IE
******************************************************/
function menu (menuitem, tem_submenu) {
	// menuitem = document.getElementById (id);
	submenu = menuitem.nextSibling;
	while (submenu.nodeName != 'DIV') {
		submenu = submenu.nextSibling;
	}

	// Criar frame para Internet Explorer, se não existir
	if (ie && menuitem.previousSibling.nodeName != 'IFRAME') {
		frame = document.createElement ('iframe');
		frame.className = submenu.className;
		submenu.parentNode.insertBefore (frame, submenu);
	}

	abrir = menuitem.aberto ? 0 : 1;

	if (abrir) {

		x = y = 0;
		if (!tem_submenu) {
			x = posicaoX (menuitem);
			y = posicaoY (menuitem) + menuitem.offsetHeight;
		} else {
			x = menuitem.offsetWidth;
			y = menuitem.offsetTop;
		}

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

		// IE...
		if (ie) {
			submenu.style.display = "block";
			frame.style.left = submenu.style.left;
			frame.style.top = submenu.style.top;
			frame.style.width = submenu.clientWidth + "px";
			frame.style.height = submenu.offsetHeight + "px";
			frame.style.display = "block";
// 			menuitem.firstChild.style.backgroundColor = "#6F4717";
// 			menuitem.firstChild.style.color = "white";
		}

		desvanece (submenu, 0, 100, 25, 15);
		menuitem.aberto = 1;
	} else {
		// IE...
		if (ie) {
			frame.style.display = "";
// 			menuitem.firstChild.style.backgroundColor = "transparent";
// 			menuitem.firstChild.style.color = "#6F4717";
		}

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