// DHTML / JS menu 1.6

// Written by David J. Taylor
// © djtSoft
// http://www.djtsoft.com/

// Note: requires lib.js

var menuAr = new Array();

function menuItem(text) {
	this.text = text;
	this.allowWrap = false;
	this.icon = "";
	this.highlightedIcon = "";
	this.link = "";
	this.target = null;
	this.submenu = "";
	this.width = 0;
}

function menuObject(id) {
	this.id = id;
	this.normalClassName = null;
	this.highlightedClassName = null;
	this.iconClassName = null;
	this.normalArrowClassName = null;
	this.highlightedArrowClassName = null;
	this.nolinkClassName = null;
	this.separatorClassName = null;
	this.scrollClassName = null;
	this.scrollDisabledClassName = null;
	this.width = null;
	this.x = null;
	this.y = null;
	this.horizontal = true;
	this.maxHeight = null;
	this.scrollHoverSpeed = null;
	this.scrollClickSpeed = null;
	this.scrollReset = null;
	this.openDelay = null;
	this.closeDelay = null;
	this.bubbleCloseDelay = null;
	this.target = null;

	this.items = new Array();

	this.addMenuItem = function(mnuItem) {
		this.items[this.items.length] = mnuItem;
	}
}

function menuGroup() {
	this.normalClassName = "";
	this.highlightedClassName = "";
	this.iconClassName = "";
	this.normalArrowClassName = "";
	this.highlightedArrowClassName = "";
	this.nolinkClassName = "";
	this.separatorClassName = "";
	this.scrollClassName = "";
	this.scrollDisabledClassName = "";
	this.width = 0;
	this.maxHeight = 0;
	this.scrollHoverSpeed = -1;
	this.scrollClickSpeed = -1;
	this.scrollReset = true;
	this.openDelay = 500;
	this.closeDelay = 1000;
	this.bubbleCloseDelay = 50;
	this.target = "";

	this.objects = new Array();

	this.addMenuObject = function(mnuObject) {
		this.objects[this.objects.length] = mnuObject;
	}

	this.init = function() {
		if (!document || (!document.all && !document.getElementById) || !document.createElement || (!document.attachEvent && !document.addEventListener) || !document.body || !document.body.appendChild || !window || !window.setTimeout) return false;

		var i, j, k, l, obj, item, otable, otd, odiv, table, tr, td, div, img;
		for (i in this.objects) {
			obj = this.objects[i];

			if (obj.id == "" || obj.id.indexOf(" ") >= 0) continue;

			if (obj.normalClassName == null) obj.normalClassName = this.normalClassName;
			if (obj.highlightedClassName == null) obj.highlightedClassName = this.highlightedClassName;
			if (obj.iconClassName == null) obj.iconClassName = this.iconClassName;
			if (obj.normalArrowClassName == null) obj.normalArrowClassName = this.normalArrowClassName;
			if (obj.highlightedArrowClassName == null) obj.highlightedArrowClassName = this.highlightedArrowClassName;
			if (obj.nolinkClassName == null) obj.nolinkClassName = this.nolinkClassName;
			if (obj.separatorClassName == null) obj.separatorClassName = this.separatorClassName;
			if (obj.scrollClassName == null) obj.scrollClassName = this.scrollClassName;
			if (obj.scrollDisabledClassName == null) obj.scrollDisabledClassName = this.scrollDisabledClassName;
			if (obj.width == null) obj.width = this.width;
			if (obj.maxHeight == null) obj.maxHeight = this.maxHeight;
			if (obj.scrollHoverSpeed == null) obj.scrollHoverSpeed = this.scrollHoverSpeed;
			if (obj.scrollClickSpeed == null) obj.scrollClickSpeed = this.scrollClickSpeed;
			if (obj.scrollReset == null) obj.scrollReset = this.scrollReset;
			if (obj.openDelay == null) obj.openDelay = this.openDelay;
			if (obj.closeDelay == null) obj.closeDelay = this.closeDelay;
			if (obj.bubbleCloseDelay == null) obj.bubbleCloseDelay = this.bubbleCloseDelay;
			if (obj.target == null) obj.target = this.target;

			otable = document.createElement("TABLE");
			attachEventHandler(otable, "selectstart", cancelSelect);
			attachEventHandler(otable, "mousemove", cancelSelect);
			attachEventHandler(otable, "dblclick", cancelSelect);
			otable.id = "menu" + obj.id;
			otable.className = obj.normalClassName;
			otable.normalClassName = obj.normalClassName;
			otable.highlightedClassName = obj.highlightedClassName;
			otable.normalArrowClassName = obj.normalArrowClassName;
			otable.highlightedArrowClassName = obj.highlightedArrowClassName;
			otable.scrollClassName = obj.scrollClassName;
			otable.scrollDisabledClassName = obj.scrollDisabledClassName;
			otable.cellSpacing = 0;
			otable.style.position = "absolute";
			otable.style.tableLayout = "fixed";
			otable.topLevel = obj.x != null && obj.y != null;
			otable.horizontal = obj.horizontal && otable.topLevel;
			otable.x = obj.x;
			otable.y = obj.y;
			otable.w = obj.width;

			if (!otable.topLevel) {
				moveObjTo(otable, 0, 0);
				setVisibility(otable, 0);

				if (getBrowserName() == "MSIE") {
					otable.iframe = document.createElement("IFRAME");
					otable.iframe.style.position = "absolute";
					otable.iframe.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0, opacity=0)";
					otable.iframe.scrolling = "no";
					otable.iframe.frameBorder = 0;
					moveObjTo(otable.iframe, 0, 0);
					setVisibility(otable.iframe, 0);
					document.body.appendChild(otable.iframe);
				}
			}

			attachEventHandler(otable, "mouseover", menuEvent);
			attachEventHandler(otable, "mouseout", menuEvent);
			attachEventHandler(otable, "click", menuEvent);
			attachEventHandler(otable, "mousedown", menuEvent);
			attachEventHandler(otable, "mouseup", menuEvent);

			tr = otable.insertRow(0);
			td = tr.insertCell(0);
			if (otable.horizontal) {
				td.scrollDir = -1;
				td.innerText = String.fromCharCode(9668);
				otable.scroll1 = td;

				otd = tr.insertCell(1);
				setWidth(otd, "100%");

				td = tr.insertCell(2);
				td.scrollDir = 1;
				td.innerText = String.fromCharCode(9658);
				otable.scroll2 = td;
			} else {
				td.scrollDir = -1;
				//div = document.createElement("DIV");
				//attachEventHandler(div, "selectstart", cancelSelect);
				td.innerText = String.fromCharCode(9650);
				//td.appendChild(div);
				otable.scroll1 = td;

				tr = otable.insertRow(1);
				otd = tr.insertCell(0);

				tr = otable.insertRow(2);
				td = tr.insertCell(0);
				td.scrollDir = 1;
				//div = document.createElement("DIV");
				//attachEventHandler(div, "selectstart", cancelSelect);
				td.innerText = String.fromCharCode(9660);
				//td.appendChild(div);
				otable.scroll2 = td;
			}

			otable.maxHeight = obj.maxHeight;
			otable.scrollHoverSpeed = ((obj.scrollHoverSpeed == 0 && obj.scrollClickSpeed == 0) || obj.scrollHoverSpeed < 0) ? ((otable.horizontal) ? 200 : 50) : obj.scrollHoverSpeed;
			otable.scrollClickSpeed = ((obj.scrollHoverSpeed == 0 && obj.scrollClickSpeed == 0) || obj.scrollClickSpeed < 0) ? ((otable.horizontal) ? 600 : 150) : obj.scrollClickSpeed;
			otable.scrollReset = obj.scrollReset;
			otable.speed = 0;
			otable.openDelay = obj.openDelay;
			otable.closeDelay = obj.closeDelay;
			otable.bubbleCloseDelay = obj.bubbleCloseDelay;
			otd.style.padding = "0";
			odiv = document.createElement("DIV");
			attachEventHandler(odiv, "selectstart", cancelSelect);
			odiv.style.overflow = "hidden";
			if (!otable.horizontal) setWidth(odiv, "100%");
			otd.appendChild(odiv);
			otable.outerBody = odiv;

			table = document.createElement("TABLE");
			attachEventHandler(table, "selectstart", cancelSelect);
			table.cellSpacing = 0;
			if (!otable.horizontal) setWidth(table, "100%");

			if (otable.horizontal) tr = table.insertRow(0);
			l = 0;

			for (j in obj.items) {
				item = obj.items[j];

				if (item.target == null) item.target = obj.target;

				if (otable.horizontal) {
					k = item.width;
					if (item.submenu != "") k -= 18;
					k = Math.max(k, 1) + "px";
				} else {
					k = "100%";
					tr = table.insertRow(j);
					l = 0;
				}

				td = tr.insertCell(l++);
				if (item.text == "-") {
					td.className = obj.separatorClassName;
					td.sep = true;
					div = document.createElement("DIV");
					attachEventHandler(div, "selectstart", cancelSelect);
					div.className = obj.separatorClassName;
					if (otable.horizontal) {
						setWidth(td, "1px");
						div.style.fontSize = 0;
						div.style.borderTop = 0;
						div.style.borderBottom = 0;
						div.style.borderRight = 0;
						div.style.margin = "0px 0px 0px 2px";
					} else {
						td.colSpan = 3;
						div.style.fontSize = 0;
						div.style.borderLeft = 0;
						div.style.borderRight = 0;
						div.style.borderBottom = 0;
						div.style.marginTop = 2;
						div.style.marginBottom = -2;
					}
					td.appendChild(div);
				} else {
					td.icn = true;
					td.className = obj.iconClassName + ((item.submenu == "" && item.link == "") ? (" " + obj.nolinkClassName) : "");
					td.style.borderRightWidth = 0;
					td.style.paddingRight = 0;
					if (item.icon != "") {
						img = document.createElement("IMG");
						img.src = item.icon;
						td.appendChild(img);
					} else {
						td.style.padding = 0;
						td.style.fontSize = 0;
						td.innerHTML = "<br />";
					}

					td = tr.insertCell(l++);
					td.className = obj.normalClassName;
					td.style.borderLeftWidth = 0;
					setWidth(td, k);
					if (item.submenu == "" && !otable.horizontal) td.colSpan = 2;
					td.link = item.link;
					td.target = item.target;
					td.submenu = item.submenu;
					td.icon = item.icon;
					td.highlightedIcon = item.highlightedIcon;
					if (item.icon != "") td.img = img;
					if (item.allowWrap) {
						td.innerText = item.text;
					} else {
						td.innerText = rplce(item.text, " ", String.fromCharCode(160));
					}
					if (item.submenu != "") {
						if (item.link == "") td.style.borderRightWidth = 0;
						td = tr.insertCell(l++);
						td.arrow = true;
						td.className = obj.normalArrowClassName;
						td.style.borderLeftWidth = 0;
						setWidth(td, "1px");
						if (item.link == "") td.style.paddingLeft = 0;
						td.innerText = String.fromCharCode((otable.horizontal) ? 9660 : 9658);
					} else if (item.link == "") {
						td.className = obj.nolinkClassName;
					}
				}
			}
			odiv.appendChild(table);
			otable.innerBody = table;
			document.body.appendChild(otable);
			menuScroll(otable, 0);
			menuAr[menuAr.length] = otable;
		}

		attachEventHandler(window, "resize", menuUpdate);
		attachEventHandler(window, "load", menuUpdate);

		menuUpdate();

		return true;
	}
}

