var DHTMLMENU_MENUS = 1;
var DHTMLMENU_CHILDS = 1;
var DHTMLMENU_CURRENT = new Array();
var DHTMLMENU_TIMER = null;

function dhtmlMenu(d)
{
	this.menuid = DHTMLMENU_MENUS++;
	this.direction = d;
	this.nodes = new Array();

	this.paint = dhtmlMenu_paint;
	this.addNode = dhtmlMenu_addNode;
}

function dhtmlMenu_paint()
{
	var html = '';
	
	if (this.direction == 'V')
	{
		for (var i in this.nodes)
		{ html += this.nodes[i].paintBranch(this.menuid,0);
		  html += '<br>';
		}
	}
	else
	{
		for (var i in this.nodes)
		{ html += this.nodes[i].paintHBranch(this.menuid,0); }
	}
	for (var i in this.nodes)
	{ html += this.nodes[i].paintChilds(this.menuid,0); }

	document.write(html);
	
	DHTMLMENU_CURRENT[this.menuid] = new Array();
}

function dhtmlMenu_addNode(n)
{ this.nodes.push(n); }


/* -------------------------------------- */

function dhtmlMenuNode(t,h)
{
	this.id = DHTMLMENU_CHILDS++;
	this.title = t;
	this.active = false;
	this.selected = false;
	this.href = h;
	this.childs = new Array();
	
	
	this.paint = dhtmlMenuNode_paint;
	this.paintBranch = dhtmlMenuNode_paintBranch;
	this.paintHBranch = dhtmlMenuNode_paintHBranch;
	this.paintChilds = dhtmlMenuNode_paintChilds;
	this.addChild = dhtmlMenuNode_addChild;
}


function dhtmlMenuNode_paintBranch(menuid,parentid)
{
	var html = '';
	var clase='dhtmlMenuBranch';
	if(this.active){clase='dhtmlMenuBranch_active';}
	if(this.selected){clase='dhtmlMenuBranch_selected';}
	var onmouseout = '';
	var addtoover = '';
	if (this.childs.length == 0){addtoover= 'dhtmlMenu_initTimeout();';}
	else{onmouseout = ' onmouseout="dhtmlMenu_initTimeout();" ';}
	
	html += '<a class="'+clase+'" href="'+this.href+'" ';
	html += ' onmouseover="dhtmlMenu_showChilds('+menuid+','+parentid+','+this.id+',false,this);'+addtoover+'" ';
	html += onmouseout;
	html += '>'+this.title+'</a>';
	return html;
}

function dhtmlMenuNode_paintHBranch(menuid,parentid)
{
	var html = '';
	var clase='dhtmlMenuHBranch';
	if(this.active){clase='dhtmlMenuHBranch_active';}
	if(this.selected){clase='dhtmlMenuHBranch_selected';}
	var onmouseout = '';
	var addtoover = '';
	if (this.childs.length == 0){addtoover= 'dhtmlMenu_initTimeout();';}
	else{onmouseout = ' onmouseout="dhtmlMenu_initTimeout();" ';}
	
	html += '<a class="'+clase+'" href="'+this.href+'" ';
	html += ' onmouseover="dhtmlMenu_showChilds('+menuid+','+parentid+','+this.id+',true,this);'+addtoover+'" ';
	html += onmouseout;
	html += '>'+this.title+'</a>';
	return html;
}


function dhtmlMenuNode_addChild(n) { this.childs.push(n); }

function dhtmlMenuNode_paint(menuid,parentid)
{
	var html = '';
	var clase='dhtmlMenuNode';
	if(this.active){clase='dhtmlMenuNode_active';}
	if(this.selected){clase='dhtmlMenuNode_selected';}

	html += '<a class="'+clase+'" href="'+this.href+'" ';
	html += ' onmouseover="dhtmlMenu_showChilds('+menuid+','+parentid+','+this.id+',false,this);" ';
	html += '>'+this.title+'</a>';
	return html;
}

function dhtmlMenuNode_paintChilds(menuid)
{
	var html ='';
	if (this.childs.length > 0)
	{
		html += '<div id="dhtmlMenu'+this.id+'" class="dhtmlMenuGroup" ';
		html += ' onmouseover="dhtmlMenu_clearTimeout();" ';
		html += ' onmouseout="dhtmlMenu_initTimeout();" ';
		html += ' style="display:none; position:absolute; left:0px: top:0px;">';
		for (var i in this.childs)
		{ html += this.childs[i].paint(menuid,this.id); }
		html += '</div>';

		for (var i in this.childs)
		{ html += this.childs[i].paintChilds(menuid); }
	}
	return html;
}


