if (NG.ua.isEng('MSIE') || NG.ua.isEng('MACIE') || NG.ua.isEng('GECKO') || NG.ua.isEng('KHTML')) {
	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 = 0;		// 0 = Process all asap, 1 = Process all after render, 2 = Process in stages
var opt_dropshadow = false;	// Set true to have a drop show effect for non IE users.

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.baseElement = null;
		this.isOverSelects = false;
		this.docOverEventId = null;

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

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

		this.doinstall = function _doinstall() {
			if (NG.ua.isEng('MSIE', 6, '>=') && document.getElementsByTagName('select').length > 0) this.isOverSelects = true;
			this.lyr = document.getElementById(this.id);
			var hrefs = this.lyr.getElementsByTagName('a');
			for (var i = 0; i < this.items.length; i++) {
				if (i < hrefs.length) {
					this.items[i].setTr(hrefs[i]);
					if (opt_prebuildsubs > 0 && this.items[i].sub != null) this.items[i].sub.build();
				}
			}
			this.lyr.onmouseover = function _over(event) {
				document.onmouseover = new Function('event', this.id+'.hide();');
			}
			this.baseElement = this.lyr;
		}

		this.hide = function _hide() {
			if (this.isOverSelects && (typeof window.event.srcElement.menu != 'undefined')) {
				return;
			}
			if (this.currentItem != null) {
				if (this.currentItem.sub != null) this.currentItem.sub.hide();
				this.currentItem.out();
			}
			if (NG.ua.isEng('KHTML')) {
				document.onmouseover = function _donothing() {};
			} else {
				document.onmouseover = function _donothing() {};
			}
		}

	}

	function NogginSubMenu(parentMenu, parentIdx) {
		if (arguments.length < 2) parentIdx = parentMenu.items.length-1;
		parentMenu.items[parentIdx].sub = this;
		this.parentMenu = parentMenu;
		this.isOverSelects = false;
		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.baseElement = null;

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

		this.build = function _build() {
			if (this.built) return false;
			this.isOverSelects = this.parentMenu.isOverSelects;
			if (this.isOverSelects) {
				this.lyr = document.createElement('iframe');
				this.lyr.frameBorder = 0;
				this.lyr.name = 'tmp';
				this.lyr.src = '/blank.html';
				document.body.appendChild(this.lyr);
				this.lyr.style.position = 'absolute';
				var doc = this.lyr.contentWindow.document;
				doc.open();
				doc.close();
				for (var i = 0; i < document.styleSheets.length; i++) {
					doc.body.appendChild(doc.createElement('style'));
					doc.styleSheets[i].cssText = document.styleSheets[i].cssText;
				}
				doc.body.style.margin = '0px';
				var fSize = false;
				if (NG.ua.isEng('MSIE') && document.body.currentStyle.fontSize) {
					fSize = document.body.currentStyle.fontSize;
				}
				var content = this.buildContents(doc, fSize);
				doc.body.appendChild(content);
				doc.body.onmouseover = detectTarget;
				this.baseElement = doc.body;
				this.lyr.style.height = content.offsetHeight+'px';
				this.lyr.style.width = content.offsetWidth+'px';
				this.lyr.menu = this;
			} else {
				this.lyr = this.buildContents(document);
				this.lyr.onmouseover = detectTarget;
				this.baseElement = this.lyr;
				document.body.appendChild(this.lyr);
			}

			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.buildContents = function _buildContents(doc) {
			var fragment = doc.createElement('table');
			fragment.style.position = 'absolute';
			fragment.setAttribute('cellpadding', '0', 0);
			fragment.setAttribute('cellspacing', '0', 0);
			if (NG.ua.isEng('MACIE')) {
				fragment.cellPadding = 0;
				fragment.cellSpacing = 0;
			}
			fragment.style.cursor = (NG.ua.isEng('MSIE') ? 'hand' : 'pointer');

			// affects the last top menu item
/*			if (this.depth == 1 && parentMenu.items[parentMenu.items.length-1].sub == this) {
				fragment.style.width = '110px';
			} else {
				fragment.style.width = '130px';
			}
*/
			fragment.style.border = '1px solid #cccccc';
			fragment.style.zIndex = this.zIndex;
			var tmp, tbody, tr, maintr;
			tbody = doc.createElement('tbody');
			if (arguments.length > 1) {
				tbody.style.fontSize = arguments[1];
			}
			for (var i = 0; i < this.items.length; i++) {
				tbody.appendChild(maintr = doc.createElement('tr'));
				maintr.appendChild(tmp = doc.createElement('td'));
				tmp.appendChild(doc.createTextNode(this.items[i].title));
				if (i > 0) {
					tmp.style.borderTop = '#cccccc solid 1px';
				}

				this.items[i].setTr(tmp);
				if (this.items[i].sub != null) {
					//tmp.className = (this.childdirection < 0 ? 'topnavitemsubleft' : 'topnavitemsubright');
					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');
				}
			}
			fragment.appendChild(tbody);
			fragment.over = function _over(event) {}
			return fragment;
		}

		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) {
		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';
			}
		}

		this.setSubPos = function _setSubPos() {
			this.subpos = NG.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.baseElement !== null) {
				this.parentMenu.baseElement.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;
				}
				if (this.parentMenu.depth == 0) {
					fadeothers(document.getElementById('stamps').getElementsByTagName('a')[this.offset]);
				}
			}
		}

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

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

	function detectTarget(event) {
		if (NG.ua.isEng('MSIE')) {
			//if (this.item.parentMenu == null || this.item.parentMenu.parentIdx == null || !this.item.parentMenu.isOverSelects) {
				var event = window.event;
			//} else {
//				var event = this.item.parentMenu.lyr.contentWindow.event;
			//}
			var item = this.item;
		} else {
			var item = event.currentTarget.item;
		}
		event.cancelBubble = true;
        if (event.stopPropagation) event.stopPropagation();
		if (item) {
			switch (event.type) {
				case 'mouseover':
					if (isFunc(item.over)) item.over(event);
					break;
				case 'click':
					if (isFunc(item.click)) item.click(event);
					break;
			}
		}
	}

}