function cancelSelect(e) {
	var src = e.srcElement;
	if ((src.tagName == "INPUT" && src.type == "text") || src.tagName == "TEXTAREA") return;
	e.cancelBubble = true;
	e.returnValue = false;
	return false;
}

function menuUpdate() {
	// give the browser time to perform content layout
	window.setTimeout("menuDoUpdate();", 10);
}

function menuDoUpdate() {
	var menu, x, y, w;
	for (var i in menuAr) {
		menu = menuAr[i];

		if (menu.topLevel) {
			x = menu.x + "";
			if (x.indexOf("expression") == 0) x = eval(x.substring(10));

			y = menu.y + "";
			if (y.indexOf("expression") == 0) y = eval(y.substring(10));

			moveObjTo(menu, x, y);
		}

		// first set the width to 1 so that the menu width isn't being included if the expression contains getPageWidth()
		setWidth(menu, "1px");

		w = menu.w + "";
		if (w.indexOf("expression") == 0) w = eval(w.substring(10));

		setWidth(menu, w);

		menuScroll(menu, 0);
	}
}

function menuGetMenu(src) {
	src = getObj(src);

	while (src) {
		if (src.tagName == "TABLE" && src.normalClassName && src.highlightedClassName) return src;
		src = getParent(src);
	}

	return;
}

