﻿var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;
var mLeftPos = 326;

function MenuLink(text, link, sub)
{
	this.text = text;
	this.action = link;
	this.submenu = sub;
}

function MenuSheet(parentObj)
{
	this.addLink = function(text, link)
	{
		this.links[this.links.length] = new MenuLink(text, link, null);
	}

	this.addSubmenu = function(text, link)
	{
		this.links[this.links.length] = new MenuLink(text, link, new MenuSheet(this));
	}

	this.create = function(path)
	{
		var res = "<div class=\"PopupMenuCont\" onmouseout=\"menuHideTimerSet()\" onmouseover=\"menuHideTimerReset()\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"PopupMenuTable\">";
		var newPath;
		if (path == null) path = "mSheets[" + this.id + "]";

		this.links.each(
			function(curLink)
			{
				res += "<tr><td class=\"PopupMenuBlock";
				if (curLink.submenu) res += " PopupMenuArrow";

				res += "\" onmouseover=\"setClass(this, 'PopupMenuAct";
				if (curLink.submenu) res += " PopupMenuActArrow";

				res += "'); ";
				res += path + ".hideCh()";

				if (curLink.submenu)
				{
					newPath = path + ".links[" + c + "].submenu";
					res += "; " + newPath + ".show(getLeftPos(this) + this.offsetWidth, getTopPos(this))";
					curLink.submenu.create(newPath);
				}

				res += "\" onmouseout=\"setClass(this, 'PopupMenuBlock";
				if (curLink.submenu) res += " PopupMenuArrow";

				res += "')\" onclick=\"gotoURL('"+curLink.action+"')\" nowrap=\"nowrap\">" + curLink.text + "</td></tr>";
			}
		);

		res += "</table></div>";
		this.block.innerHTML = res;
	}

	this.show = function(left, top)
	{
		if (this.links.length == 0) return;

		this.block.style.left = left + "px";
		this.block.style.top = top + "px";
		this.block.style.display = "block";

		if (this.underBlock != null)
		{
			this.underBlock.style.display = "";
			this.underBlock.style.left = this.block.style.left;
			this.underBlock.style.top = this.block.style.top;
			this.underBlock.style.width = this.block.offsetWidth + "px";
			this.underBlock.style.height = this.block.offsetHeight+ "px";
		}
	}

	this.hide = function()
	{
		this.hideCh();
		this.block.style.display = "none";
		if (this.underBlock != null) this.underBlock.style.display = "none";
	}

	this.flip = function(left, top)
	{
		var disp = this.block.style.display;

		if (disp == "none") this.show(left, top);
		else this.hide();
	}

	this.hideCh = function()
	{
		for (var c in this.links)
		{
			curLink = this.links[c];
			if (curLink.submenu) curLink.submenu.hide();
		}
	}

	this.id = lastMenuId;
	this.links = new Array();
	this.parent = parentObj; 

	this.block = document.createElement("DIV");
	this.block.className = "PopupMenu";
	this.block.style.position = "absolute";
	this.block.style.marginTop = "11px";
	this.block.style.display = "none";
	this.block.style.zIndex = currZ;
	document.body.appendChild(this.block);

	this.underBlock = null;

	if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent))
	{
		var ver = Number(navigator.appVersion.substr(22,3));

		if (ver < 7)
		{
			this.underBlock = document.createElement("IFRAME");
			this.underBlock.src = "about:blank";
			this.underBlock.style.position = "absolute";
			this.underBlock.style.marginTop = "11px";
			this.underBlock.style.display = "none";
			this.underBlock.style.zIndex = currZ - 1;
			this.underBlock.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
			document.body.appendChild(this.underBlock);
		}
	}

	currZ++;
	lastMenuId++;
}

function menuHideAll()
{
	mSheets.each(function(sheet) { sheet.hide(); });
}

function menuHideTimerSet()
{
	timer = window.setTimeout(menuHideAll, 100);
}

function menuHideTimerReset()
{
	if (timer) window.clearTimeout(timer);
}

function showMenu(th, cnt, sel)
{
	/*
	if (!sel) th.className = 'over';
	*/

	if (!mReady) return;

	menuHideAll();
	mSheets[cnt].show(getLeftPos(th), getTopPos(th) + 18);
	menuHideTimerReset();

	/*
	if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
		mSheets[cnt].show(getLeftPos(th) + 50, getTopPos(th) + 32);
	} else {
		mSheets[cnt].show(getLeftPos(th) + 50, getTopPos(th) + 32);
	}
	*/
}

function hideMenu(th, cnt, sel)
{
	/*
	if (!sel) th.className = '';
	*/

	if (mReady) menuHideTimerSet();
}

function showTab(name, curr, cnt)
{
	var i;

	for (i = 0; i < curr; i++)
	{
		document.getElementById(name+"_hdr_"+i).className = "tabs_inact hand";
		document.getElementById(name+"_cnt_"+i).style.display = "none";
		document.getElementById(name+"_img_"+i+"_act").style.display = "none";
		document.getElementById(name+"_img_"+i+"_inact").style.display = "block";
	}

	document.getElementById(name+"_hdr_"+curr).className = "tabs_act";
	document.getElementById(name+"_cnt_"+curr).style.display = "block";
	document.getElementById(name+"_img_"+i+"_act").style.display = "block";
	document.getElementById(name+"_img_"+i+"_inact").style.display = "none";

	for (i = curr+1; i < cnt; i++)
	{
		document.getElementById(name+"_hdr_"+i).className = "tabs_inact hand";
		document.getElementById(name+"_cnt_"+i).style.display = "none";
		document.getElementById(name+"_img_"+i+"_act").style.display = "none";
		document.getElementById(name+"_img_"+i+"_inact").style.display = "block";
	}
}
