/*** MENU SCRIPT by Nikolai Botev ***/
/*** (requires ua1.0.js) ***/

/* Class PopupMenu */

var popupMenus = new Object();
popupMenus.idGenerator = 0;
popupMenus.activeMenu = null;
popupMenus.map = new Array();

function PopupMenu(name, contents, bgColor, hideDelay, style, parentMenu) {
	// Properties
	this.name = (name) ? name : ("__menu" + popupMenus.idGenerator++);
	this.divID = "menu" + this.name;
	this.bgColor = bgColor;
	this.css = "";
	this.html = "";
	this.contents = contents;
	this.div = null;
	this.hideDelay = hideDelay ? hideDelay : 50;
	this.hideTimer = null;
	this.style = style;
	this.parentMenu = parentMenu ? parentMenu : popupMenus;
	this.subMenus = new Array();
	this.activeMenu = null;
	// Methods
	this.addSubMenu = pm_AddSubMenu;
	this.prepare = pm_Prepare;
	this.writeCss = pm_WriteCss;
	this.writeHtml = pm_WriteDiv;
	this.WriteAll = pm_WriteAll;
	this.Show = pm_Show;
	this.ShowAt = pm_ShowAt;
	this.showInternal = pm_ShowInternal;
	this.Hide = pm_Hide;
	// Initialize
	popupMenus.map[this.name] = this;
	this.prepare();
}

/* PopupMenu Methods */

function pm_AddSubMenu(menu) {
	this.subMenus[this.subMenus.length] = menu;
	menu.parentMenu = this;
}

function pm_Prepare() {
	// Build the css
	this.css = "#" + this.divID + "{ position:absolute; z-index:100; top:0px; ";
	this.css += "background-color:" + this.bgColor + "; ";
	this.css += "visibility:" + (UA.DOM ? "hidden":"hide") + "; ";
	if (this.style) this.css += this.style;
	this.css += "}\n";
	// Build the div tag
	this.html = '<div id="' + this.divID + '"';
	if (UA.DOM) {
		this.html += ' onmouseover="ieMouseOver(\'' + this.name + '\');"';
		this.html += ' onmouseout="ieMouseOut(\'' + this.name + '\');"';
	}
	this.html += '>' + this.contents + "</div>\n";
}

function pm_WriteCss() {
	document.write("<style>\n" + this.css + "\n</style>");
}

function pm_WriteDiv() {
	document.write(this.html);
	if (UA.DOM) {
		this.div = getElemById(this.divID);
	} else {
		// Capture Netscape Layer events
		this.div = document.layers[this.divID];
		this.div.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.div.onMouseOver = nsMouseOver;
		this.div.onMouseOut = nsMouseOut;
	}
}

function pm_WriteAll(recurse) {
	this.writeCss();
	this.writeHtml();
	for (var i = 0; i < this.subMenus.length; i++) {
		this.subMenus[i].WriteAll();
	}
}

function pm_Show(xOffset, yOffset, position) {
	showMenuAt(this.name, this.divID + "Button", xOffset, yOffset, position);
}

function pm_ShowAt(Control, xOffset, yOffset, position) {
	showMenuAt(this.name, Control, xOffset, yOffset, position);
}

function pm_ShowInternal() {
	doShowMenu(this.name, -1, -1);
}

function pm_Hide(immediate) {
	if (immediate)
		doHideMenu(this.name);
	else
		hideMenu(this.name);
}

/* PopupMenu Helper Functions */

function nsMouseOver(e) {
	if (e.target.document)
	{
		var menu = popupMenus.map[String(e.target.id).substr(4)]; //1.01
		if (menu) menu.showInternal(); //1.01
	}
}

function nsMouseOut(e) {
	if (e.target.document)
	{
		var menu = popupMenus.map[String(e.target.id).substr(4)]; //1.01
		if (menu) { //1.10
			while (menu.parentMenu != popupMenus)
				menu = menu.parentMenu;
			menu.Hide();
		}
	}
}

function ieMouseOver(menuName) {
	var menu = popupMenus.map[menuName];
	if (menu.hideTimer || menu.parentMenu != popupMenus) menu.showInternal();
}