function menuClearTimer(menu) {
	if (menu && menu.timer) {
		window.clearTimeout(menu.timer);
		menu.timer = null;
	}
}

function menuEvent(e) {
	var src, typ = e.type;
	if (e.target) src = e.target;
	if (e.srcElement) src = e.srcElement;
	if (!src) return;

	while (src.tagName != "TD" && src.tagName != "TABLE") {
		src = getParent(src);
		if (!src) return;
	}

	var arrow = src.arrow || src.link == "";
	if (src.arrow) src = src.previousSibling;
	if (src.icn) src = src.nextSibling;

	var menu = menuGetMenu(src);
	menuClearTimer(menu);

	if (menu && (typ == "mouseover" || typ == "mouseout")) {
		menu.mouseover = typ == "mouseover";
		if (typ == "mouseout") {
			if (menu.openSubMenuItem) menuHighlight(menu.openSubMenuItem, true, true);
			menu.timer = window.setTimeout("menuHide('" + getUniqueID(menu) + "');", menu.closeDelay);
		}
	}

	if (src.tagName == "TABLE" || (!src.link && !src.submenu)) {
		if (menu && src.scrollDir) {
			var speed = menu.speed;
			menu.speed = (typ == "mousedown") ? menu.scrollClickSpeed : ((typ != "mouseout") ? menu.scrollHoverSpeed : 0);
			if (menu.speed > 0 && speed == 0) menuScroll(menu, src.scrollDir);
		}
		return;
	}

	if (typ != "click") {
		if (typ == "mouseover" || typ == "mouseout") menuMouseOverItem(src, typ == "mouseover", false, arrow);
		return;
	}

	if (src.submenu != "" && arrow) {
		menuShow(src);
		return;
	}
	if (src.link == "") return;

	if (menu && !menu.topLevel) {
		menu.mouseover = false;
		menuMouseOverItem(src, false, true);
	}

	if (src.link.indexOf("javascript:") == 0) {
		eval(src.link);
	} else {
		switch (src.target) {
			case "":
			case "_self":
				window.location.href = src.link;
				break;
			case "_top":
				top.location.href = src.link;
				break;
			case "_blank":
				window.open(src.link);
				break;
			default:
				eval(src.target).location.href = src.link;
		}
	}
}

