// SimpleMenu cascading menu system
// verified to work under Windows in IE 6.0, mozilla 1.2.1, and Opera 7.02

// use this to create a new menu.
// you can set width, height, or horizontal, if you want.
function Menu()
{
	this.width = null;
	this.height = null;
	this.horizontal = false;
	this.spacing = 0;
	this.padding = 0;
	this.positionX = 0;
	this.positionY = 0;

	this.addMenu = _Menu_addMenu;
	this.addItem = _Menu_addItem;
	this.addSeparator = _Menu_addSeparator;
	this.write = _Menu_write;
	this.hideAll = _Menu_hideAll;

	this.contents = new Array();
	this.labels = new Array();
	this.normalImages = new Array();
	this.highlightImages = new Array();
	this.id = 'menu' + Menu.unique++;
	this.parent = null;
}

// add a submenu to an existing menu.
// normalImage and highlightImage are optional.
function _Menu_addMenu(label, normalImage, highlightImage)
{
	var menu = new Menu();
	menu.parent = this;
	this.contents[this.contents.length] = menu;
	this.labels[this.labels.length] = label;
	this.normalImages[this.normalImages.length] = normalImage;
	this.highlightImages[this.highlightImages.length] = highlightImage;
	return menu;
}

// add an item (link) to an existing menu.
// normalImage and highlightImage are optional.
function _Menu_addItem(label, href, normalImage, highlightImage)
{
	var item = new MenuItem(href);
	item.parent = this;
	this.contents[this.contents.length] = item;
	this.labels[this.labels.length] = label;
	this.normalImages[this.normalImages.length] = normalImage;
	this.highlightImages[this.highlightImages.length] = highlightImage;
}

// add a separator to an existing menu.
// label is optional, and defaults to '<hr class="menu_separator">'.
function _Menu_addSeparator(label)
{
	this.contents[this.contents.length] = new MenuSeparator();
	if (!label) label = '<hr class="menu_separator">';
	this.labels[this.labels.length] = label;
	this.normalImages[this.normalImages.length] = null;
	this.highlightImages[this.highlightImages.length] = null;
}

// call this on the top-level menu to display the menu on the page.
// it also preloads any mouseover images.
function _Menu_write(hide)
{
	var subMenus = new Array();

	var str = '<div id="' + this.id + '"';
	if (hide) str += ' style="display: none"';
	str += '><table border=0 class="menu"';
	str += ' cellspacing="' + this.spacing + '"';
	str += ' cellpadding="' + this.padding + '"';
	if (this.width) str += ' width="' + this.width + '"';
	if (this.height) str += ' height="' + this.height + '"';
	str += '><tr>';
	for (var i = 0; i < this.contents.length; ++i)
	{
		if (i && !this.horizontal) str += '</tr><tr>';

		if (this.contents[i].constructor == Menu)
			subMenus[subMenus.length] = this.contents[i];

		var id = this.contents[i].id;
		Menu.all[id] = this.contents[i];

		str += '<td class="menuitem_normal" onclick="Menu.clicked(this, \'' + id + '\')"';
		str += ' onmouseover="Menu.over(this, \'' + id + '\')" onmouseout="Menu.out(this, \'' + id + '\')">';
		if (this.normalImages[i] && this.normalImages[i] != '')
		{
			var imgid = id + 'img';
			var imgInfo = new Array(this.normalImages[i], this.highlightImages[i]);
			Menu.images[imgid] = imgInfo;
			str += '<img vspace=0 hspace=0 id="' + imgid + '" src="' + this.normalImages[i] + '">';

			if (this.highlightImages[i] && this.highlightImages[i].length)
			{
				var preload = new Image();
				preload.src = this.highlightImages[i];
			}
		}
		if (this.labels[i] && this.labels[i].length) str += ' ' + this.labels[i];
		str += '</td>';
	}

	str += '</tr></table></div>';
	document.write(str);

	for (var i = 0; i < subMenus.length; ++i)
		subMenus[i].write(true);
}

function _Menu_hideAll()
{
	for (var i = 0; i < this.contents.length; ++i)
	{
		var menu = this.contents[i];
		if (!menu || menu.constructor != Menu) continue;
		menu.hideAll();
		var domElem = document.getElementById(menu.id);
		if (domElem) domElem.style.display = 'none';
	}
}

Menu.unique = 1042;
Menu.all = new Object();
Menu.hideTimer = null;
Menu.active = new Array();
Menu.images = new Array();

Menu.clicked = _Menu_clicked;
Menu.over = _Menu_over;
Menu.out = _Menu_out;
Menu.hideInactive = _Menu_hideInactive;
Menu.isActive = _Menu_isActive;
Menu.getPosition = _Menu_getPosition;
Menu.setPosition = _Menu_setPosition;

function _Menu_clicked(what, id)
{
	var item = Menu.all[id];
	if (!item) return;

	if (item.constructor == MenuItem && item.href && item.href.length)
	{
		window.clearTimeout(Menu.hideTimer);
		window.location.href = item.href;
	}
}

function _Menu_over(what, id)
{
	var menu = Menu.all[id];
	if (!menu) return;

	Menu.active = new Array();
	var walkingMenu = menu;
	do
	{
		Menu.active[Menu.active.length] = walkingMenu;
		walkingMenu = walkingMenu.parent;
	}
	while (walkingMenu);

	what.className = 'menuitem_highlight';

	var img = document.getElementById(id + 'img');
	if (img)
	{
		var imgInfo = Menu.images[id + 'img'];
		if (imgInfo && imgInfo[1] && imgInfo[1].length) img.src = imgInfo[1];
	}

	if (menu.constructor == MenuItem)
	{
		menu.parent.hideAll();
		return;
	}

	var domElem = document.getElementById(menu.id);
	if (!domElem) return;

	var pos = Menu.getPosition(what);
	if (menu.parent.horizontal)
	{
		pos.top += what.offsetHeight + menu.positionY;
		pos.left += menu.positionX;
	}
	else
	{
		pos.top += menu.positionY;
		pos.left += what.offsetWidth + menu.positionX;
	}

	Menu.setPosition(domElem, pos);
	domElem.style.display = 'block';
}

function _Menu_out(what, id)
{
	what.className = 'menuitem_normal';

	var img = document.getElementById(id + 'img');
	if (img)
	{
		var imgInfo = Menu.images[id + 'img'];
		if (imgInfo && imgInfo[0] && imgInfo[0].length) img.src = imgInfo[0];
	}

	Menu.active = new Array();
	Menu.hideTimer = window.setTimeout('Menu.hideInactive()', 200);
}

function _Menu_getPosition(element)
{
	var obj = element;
	var ret = new Object();
	ret.left = 0;
	ret.top = 0;

	do
	{
		ret.left += obj.offsetLeft;
		ret.top += obj.offsetTop;
		obj = obj.offsetParent;
	}
	while (obj != document.body);

	return ret;
}

function _Menu_setPosition(element, pos)
{
	element.style.position = 'absolute';
	element.style.top = pos.top;
	element.style.left = pos.left;
}

function _Menu_hideInactive()
{
	for (var k in Menu.all)
	{
		var menu = Menu.all[k];
		if (Menu.isActive(menu)) continue;

		var domElem = document.getElementById(menu.id);
		if (domElem) domElem.style.display = 'none';
	}
}

function _Menu_isActive(menu)
{
	for (var i = 0; i < Menu.active.length; ++i)
	{
		if (Menu.active[i] == menu) return true;
	}

	return false;
}

function MenuItem(href)
{
	this.href = href;
	this.parent = null;
	this.id = 'menuitem' + Menu.unique++;
}

function MenuSeparator()
{
}
