//init menu
var m_List, i_List;
function InitMenu()
{
	m_List = new Array();
	i_List = new Array();
}
function GetIndexById(src, id)
{
	var i;
	for (i=0; i < src.length; i++)
	{
		if (src[i].id == id)
			return i;
	}
	return -1;
}
function AllowHide(index)
{
	m_List[index].canHide = true;
	while (m_List[index].parentIndex >= 0)
	{
		index = m_List[index].parentIndex;
		m_List[index].canHide = true;
	}
}
function DisableHide(index)
{
	m_List[index].canHide = false;
	while (m_List[index].parentIndex >= 0)
	{
		index = m_List[index].parentIndex;
		m_List[index].canHide = false;
	}
}
function RegisterMenu(id, parentIndex)
{
	var obj, index;
	index = m_List.length;
	obj = new Object();
	obj.id = id;
	obj.canHide = true;
	obj.index = index; // this index in m_List
	obj.parentIndex = parentIndex; // index of parent menu in m_List
	m_List[index] = obj;
	var tmp = document.getElementById(id);
	if (tmp)
	{
		eval("tmp.onmouseover = function() { DisableHide(" + index + "); };");
		eval("tmp.onmouseout = function() { AllowHide(" + index + "); ProcessMenu(" + index + ", 'hide');};");
	}
	return index;
}
function GetObjectWidth(id)
{
	var obj = document.getElementById(id);
	if (obj)
		return obj.clientWidth;
	else
		return 0;
}
function CalculatePosition(itemIndex, submenuId)
{
	var srcObj = i_List[itemIndex];
	var targetObj = document.getElementById(submenuId);
	if (srcObj && targetObj)
	{
		if (srcObj.menuDirection == 0)
			targetObj.style.left = calcPositionLeft(srcObj.id) + "px";
		else
		{
			targetObj.style.left = (calcPositionLeft(srcObj.container) + GetObjectWidth(srcObj.container) + 2) + "px";
			targetObj.style.top = calcPositionTop(srcObj.id) + "px";
		}
	}
}
function CalculatePosition1(submenuIndex)
{
	if (m_List[submenuIndex].parentIndex == -1)
		return;
	var srcObj = m_List[m_List[submenuIndex].parentIndex];
	var targetObj = document.getElementById(m_List[submenuIndex].id);
	if (srcObj && targetObj)
	{
		if (m_List[submenuIndex].menuDirection == 1)
		{
			targetObj.style.left = (calcPositionLeft("MenuContainer") + GetObjectWidth(srcObj.id) + 2) + "px";
//			targetObj.style.top = calcPositionTop(srcObj.id) + "px";
		}
	}
}
function RegisterItem(id, container, submenu, direction)
{
	var obj, index;
	var s_index;
	s_index = RegisterMenu(submenu, GetIndexById(m_List, container));
	index = i_List.length;
	
	obj = new Object();
	obj.id = id;
	obj.container = container;
	obj.submenu = submenu; // submenu id
	obj.submenuIndex = s_index;// submenu index
	obj.index = index; // this index in i_List
	obj.menuDirection = direction;
	i_List[index] = obj;

	CalculatePosition(index, submenu);

	var tmp = document.getElementById(id);
	if (tmp)
	{
		eval("tmp.onmouseover = function() { ProcessMenu(" + s_index + ", 'show'); };");
		eval("tmp.onmouseout = function() { ProcessMenu(" + s_index + ", 'hide'); };");
	}
}
function HideAll()
{
	var i;
	for (i=0; i < m_List.length; i++)
	{
		if (m_List[i].canHide)
		{
			HideMenu(i);
		}//else
//			alert("Skipped="+m_List[i].id);
	}
}
function ProcessMenu(index, command)
{
	switch (command)
	{
		case "show":
			m_List[index].canHide = false;
			setTimeout("ShowMenu("+ index +");", 30);
			break;
		case "hide":
			if (m_List[index].parentIndex >= 0)
			{
				m_List[m_List[index].parentIndex].canHide = true;
				m_List[index].canHide = true;
				setTimeout("HideMenu("+ m_List[index].parentIndex +");HideMenu("+ index +");", 350);
			}else
			{
				m_List[index].canHide = true;
				setTimeout("HideMenu("+ index +");", 350);
			}
			break;
	};
}

function ShowMenu(index)
{
	var obj = document.getElementById(m_List[index].id);
	if (obj)
	{
		CalculatePosition1(index);
		obj.style.visibility = "visible";
	}
}
function HideMenu(index)
{
	var obj = document.getElementById(m_List[index].id);
	if (obj && m_List[index].canHide)
	{
		obj.style.visibility = "hidden";
	}
}

var isDOM=document.getElementById; //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
var isMozilla=isDOM && (navigator.userAgent.indexOf('Gecko') != -1) && (navigator.userAgent.indexOf('etscape') == -1);
var isNetscape6=!isMozilla && isDOM && (navigator.userAgent.indexOf('Gecko') != -1);

function calcPositionTop(id)
{
	var obj = document.getElementById(id);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function calcPositionLeft(id)
{
	var obj = document.getElementById(id);
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
//-->