function menuHighlight(src, flg, arrow) {
	var item = getObj(src);
	if (!item) return;

	var menu = menuGetMenu(item);
	if (!menu) return;

	item.className = menu.normalClassName + ((flg) ? " " + menu.highlightedClassName : "");

	if (item.previousSibling) item.previousSibling.className = item.className;
	if (item.icon != "" && item.img) {
		item.img.src = (flg && item.highlightedIcon != "") ? item.highlightedIcon : item.icon;
	}

	if (item.submenu != "" && item.nextSibling) {
		item.nextSibling.className = menu.normalArrowClassName + ((flg && arrow) ? " " + menu.highlightedArrowClassName : "");
	}
}

function menuMouseOverItem(src, flg, cflg, arrow, bubble) {
	var item = getObj(src);
	if (!item) return;
	if (item.link == "" && item.submenu == "") return;
	item.mouseover = flg;
	item.mouseoverarrow = flg && arrow;

	if (item.submenu != "") {
		var menu = getObj("menu" + item.submenu);
		if (menu) {
			menuClearTimer(menu);
			if (!flg && getVisibility(menu) > 0 && menu.parentMenuItem == item) {
				menu.timer = window.setTimeout("menuHide('menu" + item.submenu + "', " + ((cflg) ? "true" : "false") + ");", (cflg) ? 10 : menu.closeDelay);
				return;
			}
		}
	}

	menuHighlight(item, flg, arrow);

	var menu = menuGetMenu(item);
	if (menu) {
		menuClearTimer(menu);
		if (flg && menu.openSubMenuItem && item != menu.openSubMenuItem) menuHighlight(menu.openSubMenuItem, false);
	}

	if (flg) {
		if (item.submenu != "") {
			menu = getObj("menu" + item.submenu);
			if (menu) {
				if (arrow) {
					if (menu.openDelay >= 0) menu.timer = window.setTimeout("menuShow('" + getUniqueID(item) + "');", menu.openDelay);
				} else {
					menu.timer = window.setTimeout("menuHide('menu" + item.submenu + "', " + ((cflg) ? "true" : "false") + ");", (cflg) ? 10 : menu.closeDelay);
				}
			}
		}
	} else {
		menu.timer = window.setTimeout("menuHide('" + getUniqueID(menu) + "', " + ((cflg) ? "true" : "false") + ");", (cflg) ? 10 : ((bubble) ? menu.bubbleCloseDelay : menu.closeDelay));
	}
}

