var spacing = 1;

function submenu(id, x, y, pos, isPopup, parent_sub, position, appendFlag)
{
	oTable = document.createElement("table");
	oTable.id = id;
	oTable.style.position = position;
	oTable.style.left = x;
	oTable.style.top = y;
	oTable.cellPadding = 0;
	oTable.cellSpacing = spacing;
	oTable.isPopup = isPopup;
	oTable.pos = pos;
	oTable.parent_sub = parent_sub;
	oTable.timer_id = null;
	oTable.className = 'menu_table';
	
	if(isPopup == 1)
		oTable.style.display = "none";

	oTable.add_button = function(id, text, image, isPopup, action)
	{
		if(this.pos == 0)
		{
			if(this.rows.length == 0)
				this.insertRow(0);
			
			var cell = this.rows[0].insertCell(this.rows[0].cells.length)
			button(cell, id, text, image, isPopup, action);
		}
		else
		{
			var row = this.insertRow(this.rows.length);
			var cell = row.insertCell(0);
			button(cell, id, text, image, isPopup, action);
		}
	}
	
	if(oTable.isPopup)
	{
		oTable.onmouseover = function()
		{
			this.show();
			var parent = this.parent_sub;
			while(parent)
			{
				var obj = document.getElementById(parent);
				obj.show();
				parent = obj.parent_sub;
			}
		}
		
		oTable.onmouseout = function()
		{
			this.hide();
			var parent = this.parent_sub;
			while(parent)
			{
				var obj = document.getElementById(parent);
				obj.hide();
				parent = obj.parent_sub;
			}
		}
	}
	oTable.show = function()
	{
		if(this.isPopup)
		{
			window.clearTimeout(this.timer_id);
			if(this.style.display == "none")
				this.timer_id = window.setTimeout("show('"+this.id+"')",50);
		}
	}
	
	oTable.hide = function()
	{
		if(this.isPopup)
		{
			window.clearTimeout(this.timer_id);
			if(this.style.display != "none")
			{
				this.timer_id = window.setTimeout("hide('"+this.id+"')",50);
			}
		}
	}
	if( appendFlag )
		document.body.appendChild(oTable);
	
	return oTable;
}

function button(obj, id, text, image, isPopup, action)
{
	obj.id = id;
	obj.isPopup = isPopup;
	obj.action = action;
	obj.className = 'menu_button';

	var oLink = document.createElement("a");
	oLink.className = "menu_link";
	if(isPopup == 0)
		oLink.href = "#"; //action;
	else
		oLink.href = "#";  
	
	var oTable = document.createElement("table");
	var cells = new Array();
	
	oTable.cellPadding = 0;
	oTable.cellSpacing = 0;
	oTable.border = 0;
		
	oTable.insertRow(0);
	cells[0] = oTable.rows[0].insertCell(0);
	cells[1] = oTable.rows[0].insertCell(1);
	cells[2] = oTable.rows[0].insertCell(2);
	
	if(image)
		cells[0].innerHTML = "<img src='"+image+"' width='20' height='20' align='left' border='0'>";
	else
		cells[0].innerHTML = "<img src='images/menu/empty.gif' width='20' height='1' align='left' border='0'>";
	
	cells[1].innerHTML = text;
	cells[1].width ="100%";
	
	if(isPopup)
		cells[2].innerHTML = "<img src='images/menu/arrow.gif' width='6' height='20' align='right' border='0'>";
	else
		cells[2].innerHTML = "<img src='images/menu/empty.gif' width='6' height='20' align='right' border='0'>";

	cells[0].className = "menu_td";
	cells[1].className = "menu_text";
	cells[2].className = "menu_td";
	
	if(!isPopup)
		oTable.onclick = function()
		{
			document.location = action;
		}

	oTable.style.cursor = "hand";

	oLink.onclick = function(){
//		document.location.href = action;
		return false;
	}
	
	oLink.appendChild(oTable);
	obj.appendChild(oLink); 
	
	obj.onmouseover = function()
	{
		this.className = 'menu_td_over';
		if(this.isPopup)
		{
			var parent = this.offsetParent;
			var sub = document.getElementById(this.action);
			
			var x = this.offsetLeft + parseInt(parent.style.left, 10);
			if(parent.pos == 1) 
				x += this.offsetWidth;
			else
				x -= spacing;
			var y = this.offsetTop + parseInt(parent.style.top, 10);
			if(parent.pos == 0)
				y += this.offsetHeight;
			else
				y -= spacing;
			
			if( sub != null && sub != 'null' && sub != 'undefined') {
				sub.style.left = x+'px';
				sub.style.top = y+'px';
			
				sub.show();
			}
		}
	}
	
	obj.onmouseout = function()
	{
		this.className = 'menu_td_out';
		if(this.isPopup)
		{
			var sub = document.getElementById(this.action);
			if( sub != null && sub != 'null' && sub != 'undefined')
				sub.hide();
		}
	}
}

function show(obj)
{
	sub = document.getElementById(obj);
	sub.style.display = "block";
}

function hide(obj)
{
	sub = document.getElementById(obj);
	sub.style.display = "none";
}
