var hideMenuTimer = null;
var lap = 2500;
var openMenus = null;
var overBg = "#CCCCCC";
var outBg = "#EFEFEF";
var maxX = 1000;
var minBW = 130;
var zIndex = 500;


function menuStartTimeout(hideTimeout) {
	hideMenuTimer = setTimeout("menuHideMenus()", hideTimeout);	
}

function menuResetTimeout() {
	if (hideMenuTimer) clearTimeout(hideMenuTimer);
	hideMenuTimer = null;
}

function menuHideMenus() {
	menuResetTimeout();
	if( openMenus ) {
		for(var i in openMenus) {
			var m = document.getElementById(openMenus[i]);
			m.style.display = "none";			
		}
		openMenus = null;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	//alert('x,y :'+curleft+','+curtop);
	return [curleft,curtop];
}

function initMenu(id_parent, id_menu, posicion, botonW, botonH) {
	menuHideMenus();
	showMenu(id_parent, id_menu, posicion, botonW, botonH) ;
	menuStartTimeout(lap);
}

function showMenu(id_parent, id_menu, posicion, botonW, botonH) {
	menuResetTimeout();
	var parent = document.getElementById(id_parent);
	var pos = findPos(parent);
	var mMenu = document.getElementById(id_menu);
	var x = 0;
	var y = 0;
	mMenu.style.position = 'absolute';
	if(posicion) {
		switch(posicion) {
			case 'HORIZONTAL':
			x = pos[0];
			y = pos[1] + botonH;
			if(botonW < minBW) {
			    botonW = minBW;
			}
			setWidth(id_menu,botonW);
			mMenu.style.width = getWidth(id_menu) + 'px';
			mMenu.style.left = x + 'px';
			mMenu.style.top = y + 'px';
			break;
			case 'VERTICAL':
			x = pos[0] + botonW;
			y = pos[1] ;
			mMenu.style.width = getWidth(id_menu) + 'px';
			mMenu.style.left = x + 'px';
			mMenu.style.top = y + 'px';			
			break;			
		}
	} else {
		//alert("SIZE "+getWidth(id_parent));
		x = pos[0] + getWidth(id_parent);
		y = pos[1] ;
				//alert("X y Y: "+x+" - "+y);
		if(x > maxX){
			x = pos[0] - getWidth(id_menu);
		}			
		mMenu.style.width = getWidth(id_menu) + 'px';
		mMenu.style.left = x + 'px';
		mMenu.style.top = y + 'px';		
	}
	mMenu.style.display =  'block';
	mMenu.style.zIndex = zIndex++;
	if(openMenus) {
		openMenus[openMenus.length] = ""+id_menu;
	}else{
		openMenus = new Array();
		openMenus[0] = ""+id_menu;
	}

}

function overMenuItem(obj,child) {
	var men = document.getElementById(obj);
	hideSubmenus(obj);
	menuResetTimeout();
	men.style.backgroundColor = overBg;
	if(child) {
		var chl = document.getElementById(child);
		showMenu(obj, child);
	}
}

function outMenuItem(obj){
	var men = document.getElementById(obj);
	men.style.backgroundColor = outBg;
	menuStartTimeout(lap);
}

function getWidth(id) {
	var arr = id.split("_");
	return eval(""+arr[0]+"W");
}

function setWidth(id,valor) {
	var arr = id.split("_");
	aux = eval(""+arr[0]+"W = "+valor+"");	
	//aux = valor;
	//alert("AUX -> "+aux);
}

function getMenuName(id) {
	var arr = id.split("_");
	return arr[0];
}

function hideSubmenus(menuName) {
	menuName = getMenuName(menuName);
	if( openMenus ) {
		var h = false;
		var c = 0;
		for(var i in openMenus) {
			if( h ) {
				var m = document.getElementById(openMenus[i]);
				m.style.display = "none";
			} else if( openMenus[i] == menuName ) {
			//alert("OK");
				h = true;
			} else {
				c++;
			}
		}
		openMenus.length = c+1;
	}
}