function menuScroll(src, dir) {
	var menu = menuGetMenu(src);
	if (!menu) return;

	var i, j;
	if (menu.horizontal) {
		var w1 = getWidth(menu.innerBody), w2 = menu.w + "";
		if (w2.indexOf("expression") == 0) w2 = eval(w2.substring(10));
		if (w2 < w1) w2 -= getWidth(menu.scroll1) + getWidth(menu.scroll2);
		if (w2 > w1) w2 = w1;
		setWidth(menu.outerBody, w2 + "px");

		j = w1 - w2;
		i = Math.min(Math.max(menu.outerBody.scrollLeft + dir * menu.speed / 10, 0), j);

		menu.outerBody.scrollLeft = i;

		menu.scroll1.style.display = (j > 0) ? "" : "none";
		menu.scroll1.className = menu.normalClassName + " " + menu.scrollClassName + ((i > 0) ? "" : (" " + menu.scrollDisabledClassName));
		menu.scroll2.style.display = (j > 0) ? "" : "none";
		menu.scroll2.className = menu.normalClassName + " " + menu.scrollClassName + ((i < j) ? "" : (" " + menu.scrollDisabledClassName));
	} else {
		var h1 = getHeight(menu.innerBody), h2 = Math.min((menu.maxHeight > 0) ? Math.min(h1, menu.maxHeight) : h1, getWindowHeight() - 32);
		setHeight(menu.outerBody, h2);

		j = h1 - h2;
		i = Math.min(Math.max(menu.outerBody.scrollTop + dir * menu.speed / 10, 0), j);

		menu.outerBody.scrollTop = i;

		menu.scroll1.style.display = (j > 0) ? "" : "none";
		menu.scroll1.className = menu.normalClassName + " " + menu.scrollClassName + ((i > 0) ? "" : (" " + menu.scrollDisabledClassName));
		menu.scroll2.style.display = (j > 0) ? "" : "none";
		menu.scroll2.className = menu.normalClassName + " " + menu.scrollClassName + ((i < j) ? "" : (" " + menu.scrollDisabledClassName));
	}

	if (j > 0 && dir != 0 && menu.speed > 0) window.setTimeout("menuScroll('" + getUniqueID(menu) + "', " + dir + ");", 100);
}

