var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;
var menu_path = new Array();

function setClass(obj, cl){
	if (obj.className!=cl) obj.className = cl;
}

function MenuLink(textVal, linkVal, subVal){
	this.text = textVal;
	this.action = linkVal;
	this.submenu = subVal;

	if (subVal)
	{
		subVal.parentLink = this;
	}
}

function menuHideAll(){
	for (var c = 0; c < mSheets.length; c++) mSheets[c].hide();
}

function menuHideTimerSet(){
	timer = window.setTimeout(menuHideAll, 300);
}

function menuHideTimerReset(){
	if (timer) window.clearTimeout(timer);
}

function menuAddLink(textVal, linkVal){
	this.links[this.links.length] = new MenuLink(textVal, linkVal, null);
}

function menuAddSubmenu(textVal, linkVal){
	this.links[this.links.length] = new MenuLink(textVal, linkVal, new MenuSheet(this));
}

function menuShow(leftVal, topVal, obj, cssClass)
{
	var theWidth = 0;

	if (document.documentElement && document.documentElement.clientWidth)
	{
		theWidth = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		theWidth = document.body.clientWidth;
	}
	else if (window.innerWidth)
	{
		theWidth = window.innerWidth;
	}

	this.block.style.visibility = "hidden";
	this.block.style.display = "block";

	if (0 < this.block.offsetWidth && leftVal + this.block.offsetWidth > theWidth)
	{
		if (this.parent)
			leftVal = leftVal - obj.offsetWidth - this.block.offsetWidth;
		else
			leftVal = leftVal + theWidth - (this.block.offsetWidth + leftVal)
	}

	this.block.style.left = leftVal + "px";
	this.block.style.top = topVal + "px";
	this.block.style.visibility = "visible";
	this.block.style.display = "block";

	if (!this.parent)
	{
		mm_highlight(this.parentLink.block, true);
	}
	else if('undefined' != typeof cssClass)
	{
		setClass(this.parentLink.block, cssClass);
	}

}

function menuHide()
{
	this.hideCh();
	this.block.style.display = "none";

	var cssClass = "blk-menu-sh blk-menu-arr";

	if (this.parentLink && this.parentLink.block)
	{
		if (!this.parent)
		{
			mm_highlight(this.parentLink.block, false);
		}
		else
		{
			setClass(this.parentLink.block, cssClass);
		}
	}
}

function menuFlip(leftVal, topVal){
	var disp = this.block.style.display;
	if (disp == "none") this.show(leftVal, topVal);
	else this.hide();
}

function menuHideCh(){
	for (var c = 0; c < this.links.length; c++){
		curLink = this.links[c];
		if (curLink.submenu) curLink.submenu.hide();
	}
}

function menuCreate()
{
	var res = "<div class=\"menu-sh\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"tab-menu-sh\">";
	var curLink;

	for (var c = 0; c < this.links.length; c++)
	{
		curLink = this.links[c];
		res += "<tr valign=\"center\"><td class=\"blk-menu-sh";
		if (curLink.submenu)
		{
			res += " blk-menu-arr";
			curLink.submenu.create();
		}
		res += "\" nowrap=\"nowrap\">" + curLink.text + "</td></tr>";
	}
	res += "</table></div>";
	this.block.innerHTML = res;

	this.block.onmouseout = menuHideTimerSet;
	this.block.onmouseover = menuHideTimerReset;

	var tds = this.block.getElementsByTagName('TD');
	for (var c = 0; c < this.links.length; c++)
	{
		this.links[c].block = tds[c];
		tds[c].menuLink = this.links[c];
		tds[c].menuSheet = this;
		tds[c].onmouseover = onMouseOverMenuLink;
		tds[c].onmouseout = onMouseOutMenuLink;
		tds[c].onclick = onClickMenuLink;
	}
}

function onMouseOverMenuLink(e)
{
	e = e || window.event;
	var el = e.target || e.srcElement;
	if (el)
	{
		el.menuSheet.hideCh();
		var cssClass = "blk-menu-sh-act"
		if (el.menuLink.submenu)
		{
			cssClass += " blk-menu-arr-act";
			el.menuLink.submenu.show(getLeftPos(el) + el.offsetWidth-3, getTopPos(el) - 1, el, cssClass);
		}
		else
		{
			setClass(el, cssClass);
		}
	}
}
function onMouseOutMenuLink(e)
{
	e = e || window.event;
	var el = e.target || e.srcElement;
	if (el)
	{
		if (!el.menuLink.submenu)
		{
			var cssClass = "blk-menu-sh";
			setClass(el, cssClass);
		}
	}
}

function onClickMenuLink(e)
{
	e = e || window.event;
	var el = e.target || e.srcElement;
	if (el)
	{
		mReady=false;
		gotoURL(el.menuLink.action, el.menuLink.target, window['onMenuClick']);
	}
}

function MenuSheet(parentObj)
{
	this.links = new Array();
	this.addLink = menuAddLink;
	this.addSubmenu = menuAddSubmenu;
	this.create = menuCreate;
	this.show = menuShow;
	this.hide = menuHide;
	this.flip = menuFlip;
	this.hideCh = menuHideCh;
	this.id = lastMenuId;
	lastMenuId++;
	this.parent = parentObj;
	this.block = document.createElement("DIV");
	this.block.className = "blk-menu";
	this.block.style.position = "absolute";
	this.block.style.display = "none";
	this.block.style.zIndex = currZ;
	currZ++;
	this.block.id = "ms" + this.id;
	document.body.appendChild(this.block);
}

function showMenu(objVal, numVal)
{
	if (mReady)
	{
		menuHideAll();
		if ( numVal >= 0 && mSheets[numVal] )
		{
			mSheets[numVal].show(getLeftPos(objVal)+185, getTopPos(objVal)-4, objVal);
			menuHideTimerReset();
		}
	}
}

function hideMenu(objVal, numVal)
{
	if (mReady)
	{
		menuHideTimerSet();
	}
}