if (agent == 'ie' || agent == 'gecko' || agent == 'konqueror') {
	var menuCapable = true;
} else {
	var menuCapable = false;
}

var opt_posxfixed = true;		// The root menu will never dynamically move on the x-axis
var opt_posyfixed = true;		// The root menu will never dynamically move on the y-axis
var opt_movetohide = false;		// Move the sub menu out of the way to hide it
var opt_prebuildsubs = 0;		// 0 = No prebuilding, 1 = Partial prebuilding, 2 = Full prebuilding
var opt_overlapx = 0;			// The default px to overlap the menus on the x axis
var opt_overlapy = 0;			// The default px to overlap the menus on the y axis
var opt_bginit = 1;				// 0 = Process all asap, 1 = Process all after render, 2 = Process in stages

if (menuCapable) {
	function NogginMenu(id) {
		this.items = new Array();
		this.parentIdx = null;
		this.currentItem = null;
		this.childdirection = 1;
		this.id = id;
		this.depth = 0;
		this.topMenu = this;
		this.zIndex = 100;
		this.onClassName = '';
		this.offClassName = '';

		this.add = function _add(title, url, isSelected) {
			this.items[this.items.length] = new NogginMenuItem(title, url, this, isSelected);
		}

		this.install = function _install() {
			if (opt_bginit == 0) {
				this.doinstall();
			} else {
				setTimeout(this.id+'.doinstall()', 1);
			}
		}

		this.doinstall = function _doinstall() {
			//var before = new Date();
			this.lyr = document.getElementById(this.id);
			for (var i = 0; i < this.items.length; i++) {
				//if (i > 0) {
				//	this.lyr.rows[0].appendChild(tmp = document.createElement('td'));
				//	tmp.appendChild(document.createTextNode('|'));
				//	tmp.className = this.offClassName;
				//}
				this.lyr.rows[0].appendChild(tmp = document.createElement('td'));
				tmp.className = this.offClassName;
				tmp.appendChild(document.createTextNode(this.items[i].title));
				this.items[i].setTr(tmp);
				if (opt_prebuildsubs > 0 && this.items[i].sub != null) this.items[i].sub.build();
			}
			this.lyr.onmouseover = function _over(event) {
				if (agent == 'konqueror') {
					document.onmouseover = new Function('event', this.id+'.hide();');
				} else {
					document.onmouseover = new Function('event', this.id+'.hide();');
				}
			}
			//var after = new Date();
			//window.defaultStatus = (after-before);
		}

		this.hide = function _hide() {
			if (this.currentItem != null) {
				if (this.currentItem.sub != null) this.currentItem.sub.hide();
				this.currentItem.out();
			}
			if (agent == 'konqueror') {
				document.onmouseover = function _donothing() {};
			} else {
				document.onmouseover = function _donothing() {};
			}
		}

	}

	function NogginSubMenu(parentMenu, parentIdx) {
		parentMenu.items[parentIdx].sub = this;
		this.parentMenu = parentMenu;
		this.parentItem = parentMenu.items[parentIdx];
		this.parentIdx = parentIdx;
		if (arguments[2] > 0 || arguments[2] < 0) {
			this.childdirection = arguments[2];
		} else {
			this.childdirection = this.parentMenu.childdirection;
		}
		this.depth = parentMenu.depth+1;
		this.topMenu = parentMenu.topMenu;
		this.zIndex = parentMenu.zIndex+1;
		this.itemNodeTemplate = null;
		this.items = new Array();
		this.lyr = null;
		this.currentItem = null;
		this.built = false;
		this.onClassName = this.parentMenu.onClassName;
		this.offClassName = this.parentMenu.offClassName;

		this.add = function _add(title, url) {
			this.items[this.items.length] = new NogginMenuItem(title, url, this, 0);
		}

		this.build = function _build() {
			if (this.built) return false;
			this.lyr = document.createElement('table');
			this.lyr.style.position = 'absolute';
			this.lyr.setAttribute('cellpadding', '3', 0);
			this.lyr.setAttribute('cellspacing', '1', 0);
			if (os == 'Mac' && agent == 'ie') {
				this.lyr.cellPadding = 3;
				this.lyr.cellSpacing = 1;
			}
			this.lyr.style.cursor = (agent == 'ie' ? 'hand' : 'pointer');
			this.lyr.style.width = '110px';
			this.lyr.style.zIndex = this.zIndex;
			var tmp, tbody, tr, maintr;
			tbody = document.createElement('tbody');
			for (var i = 0; i < this.items.length; i++) {
				tbody.appendChild(maintr = document.createElement('tr'));
				maintr.appendChild(tmp = document.createElement('td'));
				tmp.appendChild(document.createTextNode(this.items[i].title));
				this.items[i].setTr(tmp);
				if (this.items[i].sub != null) {
					tmp.className = this.offClassName+(this.childdirection < 0 ? 'left' : 'right');
					//tmp.className = this.offClassName;
					if (opt_prebuildsubs == 2) {
						this.items[i].sub.build();
					}
				} else {
					tmp.className = this.offClassName;
					//tmp.className = (this.childdirection < 0 ? 'topnavitemleft' : 'topnavitemright');
				}
			}
			this.lyr.appendChild(tbody);
			document.body.appendChild(this.lyr);
			this.lyr.onmouseover = detectTarget;
			//if (this.parentMenu && this.parentMenu.parentMenu) {
				this.lyr.style.backgroundColor = '#ffffff';
			//} else {
				//this.lyr.style.backgroundColor = '#e1e1e1';
			//}
			this.lyr.over = function _over(event) {}

			this.lyr.style.top = this.parentItem.subpos.y+'px';
			this.lyr.style.left = this.parentItem.subpos.x+'px';
			this.built = true;
			this.hide();

			return true;
		}

		this.hide = function _hide() {
			if (!this.built) return false;
			if (opt_movetohide) {
				this.lyr.style.top = (-1*this.lyr.offsetHeight-10)+'px';
				this.lyr.style.left = (-1*this.lyr.offsetWidth-10)+'px';
			} else {
				this.lyr.style.visibility = 'hidden';
			}
			if (this.currentItem != null) this.currentItem.out();
		}

		this.show = function _show() {
			if (!this.built) return false;
			this.parentItem.setSubPos();
			this.lyr.style.visibility = 'visible';
			if (!opt_posyfixed || this.lyr.offsetTop != this.parentItem.subpos.y) this.lyr.style.top = this.parentItem.subpos.y+'px';
			if (!opt_posxfixed || this.lyr.offsetLeft != this.parentItem.subpos.x) this.lyr.style.left = this.parentItem.subpos.x+'px';
			if (opt_prebuildsubs == 1) {
				for (var i = 0; i < this.items.length; i++) {
				 	if (this.items[i].sub != null) this.items[i].sub.build();
				}
			}
		}

	}

	function NogginMenuItem(title, url, parentMenu, isSelected) {
		this.title = title;
		this.url = url;
		this.tr = null;
		this.parentMenu = parentMenu;
		this.sub = null;

		this.setTr = function _setTr(tr) {
			this.tr = tr;
			this.tr.item = this;
			this.tr.onmouseover = detectTarget;
			this.tr.onclick = detectTarget;
			if (this.sub != null) {
				this.setSubPos();
			} else if (this.url == '#') {
				this.tr.style.textDecoration = 'line-through';
			}
			if (isSelected) {
				this.tr.className = this.parentMenu.onClassName+(this.childdirection < 0 ? 'left' : 'right');
			}
		}

		this.setSubPos = function _setSubPos() {
			this.subpos = getPagePos(this.tr);
			if (this.parentMenu.parentIdx == null) {
				if (this.sub != null && this.sub.lyr && this.sub.childdirection < 0) {
					this.subpos.x += this.tr.offsetWidth-this.sub.lyr.offsetWidth-opt_overlapx;
				} else {
					this.subpos.x += opt_overlapx;
				}
				this.subpos.y += this.tr.offsetHeight-opt_overlapy;
			} else {
				if (this.sub != null && this.sub.lyr && this.parentMenu.childdirection < 0) {
					this.subpos.x -= this.sub.lyr.offsetWidth-opt_overlapx;
				} else {
					this.subpos.x += this.tr.offsetWidth-opt_overlapx;
				}
				this.subpos.y += opt_overlapy;
			}
		}

		this.over = function _over(event) {
			if (this.parentMenu.currentItem != null) {
				if (this.parentMenu.currentItem.sub != null) this.parentMenu.currentItem.sub.hide();
				this.parentMenu.currentItem.out();
			} else if (this.parentMenu.parentIdx == null) {
				this.parentMenu.lyr.onmouseover(event);
			}
			if (this.tr != null) {
				this.parentMenu.currentItem = this;
				if (this.sub != null) {
					this.tr.className = this.parentMenu.onClassName+(this.childdirection < 0 ? 'left' : 'right');
					this.sub.build();
					this.sub.show();
				} else {
					this.tr.className = this.parentMenu.onClassName;
				}
			}
		}

		this.out = function _out() {
			if (this.tr != null) {
				if (this.sub != null) {
					if (isSelected) {
						this.tr.className = this.parentMenu.onClassName+(this.childdirection < 0 ? 'left' : 'right');
					} else {
						this.tr.className = this.parentMenu.offClassName+(this.childdirection < 0 ? 'left' : 'right');
					}
					this.sub.hide();
				} else {
					if (isSelected) {
						this.tr.className = this.parentMenu.onClassName;
					} else {
						this.tr.className = this.parentMenu.offClassName;
					}
				}
			}
			if (this.parentMenu.currentItem == this) this.parentMenu.currentItem = null;
		}

		this.click = function _click() {
			window.location = this.url;
		}
	}

	function detectTarget(event) {
		if (agent == 'gecko') {
			var item = event.currentTarget.item;
		} else {
			var event = window.event;
			var item = this.item;
		}
		event.cancelBubble = true;
		if (item) {
			switch (event.type) {
				case 'mouseover':
					item.over(event);
					break;
				case 'click':
					item.click(event);
					break;
			}
		}
	}

}
