function Menu (x, y, id) {
	this.x = x;
	this.y = y;
	this.orientation = "vertical";
	this.alwaysActive = false;
	if (arguments.length > 2) {
		this.id = id;
		Menu.Registry [id] = this;
	} else {
		this.id = "";
	}
	this.width = 0;
	this.maxWidth = 0;
	this.height = 0;
	this.maxHeight = 0;
	this.status = Menu.Collapsed;
	this.options = new Array ();
	this.parent = null;
	this.children = new Array ();
	this.active = false;
	this.regIndex = Menu.Registry.length;
	
	this.obj = document.createElement ("div");
	this.obj.className = Menu.mClass;
	this.style = this.obj.style;
	this.style.position = "absolute";
	this.style.width = "auto";
	this.style.height = "auto";
	this.style.overflow = "hidden";
	this.style.left = x + "px";
	this.style.top = y + "px";
	if (!this.alwaysActive) this.style.visibility = "hidden";
	
	Menu.Registry [this.regIndex] = this;
	document.body.appendChild (this.obj);
}

Menu.Registry = new Array ();
Menu.OptionRegistry = new Array ();
Menu.CloseDelay = 10;
Menu.CloseTimer = -1;
Menu.Collapsed = 0;
Menu.Expanding = 1;
Menu.Expanded = 2;
Menu.Collapsing = 3;
Menu.ExpandSpeed = 10;
Menu.CollapseSpeed = Menu.ExpandSpeed;
Menu.expImage = "images/expandarrow.gif";

Menu.setClasses = function (mClass, oClass, sClass) {
	Menu.mClass = mClass;
	Menu.oClass = oClass;
	Menu.sClass = sClass;
}

Menu.setSpeeds = function (expSpeed, colSpeed) {
	Menu.ExpandSpeed = expSpeed;
	Menu.CollapseSpeed = colSpeed;
}

Menu.setCloseTimer = function (t) {
	Menu.CloseTimer = t;
}

Menu.drop = function () {
	Menu.CloseTimer = Menu.CloseDelay;
}

Menu.expand = function (index) {
	for (var i = 0; i < Menu.Registry.length; i++) Menu.Registry [i].active = false;
	if (index != -1) {
		Menu.CloseTimer = -1;
		mObj = Menu.Registry [index];
		mObj.active = true;
		if (arguments.length > 1) {
			if (typeof arguments [1] == "object") {
				elm = arguments [1];
				mObj.x = elm.offsetLeft;
				mObj.y = elm.offsetTop + elm.offsetHeight;

				while (elm.offsetParent !== document.body) {
					elm = elm.offsetParent;
					mObj.x += elm.offsetLeft;
					mObj.y += elm.offsetTop;
				}
				
				if (arguments.length > 3) {
					mObj.x += arguments [2];
					mObj.y += arguments [3];
				}
			} else {
				mObj.x = arguments [1];
				mObj.y = arguments [2];
			}
			mObj.style.left = mObj.x + "px";
			mObj.style.top = mObj.y + "px";
		}
		while (mObj.parent !== null) {
			mObj = mObj.parent;
			mObj.active = true;
		}
	}
}

Menu.collapse = function (index) {
	for (var i = 0; i < Menu.Registry.length; i++) Menu.Registry [i].active = false;
	mObj = Menu.Registry [index];
	mObj.active = false;
}

Menu.drop = function () {
	Menu.CloseTimer = Menu.CloseDelay;
}

Menu.update = function () {
	if (--Menu.CloseTimer == 0) Menu.expand (-1);
	for (var i = 0; i < Menu.Registry.length; i++) {
		Menu.Registry [i].update ();
	}
}

Menu.prototype.addOption = function (txt, lnk) {
	var optIndex = Menu.OptionRegistry.length;
	var newOpt = this.options [this.options.length] = new Object ();
	if (this.orientation == "vertical") newOpt.obj = document.createElement ("div");
	else newOpt.obj = document.createElement ("span");
	newOpt.obj.style.cursor = "default";
	newOpt.obj.className = Menu.oClass;
	newOpt.lnk = lnk;
	if (typeof newOpt.lnk == "object") {
		this.children [this.children.length] = newOpt.lnk;
		newOpt.lnk.parent = this;
		newOpt.obj.innerHTML = "<nobr>" + txt + "</nobr>";
		newOpt.obj.onmouseover = new Function ("this.className = Menu.sClass; Menu.expand (" + newOpt.lnk.regIndex + ",this.offsetLeft + this.offsetWidth + this.offsetParent.offsetLeft, this.offsetTop + this.offsetParent.offsetTop);");
		newOpt.obj.onmouseout = new Function ("this.className = Menu.oClass; Menu.CloseTimer = Menu.CloseDelay");
		newOpt.obj.onclick = new Function ("return false;");
	} else {
		newOpt.obj.innerHTML = "<nobr>" + txt + "</nobr>";
		newOpt.obj.onmouseover = new Function ("this.className = Menu.sClass; Menu.expand (" + this.regIndex + ");");
		newOpt.obj.onmouseout = new Function ("this.className = Menu.oClass; Menu.CloseTimer = Menu.CloseDelay");
		if (arguments.length < 3) newOpt.obj.onclick = new Function ("location.href = \"" + newOpt.lnk + "\";");
		else newOpt.obj.onclick = ((arguments [2] != "_blank") ? new Function ("document.frames [\"" + arguments [2] + "\"].window.document.location.href = \"" + newOpt.lnk + "\";") : new Function ("var w = window.open (\"" + newOpt.lnk + "\", \"NEW_WINDOW\", \"\"); w.focus ();"));
	}
	this.obj.appendChild (newOpt.obj);
	this.style.height = "auto";
	this.style.width = "auto";
	this.maxHeight = 0;
	for (var i = 0; i < this.options.length; i++) this.maxHeight += this.options [i].obj.offsetHeight;
	w1 = this.obj.offsetWidth;
	newOpt.obj.className = Menu.sClass;
	w2 = this.obj.offsetWidth;
	
	this.style.height = "0px";
	this.style.width = (w1 > w2 ? w1 : w2) + "px";
	newOpt.obj.className = Menu.oClass;
	Menu.OptionRegistry [Menu.OptionRegistry.length] = this;
}

Menu.prototype.update = function () {
	if (this.active && (this.status == Menu.Collapsed || this.status == Menu.Collapsing)) this.status = Menu.Expanding;
	else if (!this.active && (this.status == Menu.Expanded || this.status == Menu.Expanding)) {
		var keepActive = false;
		for (var i = 0; i < this.children.length; i++) {
			if (this.children [i].status != Menu.Collapsed) keepActive = true;
		}
		if (!keepActive) this.status = Menu.Collapsing;
	}

	if (!this.alwaysActive) {
		switch (this.status) {
			case Menu.Expanding : {
				this.style.visibility = "visible";
				this.height += Menu.ExpandSpeed;
				if (this.height >= this.maxHeight || Menu.ExpandSpeed == 0) {
					this.height = this.maxHeight;
					this.status = Menu.Expanded;
				}
				this.style.height = this.height + "px";
				break;
			}
			case Menu.Collapsing : {
				this.height -= Menu.CollapseSpeed;
				if (this.height <= 0 || Menu.CollapseSpeed == 0) {
					this.height = 0;
					this.status = Menu.Collapsed;
					this.style.visibility = "hidden";
				}
				this.style.height = this.height + "px";
				break;
			}
		}
	}
}
