// SimpleMenu cascading menu system
// Version 11

// Windows: IE 6.0, Mozilla 1.2.1, Opera 7.02
// Mac: Netscape 7.0, (not IE 5.2)

//!! make it so there's a choice between writing submenus on onload or with write()?

//!! can you get the position of a table relative to the page, and fudge the submenu position
// based on that in IE 5.2/Mac?

// use this to create a new menu.
// you can set width, height, or horizontal, if you want.

function Menu()
{
	this.width = null; // coerce the menu to a given pixel width
	this.height = null; // coerce the menu to a given pixel height
	this.horizontal = false; // lay the menu out horizontally?
	this.spacing = 0; // pixel spacing between menu labels
	this.padding = 0; // pixel padding around menu labels
	this.positionX = 0; // fudge submenus' horizontal offset position
	this.positionY = 0; // fudge submenus' vertical offset position
	this.nbsps = 1; // number of non-breaking spaces to put before and after menu labels

	this.addMenu = _Menu_addMenu;
	this.addItem = _Menu_addItem;
	this.addSeparator = _Menu_addSeparator;
	this.addBlob = _Menu_addBlob;
	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;
	

	if ( typeof( window[ 'menuitem_normal' ] ) == "undefined" ) {
   		this.menuitem_normal = "menuitem_normal";
   	}else{
		this.menuitem_normal = menuitem_normal;
	}
	if ( typeof( window[ 'menuitem_highlight' ] ) == "undefined" ) {
		this.menuitem_highlight = "menuitem_highlight";
   	}else{
		this.menuitem_highlight = menuitem_highlight;
	}
}

// add a submenu to an existing menu.
// normalImage and highlightImage are optional.
function _Menu_addMenu(label, href, normalImage, highlightImage)
{
	var menu = new Menu();
	menu.parent = this;
	menu.href = href;
	this.contents[this.contents.length] = menu;

	this.labels[this.labels.length] = label;
	this.normalImages[this.normalImages.length] = normalImage;
	this.highlightImages[this.highlightImages.length] = highlightImage;
	
	menu.menuitem_normal = this.menuitem_normal;
	menu.menuitem_highlight = this.menuitem_highlight;

	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;
	
	item.menuitem_normal = this.menuitem_normal;
	item.menuitem_highlight = this.menuitem_highlight;

	return item;
}

// 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;
}

// add a blob of html to an existing menu.
// you can use this to create special tips for your menus.
function _Menu_addBlob(html)
{
	var blob = new MenuBlob();
	blob.parent = this;
	this.contents[this.contents.length] = blob;

	this.labels[this.labels.length] = html;
	this.normalImages[this.normalImages.length] = null;
	this.highlightImages[this.highlightImages.length] = null;

	return blob;
}

// call this on the top-level menu to display the menu on the page.
// it also preloads any mouseover images.
function _Menu_write()
{
	var defer = false;
	if (_Menu_write.arguments.length && _Menu_write.arguments[0]) defer = true;

	var subMenus = new Array();

	var str = '';
	if (!defer) str += '<div id="' + this.id + '">';
	

	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';

		if (this.contents[i].constructor == MenuBlob) str += ' class="menu_blob"';
		else if (this.contents[i].constructor == MenuSeparator) str += ' class="menu_separator"';
		else str += ' class="' + this.menuitem_normal + '"'; //changed by nate for multiple styles
		
		

		if (this.contents[i].href && this.contents[i].href.length)
			str += ' style="cursor: ' + Menu.linkCursor + '"';
		else
			str += ' style="cursor: ' + Menu.normalCursor + '"';

		str += ' onclick="Menu.clicked(this, \'' + id + '\')"';
		str += ' onmouseover="Menu.over(this, \'' + id + '\')" onmouseout="Menu.out(this, \'' + id + '\')">';

		if (this.contents[i].constructor != MenuSeparator && this.contents[i].constructor != MenuBlob)
			for (var j = 0; j < this.nbsps; ++j) str += '&nbsp;';

		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 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];

		if (this.contents[i].constructor != MenuSeparator && this.contents[i].constructor != MenuBlob)
			for (var j = 0; j < this.nbsps; ++j) str += '&nbsp;';

		str += '</td>';
	}

	str += '</tr></table>';
	if (!defer)
	{
		str += '</div>';
		document.write(str);
	}
	else Menu.subMenus[this.id] = str;

	if (subMenus.length && !Menu.haveSetOnload)
	{
		Menu.haveSetOnload = true;
		Menu.oldBodyOnload = document.body.onload;
		Menu.oldWindowOnload = window.onload;
		document.body.onload = Menu.writeSubMenus;
		window.onload = Menu.writeSubMenus;
	}

	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';
	}
}

// --------------------------------------------------------------------------------------
// private implementation.  meddle at your own risk.

Menu.normalCursor = 'default';
Menu.linkCursor = 'pointer';
if (navigator.appVersion.indexOf('MSIE 5') != -1 && navigator.appVersion.indexOf('Windows') != -1)
	Menu.linkCursor = 'hand';

Menu.clicked = _Menu_clicked;
Menu.over = _Menu_over;
Menu.out = _Menu_out;
Menu.getPosition = _Menu_getPosition;
Menu.setPosition = _Menu_setPosition;
Menu.hideInactive = _Menu_hideInactive;
Menu.isActive = _Menu_isActive;
Menu.writeSubMenus = _Menu_writeSubMenus;

Menu.unique = 42;
Menu.all = new Object();
Menu.hideTimer = null;
Menu.active = new Array();
Menu.images = new Array();
Menu.subMenus = new Object();
Menu.haveSetOnload = false;
Menu.oldBodyOnload = null;
Menu.oldWindowOnload = null;

function _Menu_clicked(what, id)
{
	var item = Menu.all[id];
	if (!item) return;

	if (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);

	if (menu.constructor != MenuBlob && menu.constructor != MenuSeparator)
		what.className = menu.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.constructor == MenuBlob)
	{
		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)
{
	var menu = Menu.all[id];
	
	if (!menu) return;

	if (menu.constructor != MenuBlob && menu.constructor != MenuSeparator)
		what.className = menu.parent.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 _Menu_writeSubMenus()
{
	if (_Menu_writeSubMenus.called) return;
	_Menu_writeSubMenus.called = true;

	for (var k in Menu.subMenus)
	{
		var div = document.createElement('DIV');
		div.id = k;
		div.innerHTML = Menu.subMenus[k] + '</div>';
		div.style.display = 'none';
		document.body.appendChild(div);
	}

	if (Menu.oldBodyOnload) Menu.oldBodyOnload();
	else if (Menu.oldWindowOnload) Menu.oldWindowOnload();
}

function MenuItem(href)
{
	this.href = href;
	this.parent = null;
	this.id = 'menuitem' + Menu.unique++;
}

function MenuSeparator()
{
	// nothing here.
}

function MenuBlob()
{
	this.parent = null;
	this.id = 'menublob' + Menu.unique++;
}