function menuShow(src) {
	var item = getObj(src);
	if (!item) return;
	if (!item.mouseoverarrow || item.submenu == "") return;

	var menu = menuGetMenu(item);
	if (!menu) return;

	if (menu.openSubMenuItem) {
		if (menu.openSubMenuItem.submenu != "") menuHide("menu" + menu.openSubMenuItem.submenu, true);
		menu.openSubMenuItem = null;
	}

	var menu2 = getObj("menu" + item.submenu);
	if (!menu2 || menu2.topLevel) return;

	if (menu2.scrollReset) {
		menu2.outerBody.scrollLeft = 0;
		menu2.outerBody.scrollTop = 0;
	}
	menuScroll(menu2, 0);

	menu.openSubMenuItem = item;
	menu2.parentMenuItem = item;

	var x = getTrueLeft(item) - Math.max(menu.outerBody.scrollLeft, 0), y = getTrueTop(item) - Math.max(menu.outerBody.scrollTop, 0);
	if (menu.horizontal) {
		var i = getLeft(menu2.scroll1);
		if (i) x -= i;
		y += getHeight(menu);
		if (x + getWidth(menu2) > getWindowWidth()) x = getWindowWidth() - getWidth(menu2);
		if (y + getHeight(menu2) > getWindowHeight()) y = getTrueTop(item) - getHeight(menu2) - 1;
	} else {
		x += getWidth(menu);
		var i = getTop(menu2.scroll1);
		if (i) y -= i;
		if (x + getWidth(menu2) > getWindowWidth()) x = getTrueLeft(item) - getWidth(menu2) + 1;
		if (y + getHeight(menu2) > getWindowHeight()) y = getWindowHeight() - getHeight(menu2) - 1;
	}

	if (menu2.iframe) {
		moveObjTo(menu2.iframe, Math.max(x, 0), Math.max(y, 0));
		setWidth(menu2.iframe, getWidth(menu2) + "px");
		setHeight(menu2.iframe, getHeight(menu2) + 2);
		setVisibility(menu2.iframe, 1);
	}
	moveObjTo(menu2, Math.max(x, 0), Math.max(y, 0));
	if (menu2.filters) {
		for (var i = 0; i < menu2.filters.length; i++) {
			menu2.filters[i].stop();
			menu2.filters[i].apply();
		}
		setVisibility(menu2, 1);
		for (var i = 0; i < menu2.filters.length; i++) {
			menu2.filters[i].play();
		}
	} else {
		setVisibility(menu2, 1);
	}
}

function menuHide(src, cflg) {
	var menu = menuGetMenu(src);
	if (!menu || menu.topLevel) return;

	if (menu.mouseover || menu.openSubMenuItem) {
		if (!cflg) return;
		if (menu.openSubMenuItem) {
			if (menu.openSubMenuItem.mouseover) return;
			if (menu.openSubMenuItem.submenu != "") menuHide("menu" + menu.openSubMenuItem.submenu, cflg);
		}
		if (menu.mouseover || menu.openSubMenuItem) return;
	}

	if (menu.parentMenuItem) {
		if (menu.parentMenuItem.mouseoverarrow) return;
	}

	if (menu.filters) {
		for (var i = 0; i < menu.filters.length; i++) {
			menu.filters[i].stop();
			menu.filters[i].apply();
		}
		setVisibility(menu, 0);
		for (var i = 0; i < menu.filters.length; i++) {
			menu.filters[i].play();
		}
	} else {
		setVisibility(menu, 0);
	}
	if (menu.iframe != null) setVisibility(menu.iframe, 0);
	if (menu.parentMenuItem) {
		if (!menu.parentMenuItem.mouseover) menuMouseOverItem(menu.parentMenuItem, false, cflg, false, true);

		var menu2 = menuGetMenu(menu.parentMenuItem);
		if (menu2) {
			if (menu2.openSubMenuItem == menu.parentMenuItem) {
				menu2.openSubMenuItem = null;
			}
		}
		menu.parentMenuItem = null;
	}
}