function NogginCollapsableMenu() {
	this.items = new Array();
	this.itemClassOn = 'nav-on';
	this.itemClassOff = 'nav';
	this.itemClassLeaf = 'nav-leaf';
	this.paddingPerDepth = 15;
	this.maxDynamicDepth = 10;
	this.lastItem = null;
	this.itemsById = new Object();
	this.currentItem = null;
	this.url = null;
	this.waitingNode = null;
	this.containerNode = null;

	this.destroy = function _destroy() {
		for (var i = this.items.length-1; i >= 0; i--) {
			this.itemsById[this.items[i].id] = null;
			this.items[i].destroy();
			this.items[i] = null;
		}
		this.lastItem = null;
		this.currentItem = null;
		this.waitingNode = null;
		this.containerNode = null;
	}

	this.addChild = function _addChild(item) {
		this.items[this.items.length] = item;
	}

	this.add = function _add(id, name, depth, pageid, url) {
		var tmp = new Item(id, name);
		tmp.nav = this;
		tmp.depth = depth;
		if (arguments.length > 4) tmp.url = url;
		if (parseInt(pageid) > 0) tmp.pageid = parseInt(pageid);
		if (depth == 0 || this.lastItem === null) {
			this.addChild(tmp);
		} else if (depth == this.lastItem.depth+1) {

			this.lastItem.addChild(tmp);
		} else if (depth == this.lastItem.depth) {
			this.lastItem.parentItem.addChild(tmp);
		} else if (depth < this.lastItem.depth) {
			tmpParent = this.lastItem.parentItem;
			while (tmpParent !== null && tmpParent.depth != depth-1) {
				tmpParent = tmpParent.parentItem;
			}
			tmpParent.addChild(tmp);
		}
		if (typeof this.itemsById[tmp.id] == 'undefined') this.itemsById[tmp.id] = tmp;
		this.lastItem = tmp;
	}
	this.a = this.add;

	this.show = function _show(containerid, expandid) {
		var scrolly = (arguments.length >= 4) ? arguments[3] : -1;
		if (typeof expandid == 'string') {
			expandid = parseInt(expandid);
		}

		this.containerNode = document.getElementById(containerid);
		if (this.containerNode) {
			while (this.containerNode.firstChild) {
				this.containerNode.removeChild(this.containerNode.firstChild);
			}
			this.waitingNode = document.createElement('DIV');
			this.waitingNode.className = this.waitingClass;
			this.waitingNode.appendChild(document.createElement('BR'));
			this.waitingNode.appendChild(document.createElement('BR'));
			this.waitingNode.appendChild(document.createTextNode('Loading categories...'));
			this.waitingNode.appendChild(document.createElement('BR'));
			this.waitingNode.appendChild(document.createElement('BR'));
			this.waitingNode.appendChild(document.createElement('BR'));
			this.containerNode.appendChild(this.waitingNode);
			setTimeout('nav.showActual('+expandid+', '+scrolly+')', 1);
		}

	}

	this.showActual = function _showActual(expandid) {
		this.install();
		if (this.waitingNode !== null) this.waitingNode.parentNode.removeChild(this.waitingNode);
		for (var i = 0; i < this.items.length; i++) {
			this.containerNode.appendChild(this.items[i].getNode());
		}
		if (typeof this.itemsById[expandid] != 'undefined') {
			this.itemsById[expandid].expand();
		}
		if (arguments.length >= 3 && arguments[2] >= 0) {
			window.scrollTo(0, arguments[2]);
		}
	}

	function Item(id, name) {
		this.id = id;
		this.name = name;
		this.url = null;
		this.depth = null;
		this.node = null;
		this.nav = null;
		this.parentItem = null;
		this.childItems = new Array();
		this.childrenHidden = true;
		this.getNode = _getNode;
		this.expand = _expand;
		this.collapse = _collapse;
		this.addChild = _addChild;
		this.select = _select;
		this.showChildren = _showChildren;
		this.hideChildren = _hideChildren;
		this.pageid = null;
		this.destroy = _destroyItem;
	}

	function _destroyItem() {
		if (this.node != null) {
			this.node.navItem = null;
			this.onclick = null;
		}
		this.node = null;
		this.nav = null;
		this.parentItem = null;
		for (var i = this.childItems.length; i >= 0; i--) {
			this.childItems[i] = null;
		}
	}

	function _getNode() {
		if (this.node === null) {

			this.node = document.createElement('DIV');
			this.node.navItem = this;
			if (this.childItems.length == 0 && this.depth > 0) {
				this.node.className = nav.itemClassOff+' '+nav.itemClassLeaf;
			} else {
				if (this.depth > 0) {
					this.node.className = nav.itemClassOff+' '+nav.itemClassLeaf;
				} else {
					this.node.className = nav.itemClassOff;
				}
			}

			if (this.depth > 0) {
				var ul = document.createElement('UL');
				var li = document.createElement('LI');
				li.appendChild(document.createTextNode(this.name));
				ul.appendChild(li);
				this.node.appendChild(ul);
				li.className = 'navitem-li';
				ul.className = 'navitem-ul';
			} else {
				this.node.appendChild(document.createTextNode(this.name));
			}
			this.node.style.paddingLeft = ((this.depth+1)*this.nav.paddingPerDepth)+'px';
			this.node.style.textAlign = 'left';
			this.node.onclick = this.select;
		}
		return this.node;
	}

	function _expand() {
		if (this.nav.currentItem !== null) this.nav.currentItem.collapse();
		if (this.parentItem !== null) this.parentItem.expand();
		this.showChildren();
		this.nav.currentItem = this;
	}

	function _collapse() {
		this.hideChildren();
		if (this.nav.currentItem == this) {
			this.nav.currentItem = null;
		}
		if (this.parentItem !== null) this.parentItem.collapse();
	}

	function _addChild(item) {
		item.parentItem = this;
		this.childItems[this.childItems.length] = item;
	}

	function _select() {
		var args = '';
		if (typeof window.scrollY != 'undefined' && window.scrollY > 0) {
			args += '?scrolly='+window.scrollY;
		} else if (typeof document.body.scrollTop != 'undefined' && document.body.scrollTop > 0) {
			args += '?scrolly='+document.body.scrollTop;
		}
		if (this.navItem.url != null) {
			window.location = this.navItem.url;
			return;
		}
		if (this.navItem.pageid != null) {
			window.location = '/content.php/' + this.navItem.pageid + '.html' + args;
			return;
		}
		if (this.navItem.childItems.length > 0 && this.navItem.depth <= this.navItem.nav.maxDynamicDepth) {
			if (this.navItem.childrenHidden) {
				this.navItem.expand();
			} else {
				this.navItem.hideChildren();
			}
		}
	}

	function _showChildren() {
		if (!this.childrenHidden) return;
		if (this.getNode().nextSibling) {
			var next = this.getNode().nextSibling;
			for (var i = 0; i < this.childItems.length; i++) {
				next.parentNode.insertBefore(this.childItems[i].getNode(), next);
			}
		} else {
			for (var i = 0; i < this.childItems.length; i++) {
				this.getNode().parentNode.appendChild(this.childItems[i].getNode());
			}
		}
		if (this.childItems.length == 0 && this.depth > 0) {
			this.node.className = nav.itemClassOn+' '+nav.itemClassLeaf;
		} else {
			if (this.depth > 0) {
				this.node.className = nav.itemClassOn+' '+nav.itemClassLeaf;
			} else {
				this.node.className = nav.itemClassOn;
			}
		}
		this.childrenHidden = false;
	}

	function _hideChildren() {
		if (this.childrenHidden) return;
		for (var i = 0; i < this.childItems.length; i++) {
			this.childItems[i].hideChildren();
			this.childItems[i].getNode().parentNode.removeChild(this.childItems[i].getNode());
		}
		if (this.childItems.length == 0 && this.depth > 0) {
			this.node.className = nav.itemClassOff+' '+nav.itemClassLeaf;
		} else {
			if (this.depth > 0) {
				this.node.className = nav.itemClassOff+' '+nav.itemClassLeaf;
			} else {
				this.node.className = nav.itemClassOff;
			}
		}
		this.childrenHidden = true;
	}

	this.install = function _install() {
		//loadNavData(this);
	}

}