function Menu() {

	var self = this;

	this.ua = window.navigator.userAgent.toLowerCase();

	//Arrays for storing menu/level info
	this.menu_prevs = new Array();
	this.menu_prev_objects = new Array();
	this.menu_prev_elements = new Array();
	this.menu_timerIDs = new Array();

	//Variable for last highlighted element
	this.menu_prev_e = null;
	this.menu_prev_e_classname = null;

	//Timeout for menus
	this.menu_time = 200;

	//Boolean wether to hide select tag or not
	this.hide_selects = false;

	this.currentitems = new Array();
	this.amounts = new Object();
	this.currentmenu = null;

	if (this.ua.indexOf("msie")!=-1 && parseInt(this.ua.substring(this.ua.indexOf("msie ")+5)) < 7 && this.ua.indexOf("opera")==-1) {
		this.hide_selects = true;
	}

	this.clear = function(limit) {
		for(var i=0; i <= limit; i++) {
			clearTimeout(this.menu_timerIDs[i]);
		}
	};

	this.showDelay = function() {
		if (parseInt(navigator.appVersion)>=4) {
	    	var args = this.showDelay.arguments;

			if (args[0] && args[1] && args[2]) {
				this.menu_timerIDs[args[0]] = setTimeout(function(){self.show(args[0], args[1], args[2]);}, this.menu_time);
			} else if (args[0] && args[1]) {
				this.menu_timerIDs[args[0]] = setTimeout(function(){self.show(args[0], args[1]);}, this.menu_time);
			} else if (args[0]) {
				this.menu_timerIDs[args[0]] = setTimeout(function(){self.show(args[0]);}, this.menu_time);
			} else {
				this.menu_timerIDs[args[0]] = setTimeout(function(){self.show();}, this.menu_time);
			}
		}
	};

	this.hideSelects = function() {
		var args = this.hideSelects.arguments;
		var d = document;

		if (args[0] && args[1] && args[2]) {
			var selects = Array();
			if (d.getElementById) {
				var menu_height = this.yOffset(args[1], true)+d.getElementById(args[0]).offsetHeight;
				var menu_width_min = this.xOffset(args[1]);
				var menu_width_max = this.xOffset(args[1])+d.getElementById(args[0]).offsetWidth;
				selects = d.getElementsByTagName("SELECT");
			} else if (d.all) {
				var menu_height = this.yOffset(args[1], true)+d.all(args[0]).offsetHeight;
				var menu_width_min = this.xOffset(args[1]);
				var menu_width_max = this.xOffset(args[1])+d.all(args[0]).offsetWidth;
				selects = d.body.all.tags("SELECT");
			}
			selects_length = selects.length;
			if (selects_length  > 5)
				selects_length = 5;
			for (var i=0; i < selects_length; i++) {
				if (menu_height > this.yOffset(selects[i])
					&& (
					( menu_width_max > this.xOffset(selects[i]) && menu_width_min < this.xOffset(selects[i]) )
					|| 
					( menu_width_min >= this.xOffset(selects[i]) && (menu_width_max-menu_width_min) < selects[i].offsetWidth && menu_width_max <= selects[i].offsetWidth )
					|| 
					( menu_width_min < (this.xOffset(selects[i]) + selects[i].offsetWidth) && menu_width_max > (this.xOffset(selects[i]) + selects[i].offsetWidth) )
					)
					) {
					selects[i].style.visibility = args[2];
				}
			}
		}
	};

	this.show = function() {
		if (parseInt(navigator.appVersion)>=4) {
			var v, args = this.show.arguments;
			var obj, obj_style, parent;

			if (args[0]) {
				clearTimeout(this.menu_timerIDs[args[0]]);
				for(var i=args[0]; i <= this.menu_prevs.length; i++) {
					if (this.menu_prevs[i] != null) {
						this.menu_prevs[i].visibility = 'hidden';
						//hide select boxes if browser == ie
						if (this.hide_selects) {
							if (this.menu_prev_elements[i] != null) {
								this.hideSelects(this.menu_prev_objects[i], this.menu_prev_elements[i], 'visible');
							}
						}
					}
				}
			} else {
				for(var i=1; i <= this.menu_prevs.length; i++) {
					if (this.menu_prevs[i] != null) {
						this.menu_prevs[i].visibility = 'hidden';
						//hide select boxes if browser == ie
						if (this.hide_selects) {
							if (this.menu_prev_elements[i] != null) {
								this.hideSelects(this.menu_prev_objects[i], this.menu_prev_elements[i], 'visible');
							}
						}
					}
				}
			}

			if (args[1]) {
				d = document;
				if ( d.layers ) {
					obj_style = d.layers[args[1]];
					obj = d.layers[args[1]];
				} else if ( d.getElementById ) {
					obj = d.getElementById([args[1]]);
					obj_style = obj.style;
				} else if ( d.all ) {
					obj = d.all(args[1]);
					obj_style = obj.style;
				}

				obj.clicked = true;
				if (args[2]) {
					v = args[2];
					v = (v == 'v') ? 'visible' : 'hidden';
				} else {
					v = (obj_style.visibility == 'visible') ? 'hidden' : 'visible';
				}
				if (args[3]) {
					if ( d.layers ) {
						parent = this.menu_prevs[args[0]-1];
						if (args[4]) {
							if (parent) {
								obj_style.left = parent.left + this.xOffset(args[3], args[4]);
								obj_style.top = parent.top + this.yOffset(args[3], !args[4]);
							} else {
								obj_style.left = this.xOffset(args[3], args[4]);
								obj_style.top = this.yOffset(args[3], !args[4]);
							}
						} else {
							if (parent) {
								obj_style.left = parent.left + this.xOffset(args[3]);
								obj_style.top = parent.top + this.yOffset(args[3], true);
							} else {
								obj_style.left = this.xOffset(args[3]);
								obj_style.top = this.yOffset(args[3], true);
							}
						}
					} else {
						if (args[4]) {
							parent = args[3];
							while (parent.tagName != "TABLE") {
								parent = parent.parentNode;
								if (parent == null) {
									parent = args[3];
									break;
								}
							}
		
							if (args[5]) {
								args[5] += "";
								if (args[5].indexOf(":") != -1) {
									x_pad = args[5].substring(0, args[5].indexOf(":"))*1;
									y_pad = args[5].substring(args[5].indexOf(":")+1)*1;
								} else {
									x_pad = args[5]*1;
									y_pad = args[5]*1;
								}
							} else {
								x_pad = 0;
								y_pad = 0;
							}
							
							switch(args[4]) {
								case "left":
									if (!d.getElementById && d.all) {
										obj_style.left = (this.xOffset(args[3]) - 170 - x_pad) + 'px';
									} else {
										obj_style.left = (this.xOffset(args[3]) - parent.offsetWidth - x_pad) + 'px';
									}
									obj_style.top = (this.yOffset(args[3]) + y_pad) + 'px';
									break;
								case "right":
									if (!d.getElementById && d.all) {
										obj_style.left = (this.xOffset(args[3]) + 170 + x_pad) + 'px';
									} else {
										obj_style.left = (this.xOffset(args[3]) + parent.offsetWidth + x_pad) + 'px';
									}
									obj_style.top = (this.yOffset(args[3]) + y_pad) + 'px';
									break;
								case "above":
									obj_style.left = (this.xOffset(args[3]) + x_pad) + 'px';
									obj_style.top = (this.yOffset(args[3]) - parent.offsetHeight - y_pad) + 'px';
									break;
								case "below":
									obj_style.left = (this.xOffset(args[3]) + x_pad) + 'px';
									obj_style.top = (this.yOffset(args[3]) + parent.offsetHeight + y_pad) + 'px';
									break;
							}
						} else {
							obj_style.left = this.xOffset(args[3]);
							obj_style.top = this.yOffset(args[3], true);
						}
					}
					this.menu_prev_elements[args[0]] = args[3];
					//hide/show select boxes if browser == ie
					if (this.hide_selects) {
						v2 = (v == 'visible') ? 'hidden' : 'visible';
						this.hideSelects(args[1], args[3], v2);
					}
				}
				obj_style.visibility = v;
				for(var i=1; i < args[0]; i++) {
					if (this.menu_prevs[i] != null) {
						this.menu_prevs[i].visibility = v;
						//hide select boxes if browser == ie
						if (this.hide_selects) {
							if (this.menu_prev_elements[i] != null) {
								this.hideSelects(this.menu_prev_objects[i], this.menu_prev_elements[i], v2);
							}
						}
					}
				}
				this.menu_prev_objects[args[0]] = args[1];
				this.menu_prevs[args[0]] = obj_style;
			}
		}
	};

	this.toggleClass = function(e, className) {
		if (this.menu_prev_e != null) {
			this.menu_prev_e.className = this.menu_prev_e_classname;
		}
		if (e) {
			this.menu_prev_e_classname = e.className;
			e.className = className;
			this.menu_prev_e = e;
		}
		return true;
	};

	this.toggleCustom = function(e, bgcolor) {
		if (this.menu_prev_e != null) {
			this.menu_prev_e.style.backgroundColor = "";
		}
		if (e && bgcolor) {
			e.style.backgroundColor = bgcolor;
			this.menu_prev_e = e;
		}
		return true;
	};

	this.yOffset = function(element, add_element_height) {
		var y = 0;

		if (element == null)
			return 0;

		if (element.y) {
			if (add_element_height)
				if (element.offsetHeight)
					y += element.offsetHeight;
				else
					y += 20;
			y += element.y;
		} else {
			e = element;
			if(e.offsetParent) {
				while(e.offsetParent) {
					y += e.offsetTop;
					e = e.offsetParent;
				}
			}
			if (add_element_height)
				y += element.offsetHeight;
		}

		if (isNaN(y)) {
			return 0;
		}
		return y;
	};

	this.xOffset = function(element, add_element_width) {
		var x = 0;

		if (element == null)
			return 0;

		if (element.x) {
			if (add_element_width)
				if (element.offsetWidth)
					x += element.offsetWidth;
				else
					x += 50;
			x += element.x;
		} else {
			e = element;
			if(e.offsetParent) {
				while(e.offsetParent) {
					x += e.offsetLeft;
					e = e.offsetParent;
				}
			}
			if (add_element_width)
				x += element.offsetWidth;
		}

		if (isNaN(x)) {
			return 0;
		}
		return x;
	};

	this.addCurrent = function(id) {
		this.currentitems[this.currentitems.length] = id;
	};

	this.openItems = function(items, dimension, index) {
		if (!index) {
			index = 0;
		}
		if (index == items.length) {
			return;
		}
		var itemid = items[index];
		var e = document.getElementById(itemid);
		if (!Resizer.InProgress(e)) {
			var am = 0;
			if (dimension == "height") {
				am = e.offsetHeight;
			} else {
				am = e.offsetWidth;
			}
			if (am < this.amounts[itemid]) {
				Resizer.ResizeTo(e, dimension, "+", this.amounts[itemid], 30);
			}
			index++;
		}
		window.setTimeout(function() {self.openItems(items, dimension, index);}, 20);
	};

	this.showMenu = function(element, dimension) {
		var found = false;
		var items = new Array();
		element = element.parentNode;
		var uls = element.getElementsByTagName("ul");
/*
		if (uls[0].style.visibility == "hidden") {
			uls[0].style.position = "";
			uls[0].style.visibility = "visible";
		}
*/
		var length = uls[0].childNodes.length;
		var counter = 0;
		for (var i=0; i < length; i++) {
			if (uls[0].childNodes[i].tagName && uls[0].childNodes[i].tagName.toLowerCase() == "li") {
				items[counter] = uls[0].childNodes[i].id;
				counter++;
			}
		}
		length = this.currentitems.length;
		for (var i=0; i < length; i++) {
			if (this.currentitems[i] == element.id) {
				found = true;
				break;
			}
		}
		if (found) {
			length = items.length;
			//Add to current items if submenu opened
			for (var i=0; i < length; i++) {
				this.currentitems[this.currentitems.length] = items[i];
			}
			this.currentmenu = uls[0];
			this.openItems(items, dimension);
		} else {
			this.closeShowItems(items, dimension, this.currentitems.length-1);
		}
	};

	this._hideUL = function(menu, items) {
		var inprogress = false;
		for (var i=0; i<items.length; i++) {
			var e = document.getElementById(items[i]);
			if (Resizer.InProgress(e)) {
				inprogress = true;
				break;
			}
		}
		if (inprogress) {
			window.setTimeout(function() {self._hideUL(menu, items);}, 20);
		} else {
			menu.style.position = "absolute";
			menu.style.visibility = "hidden";
		}
	}

	this.closeShowItems = function(items, dimension, index) {
		if (index < 0 || this.currentitems.length == 0) {
			if (index < 0) {
				if (this.currentmenu == null && this.currentitems.length > 0) {
					var itm = document.getElementById(this.currentitems[0]);
					if (itm) {
						self._hideUL(itm.parentNode, this.currentitems);						
					}
				}
				if (this.currentmenu && this.currentitems.length > 0 && this.currentmenu != document.getElementById(items[0]).parentNode) {
					var m = this.currentmenu;
					window.setTimeout(function() {self._hideUL(m, items);}, 20);
				}
				this.currentmenu = document.getElementById(items[0]).parentNode;
			}
			this.currentmenu.style.position = "";
			this.currentmenu.style.visibility = "visible";
			this.currentitems = items;
			this.openItems(items, dimension);
			return;
		}
		var itemid = this.currentitems[index];
		var e = document.getElementById(itemid);
		if (e) {
			if (!this.amounts[itemid]) {
				if (dimension == "height") {
					this.amounts[itemid] = e.offsetHeight;
				} else {
					this.amounts[itemid] = e.offsetWidth;
				}
			}
			if (!Resizer.InProgress(e)) {
				Resizer.Resize(e, dimension, "-", 30);
				index--;
			}
		} else {
			index--;
		}
		window.setTimeout(function() {self.closeShowItems(items, dimension, index);}, 20);
	};

	this.hideMenu = function(itemid, dimension) {
		var e = document.getElementById(itemid);
		if (dimension == "height") {
			this.amounts[itemid] = e.offsetHeight;
			e.style.height = "0px";
			e.style.minHeight = "0px";
		} else {
			this.amounts[itemid] = e.offsetWidth;
			e.style.width = "0px";
			e.style.minWidth = "0px";
		}
		if (e.parentNode.style.visibility != "hidden") {
			e.parentNode.style.position = "absolute";
			e.parentNode.style.visibility = "hidden";
		}
	};

};
var TWM = new Menu();
document.writeln("<script src=\"js/includes/Resizer.js\"></script>");
