var menus = new Array();
var SelColF = 'white';
var SelColB = 'black';
var CurColF = 'black';
var CurColFL = 'black';
var BodyLeftMargin = 10;

function MenuRegister(item)
{ menus[menus.length] = item;
	return (menus.length - 1);
}

function MenuItem(caption, command, image, submenu, separator) 
{ if (MenuItem.arguments.length > 5)
		this.target = MenuItem.arguments[5];
	else
		this.target = "_top";
	this.caption = caption;
	this.command = command;
	this.image = image;
	this.submenu = submenu;
	this.separator = (separator) ? true : false;
	this.id = MenuRegister(this);
}

function MenuItemOnClick(obj)
{ var item = menus[obj.menuid];
	window.event.cancelBubble = true;
	if (item == null)
		return;
	if ((typeof item.command) == 'function')
		item.command();
}

function MenuItemOnMouseOver(obj)
{ var item = menus[obj.menuid];
	var parent = menus[item.parent];
	var menub1 = document.all['MENU' + item.parent + 'B1'];
	var fromElement = window.event.fromElement;
	var toElement = window.event.toElement;
	window.event.cancelBubble = true;
	if ((fromElement != null) && (toElement != null))
	{ if (fromElement.menuid == toElement.menuid)
			return;
	}
	obj.style.backgroundColor = SelColB;
	CurColFL = obj.style.color;
	obj.style.color = SelColF;
	// If a submenu is open that is not for this menu item, close it
	if ((parent.submenu != null) && (parent.submenu != item.submenu))
	{	parent.submenu.hide();
		parent.submenu = null;
	}
	if (!(item.submenu != null || (typeof item.command) == 'function'))
		window.status = item.command;
	// If this item has a submenu, open it
	if ((item.submenu != null) && (parent.submenu != item.submenu))
	{
//		item.submenu.top = obj.offsetTop + MenuBar.offsetHeight;
		item.submenu.top = obj.offsetTop + parent.top;
		item.submenu.left = menub1.offsetLeft + obj.offsetWidth - BodyLeftMargin;
		item.submenu.show();
		parent.submenu = item.submenu;
		return;
	}
}

function MenuItemOnMouseOut(obj)
{ var item = menus[obj.menuid];
	var parent = menus[item.parent];
	var toElement = window.event.toElement;
	window.event.cancelBubble = true;
	if ((toElement != null) && (toElement.menuid == parent.id))
	{ if ((parent.submenu != null) && (parent.submenu != item))
		{ parent.submenu.hide();
			parent.submenu = null;
		}
	}
	if ((window.event.fromElement != null) && (window.event.toElement != null))
	{ if (window.event.fromElement.menuid == window.event.toElement.menuid)
			return;
	}
	obj.style.backgroundColor = "transparent";
	obj.style.color = CurColFL;
	if (!(item.submenu != null || (typeof item.command) == 'function'))
		window.status = '';
}

function MenuItemToString()
{ var str = '';
	if (this.separator)
		str = "<tr style='height:5px'><td colspan=3><hr></td></tr>\n";
	else
	{ str =   "<tr class=menuRow" +
				" onMouseOver='MenuItemOnMouseOver(this)'" +
				" onMouseOut='MenuItemOnMouseOut(this)'";
		if (this.submenu != null || (typeof this.command) == 'function')
			str += " onClick='MenuItemOnClick(this)'";
		str += " menuid=" + this.id + ">";
		if (!(this.submenu != null || (typeof this.command) == 'function'))
			str += "<a href=" + this.command + " target=" + this.target + ">";
		str += "<td class=menuImageCell noWrap=noWrap menuid=" + this.id + ">" +
				((this.image != null) ? "&nbsp;&nbsp;<img class=menuImage menuid=" + this.id + " src='" + this.image + "'>&nbsp;&nbsp;" : "&nbsp;&nbsp;" ) + "</td>" +
				"<td class=menuCaptionCell noWrap=noWrap menuid=" + this.id + ">" + this.caption + "</td>" +
				"<td class=menuArrowCell noWrap=noWrap menuid=" + this.id + " " +
				((this.submenu != null) ? "style='font-family:Webdings'>4" : "style='font-family:times'>&nbsp;&nbsp;&nbsp;") + "</td>";
		if (!(this.submenu != null || (typeof this.command) == 'function'))
			str += "</a>";
		str += "</tr>\n";
	}
	return str;
}

MenuItem.prototype.toString = MenuItemToString;

function Menu(top, left)
{ this.items = new Array();
	this.top = top;
	this.left = left;
	this.id = MenuRegister(this);
	this.update = true;
//	document.write (this.borders());
	MENUINSERT.insertAdjacentHTML('BeforeEnd', this.borders());
}

function MenuAddItem(item)
{ this.items[this.items.length] = item;
	item.parent = this.id;
}

function MenuShow(noDisplay)
{ var menub1 = document.all['MENU' + this.id + 'B1'];
	if (this.update)
	{ menub1.innerHTML = this.getTable();
		this.update = false;
	}
	var menub1 = document.all['MENU' + this.id + 'B1'];
	var menu = document.all['MENU' + this.id];
	menub1.style.top = MenuBar.offsetTop + this.top;
	menub1.style.left = this.left + BodyLeftMargin;
	menub1.style.width = menu.offsetWidth;
	menub1.style.height = menu.offsetHeight;

	if (menub1.offsetTop < 0)
		menub1.style.top = 0;
	if (menub1.offsetLeft < 0)
		menub1.style.left = 0;
	if (noDisplay)
	{ menub1.style.top = -1000;
		menub1.style.left = -1000;
	}
	else
	{
		menub1.style.visibility = '';
	}
}

function MenuHide()
{ var menub1 = document.all['MENU' + this.id + 'B1'];
	if (this.submenu != null)
		this.submenu.hide();
	menub1.style.visibility = 'hidden';
	menub1.style.top = -1000;
	menub1.style.left = -1000;
}

function MenuBorders()
{ var str = "";
	str =   "<div id=MENU" + this.id +"B1 style='position: absolute' class=menuBorder1 menuid=" + this.id + 
			" onClick='window.event.cancelBubble = true'>\n" +
			"</div>\n";
	return (str);
}

function MenuTable()
{ var str;
	str = "<table id=MENU" + this.id +
		" cellpadding=0 cellspacing=0 border=0 class=menuTable>";
	for (var i=0; i < this.items.length; i++)
		str += this.items[i];
	str += "</table>";
	return str;
}

Menu.prototype.addItem = MenuAddItem;
Menu.prototype.borders = MenuBorders;
Menu.prototype.getTable = MenuTable;
Menu.prototype.show = MenuShow;
Menu.prototype.hide = MenuHide;