function ieMouseOut(menuName) {
	if (UA.ie4) {
		var dest = event.toElement;
		while (dest) {
			if (dest == popupMenus.map[menuName].div) return;
			dest = dest.parentElement;
		}
	}
	var menu = popupMenus.map[menuName];
	while (menu.parentMenu != popupMenus)
		menu = menu.parentMenu;
	menu.Hide();
}

function ieHE(elmID, x, y, w, h)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (!obj || !obj.offsetParent) continue;
		var ox = obj.offsetLeft;
		var oy = obj.offsetTop;
		var parent = obj;
		while (parent = parent.offsetParent)
		{
			ox += parent.offsetLeft;
			oy += parent.offsetTop;
		}
		if (x > (ox + obj.offsetWidth) || ox > (x + w)) continue;
		if (y > (oy + obj.offsetHeight) || oy > (y + h)) continue;
		obj.style.visibility = "hidden";
	}
}

function ieSE(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (obj && obj.offsetParent) obj.style.visibility = "";
	}
}

function doShowMenu(menuName, x, y) {
	var menu = popupMenus.map[menuName];
	if (menu.div == null) return; //1.02
	if (menu.parentMenu.activeMenu != null && menu.parentMenu.activeMenu.name != menuName)
		doHideMenu(menu.parentMenu.activeMenu.name);
	var p = menu;
	while (p != popupMenus) {
		clearTimeout(p.hideTimer);
		p.hideTimer = null;
		p = p.parentMenu;
	}
	menu.parentMenu.activeMenu = popupMenus.map[menuName];
	// Stop the menu hide timer, get a reference to the menu
	var div = menu.div;
	if (UA.ie4) { w = div.offsetWidth; h = div.offsetHeight; }
	if (UA.DOM) div = div.style;
	// Position and display the menu
	if (x != -1) div.left = x;
	if (y != -1) div.top = y;
	div.visibility = "visible";
	if (UA.ie4 && x != -1 && y != -1) {
		ieHE("SELECT", x, y, w, h);
		ieHE("OBJECT", x, y, w, h);
		//ieHE("IFRAME", x, y, w, h); // commented feb 21 2007 - to allow menu on chart iframes
	}
}

var mspRight = 1, mspBottom = 2;
function showMenuAt(menuName, control, xOffset, yOffset, position) {
	// Set default param values
	if (!xOffset) xOffset = 0; if (!yOffset) yOffset = 0; if (!position) position = 0;
	// Calculate the menu coordinates
	var x, y, btn;
	if (UA.DOM) {
		// Internet Explorer
		if (typeof(control) == 'string')
			btn = getElemById(control);
		else
			btn = control;
		x = btn.offsetLeft;
		y = btn.offsetTop;
		var p = btn;
		while (p = p.offsetParent) {
			x += p.offsetLeft;
			y += p.offsetTop;
		}
		if ((position & mspRight) != 0) x += btn.offsetWidth;
		if ((position & mspBottom) != 0) y += btn.offsetHeight;
	} else {
		// Netscape Navigator
		if (typeof(control) == 'string')
			btn = document.anchors[control];
		if (!btn) {
			btn = document.layers[control];
			x = btn.pageX;
			y = btn.pageY;
			if ((position & mspRight) != 0) x += btn.clip.width;
			if ((position & mspBottom) != 0) x += btn.clip.height;
		} else {
			x = btn.x;
			y = btn.y;
		}
	}
	// Show the menu
	doShowMenu(menuName, x + xOffset, y + yOffset);
}

function doHideMenu(menuName) {
	var menu = popupMenus.map[menuName];
	if (menu.div == null) return; //1.02
	if (UA.DOM) {
		menu.div.style.visibility = "hidden";
	} else {
		menu.div.visibility = "hide";
	}
	clearTimeout(menu.hideTimer);
	menu.hideTimer = null;
	if (menu.activeMenu != null) doHideMenu(menu.activeMenu.name);
	menu.parentMenu.activeMenu = null;
	if (UA.ie4) {
		ieSE("SELECT");
		ieSE("OBJECT");
		ieSE("IFRAME");
	}
}

function hideMenu(menuName) {
	popupMenus.map[menuName].hideTimer = setTimeout("doHideMenu('" + menuName + "')",
		popupMenus.map[menuName].hideDelay);
}