function dhtmlMenu_showChilds(menuid,parentid,id,bottom,src)
{
	var obj = document.getElementById('dhtmlMenu' + id);
	if (obj != null)
	{
		if (src != null)
		{
			// primero cerramos todos los nodos que sean de otros menus...
			for (var otro in DHTMLMENU_CURRENT)
			{
				if (otro != null && otro != menuid)
				{
					var menu = DHTMLMENU_CURRENT[otro];
					if (menu != null)
					{
						while (menu.length > 0)
						{
							var n = menu.pop();
							if (n != null)
							{ dhtmlMenu_closeChild(n); }
						}
					}
				}
			}

			
			
			if (DHTMLMENU_CURRENT[menuid] != null)
			{
				var salir=false;
				while ((!salir) && (DHTMLMENU_CURRENT[menuid].length > 0))
				{
					// si el nodo que estamos abriendo no cuelga del ultimo nodo que tenemos abierto,
					// cerramos este y miramos si cuelga del anterior...
					var length = DHTMLMENU_CURRENT[menuid].length;
					var last = DHTMLMENU_CURRENT[menuid][length-1];
					
					if (last != null)
					{
						if (last != parentid)
						{
							dhtmlMenu_closeChild(last);
							DHTMLMENU_CURRENT[menuid].pop();
						}
						else
						{ salir=true; }
					}
					else
					{ var kk = DHTMLMENU_CURRENT[menuid].pop(); }
				}
			}
			
			// buscamos las coordenadas del nodo actual...
			var top = dhtmlMenu_getRealTop(src);
			var left = dhtmlMenu_getRealLeft(src);
			if (bottom)
			{
				var height = src.offsetHeight;
				top += height + 2;
			}
			else
			{
				var width = src.offsetWidth;
				left += width + 2;
			}
			
			obj.style.left = left + 'px';
			obj.style.top = top + 'px';
			obj.style.display = 'block';
			
			if (DHTMLMENU_CURRENT[menuid] == null)
			{ DHTMLMENU_CURRENT[menuid] = new Array(); }

			DHTMLMENU_CURRENT[menuid].push(id);
			
			dhtmlMenu_clearTimeout(DHTMLMENU_TIMER);
		}
	}
}



function dhtmlMenu_closeChild(id)
{
	var obj = document.getElementById('dhtmlMenu' + id);
	if (obj != null)
	{ obj.style.display='none'; }
}


function dhtmlMenu_closeAll()
{
	while (DHTMLMENU_CURRENT.length > 0)
	{
		var menu = DHTMLMENU_CURRENT.pop();
		if (menu != null)
		{
			while (menu.length > 0)
			{ var n = menu.pop();
			  if (n != null)
			  { dhtmlMenu_closeChild(n); }
			}
		}
	}
}


function dhtmlMenu_initTimeout()
{ DHTMLMENU_TIMER = setTimeout(dhtmlMenu_closeAll,2000); }

function dhtmlMenu_clearTimeout()
{
	if (DHTMLMENU_TIMER != null)
	{ clearTimeout(DHTMLMENU_TIMER); }
}


function dhtmlMenu_getRealLeft(imgElem)
{
	xPos = eval(imgElem).offsetLeft;
	tempEl = eval(imgElem).offsetParent;
  	while (tempEl != null)
	{
		if (tempEl.style.position == 'absolute')
		{
			var pixels = tempEl.style.left;
			pixels = pixels.replace('px','');
			xPos += parseInt(pixels);
			tempEl = null;
		}
		else
		{
			xPos += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		}
  	}
	return xPos;
}

function dhtmlMenu_getRealTop(imgElem)
{
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while (tempEl != null)
	{
		if (tempEl.style.position == 'absolute')
		{
			var pixels = tempEl.style.top;
			pixels = pixels.replace('px','');
			yPos += parseInt(pixels);
			tempEl = null;
		}
		else
		{
			yPos += tempEl.offsetTop;
			tempEl = tempEl.offsetParent;
		}
  	}
	return yPos;
}

