var contextMenuArbolTimeOut = null;
var lastMenu 				= null;
var menuChangeColor			= null;
var menuTdObj				= null;
var idImgMenu				= null;
var isOpeningMenu			= false;

var isClosingMenu			= false;
var waitIdObject			= null;
var waitParentObj			= null;
var waitPosX				= null;
var waitId					= null;
var waitIsInHome			= null;
var isInHomeVar				= false;

function mostrarMenu (idObject, parentObj, posX, id, isInHome) {
	if (isOpeningMenu) return;
	
	isInHomeVar = isInHome;
	
	if (lastMenu && lastMenu != idObject && isInHome) {
		waitIdObject 	= idObject;
		waitParentObj 	= parentObj;
		waitPosX 		= posX;
		waitId 			= id;
		waitIsInHome 	= isInHome;
		
		if (contextMenuArbolTimeOut)
			clearTimeout (contextMenuArbolTimeOut);
		
		if (isClosingMenu) return;
		
		closeMenu ();
		
		return;
	} else {
		isClosingMenu = false;
	}
	
	if (lastMenu == idObject)
		return;
	
	closeMenu ();
	clearTimeOutMenu ();
	
	menuTdObj		= parentObj;
	idImgMenu		= id;
	
	var object = document.getElementById (idObject); 
	
	if (!object) 
		return;
	
	x = moveXbySlicePos (0, isInHome? parentObj.parentNode : parentObj);
	y = moveYbySlicePos (0, parentObj);

	object.style.left 	= (x - 0 + posX) + "px";
	object.style.top 	= (y + 46) + "px";
	
	YAHOO.util.Dom.setStyle (idObject, 'opacity', 1.0);
	
	if (isInHome) {
		YAHOO.util.Dom.setStyle (idObject + "_img", 'display', "none");
		YAHOO.util.Dom.setStyle (idObject, 'overflow', "hidden");
		YAHOO.util.Dom.setStyle (idObject, 'height', "1px");
		
		var anim1 = new YAHOO.util.Anim(idObject, {height: {from: 1, to: 300}}, 0.8, YAHOO.util.Easing.easeBoth);
		anim1.onComplete.subscribe (function () {
			YAHOO.util.Dom.setStyle (idObject + "_img", 'opacity', 0.0);
			YAHOO.util.Dom.setStyle (idObject + "_img", 'display', "block");
			YAHOO.util.Dom.setStyle (idObject + "_div", 'opacity', 1.0);
			YAHOO.util.Dom.setStyle (idObject + "_td", 'opacity', 1.0);
			var anim2 = new YAHOO.util.Anim (idObject + "_img", {opacity: {from: 0, to: 1 }},0.2);
			anim2.onTween.subscribe (function () {
				YAHOO.util.Dom.setStyle (idObject + "_div", 'opacity', 1.0);
				YAHOO.util.Dom.setStyle (idObject + "_td", 'opacity', 1.0);
			});
			anim2.animate();
			initTimeOutMenu ();
			isOpeningMenu = false;
		});
		anim1.animate();
		isOpeningMenu = true;
	} else {
		initTimeOutMenu();
	}
	object.style.display = '';
	
	lastMenu = idObject;
}
		
function initTimeOutMenu () {
	if (contextMenuArbolTimeOut)
		clearTimeout (contextMenuArbolTimeOut);
		
	contextMenuArbolTimeOut = setTimeout ('closeMenu()', 2000);
}
		
function clearTimeOutMenu(){
	if (contextMenuArbolTimeOut)
		clearTimeout (contextMenuArbolTimeOut);
	contextMenuArbolTimeOut = null;
}

function closeMenu(){
	try{
		if (lastMenu) {
			if (isInHomeVar) {
				if (!isClosingMenu) {
					isClosingMenu = true;
					
					var anim1 = new YAHOO.util.Anim (lastMenu, {height: {from: 300, to: 1}}, 0.8, YAHOO.util.Easing.easeBoth);
					anim1.onComplete.subscribe (function () {
						document.getElementById(lastMenu).style.display = "none";
						
						mustClose = lastMenu != waitIdObject;
						
						lastMenu 		= null;
						menuChangeColor = null;
						menuTdObj		= null;
						idImgMenu		= null;
						isClosingMenu	= false;
						
						if (mustClose) {
							if (waitIdObject)
								mostrarMenu (waitIdObject, waitParentObj, waitPosX, waitId, waitIsInHome);
						}
					});
					anim1.animate();
				}
			} else {
				document.getElementById(lastMenu).style.display = "none";
				lastMenu 		= null;
				menuChangeColor = null;
				menuTdObj		= null;
				idImgMenu		= null;
				isClosingMenu	= false;
			}
		}
	}catch(exc){}
}

function moveXbySlicePos (x, img) { 
	if (!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.leftMargin && ! onWindows ) x += parseInt(par.leftMargin);
			if( (par.offsetLeft != lastOffset) && par.offsetLeft ) x += parseInt(par.offsetLeft);
			if( par.offsetLeft != 0 ) lastOffset = par.offsetLeft;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}
	} else if (img.x) x += img.x;
	return x;
}

function moveYbySlicePos (y, img) {
	if(!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.topMargin && !onWindows ) y += parseInt(par.topMargin);
			if( (par.offsetTop != lastOffset) && par.offsetTop ) y += parseInt(par.offsetTop);
			if( par.offsetTop != 0 ) lastOffset = par.offsetTop;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}		
	} else if (img.y >= 0) y += img.y;
	return y;
}
