oMenu.n = -1;
oMenu.prefixId = "p_mn";
oMenu.className = "clMenuBox";
oMenu.open = "onmouseover";
oMenu.arrows = "all";
oMenu.showTimeout = 200;
oMenu.closeTimeout = 1000;

oMenu.buttonShow = "hor";
oMenu.voiceShow = "ver";
oMenu.subvoiceShow = "ver";
oMenu.voiceAlign = "bottom";
oMenu.subvoiceAlign = "right";

oMenu.separator = "<img src=\""+p.conf+"spacer.gif\" class=\"separator\">";

function oMenu(className, open, show, arrows) {
	this.n = oMenu.n;
	this.divid = this.getId();
	this.id = this.getId("mb", this.n);
	this.className = className || oMenu.className;
	this.open = open || oMenu.open;
	this.arrows = arrows || oMenu.arrows;
	this.menus = [];
	this.items = [];
	this.buttons = [];
	this.stylesFor = [];
	this.stylesFor.show = show || oMenu.buttonShow;
	
	document.write('<div id="'+this.divid+'" class="'+this.className+'"></div>');
	this.boxhook = p.bw.getById(this.divid);

	this.isopen = false;
	this.workels = [];

	this.boxhook.onmouseout = new Function("oMenu["+this.n+"].close("+oMenu.closeTimeout+", '"+this.open+"')");
	p.bw.addEvent(document.body, "click", new Function("if (oMenu["+this.n+"].closeOnClick) oMenu["+this.n+"].close()"));
}

oMenu.prototype.addButton = function(vname, vtitle, vlink, vclass, show, align, bwidth, bheight, width, height) {
	var nb = this.buttons.length;
	var obj = (this.buttons[nb] = new Object());
	
	obj.parent = this;
	obj.id = this.getId("b", nb);
	obj.arrow = (this.arrows=="all" || this.arrows=="button") ? 1 : 0;
	
	with (obj.el = this.hookElement(nb, obj, "buttons")) {
		if (nb==0) obj.el.className = "first";
		if (bwidth) style.width = bwidth;
		if (bheight) style.height = bheight;


//		var div = document.createElement("DIV");
//		var handler = eval("div."+this.open);
//		handler = new Function('oMenu['+this.n+'].over(this, \''+obj.id+'\''+((this.open=="onclick") ? ', 1' : '')+')');

//		appendChild(div);

//		this.addLink(vname, vtitle, vlink, vclass, div);
		

		innerHTML =	'<div '+this.open+'="'+'oMenu['+this.n+'].over(this, \''+obj.id+'\''+((this.open=="onclick") ? ', 1' : '')+')'+'">'+
				this.addLink(vname, vtitle, vlink, vclass)+'</div>';
	}

	obj.voices = [];
	obj.stylesFor = [];
	obj.stylesFor.show = show || oMenu.voiceShow;
	obj.stylesFor.align = align || oMenu.voiceAlign;
	obj.stylesFor.display = "none";
	obj.stylesFor.position = "absolute";
	obj.stylesFor.width = width;
	obj.stylesFor.height = height;
}

oMenu.prototype.addVoice = function(vname, vtitle, vlink, vclass, show, align, width, height) {
	var nb = this.buttons.length-1;
	var nv = this.buttons[nb].voices.length;
	var obj = (this.buttons[nb].voices[nv] = new Object());
	
	obj.parent = this.buttons[nb];
	obj.id = this.getId("v", nb+"_"+nv);
	obj.arrow = (this.arrows=="all" || this.arrows=="voice") ? 1 : 0;
	
	with (obj.el = this.hookElement(nv, obj, "voices")) {
		if (nv==0) obj.el.className = "first";

		innerHTML = '<div onmouseover="oMenu['+this.n+'].over(this, \''+obj.id+'\')">'+
				this.addLink(vname, vtitle, vlink, vclass)+'</div>';
	}
	
	obj.subvoices = [];
	obj.stylesFor = [];
	obj.stylesFor.show = show || oMenu.subvoiceShow;
	obj.stylesFor.align = align || oMenu.subvoiceAlign;
	obj.stylesFor.display = "none";
	obj.stylesFor.position = "absolute";
	obj.stylesFor.width = width;
	obj.stylesFor.height = height;
}

oMenu.prototype.addSubVoice = function(vname, vtitle, vlink, vclass) {
	var nb = this.buttons.length-1;
	var nv = this.buttons[nb].voices.length-1;
	var ns = this.buttons[nb].voices[nv].subvoices.length;
	var obj = (this.buttons[nb].voices[nv].subvoices[ns] = new Object());
	
	obj.parent = this.buttons[nb].voices[nv];
	obj.id = this.getId("s", nb+"_"+nv+"_"+ns);
	
	with (obj.el = this.hookElement(ns, obj, "subvoices")) {
		if (ns==0) obj.el.className = "first";

		innerHTML = '<div onmouseover="oMenu['+this.n+'].over()">'+
				this.addLink(vname, vtitle, vlink, vclass)+'</div>';
	}
}

oMenu.prototype.hookElement = function(n, obj, vClass) {
	var eltmp, elrow, elcell;
	
	this.items[obj.id] = obj;

	if (n==0) {
		var eltable = document.createElement("TABLE");
		this.boxhook.appendChild(eltable);
		obj.parent.evnt = eltable;
		
		with (obj.parent) {
			evnt.id = id;
			evnt.setAttribute("cellPadding", 0);
			evnt.setAttribute("cellSpacing", 0);
			evnt.style.display = stylesFor.display || "";
			evnt.style.position = stylesFor.position || "";
			evnt.style.width = stylesFor.width || "";
			evnt.style.height = stylesFor.height || "";
			evnt.className = vClass;
			
			if (!evnt.firstChild) evnt.appendChild(document.createElement("TBODY"));
			if (stylesFor.show=="hor") {
				var elrow = document.createElement("TR");
				evnt.firstChild.appendChild(elrow);
				obj.parent.hook = elrow;
			} else obj.parent.hook = evnt.firstChild;
			
			if (eltmp = obj.parent.el) {
				if (obj.parent.arrow) {
					while (eltmp.firstChild) {
						if (eltmp.tagName=="A") break;
						eltmp = eltmp.firstChild;
					}
					eltmp.className = "aw"+stylesFor.align;
				}
			}
			if (!obj.parent.buttons) this.menus[id] = obj;
		}	
	}
	if (obj.parent.stylesFor.show=="hor") {
		obj.parent.hook.appendChild(elcell = document.createElement("TD"));
	} else {
		obj.parent.hook.appendChild(elrow = document.createElement("TR"));
		elrow.appendChild(elcell = document.createElement("TD"));
	}
	return elcell;
}

oMenu.prototype.getId = function(el, n) {
	if (el) return oMenu.prefixId+"_"+this.n+"_"+el+"_"+n;
	else return oMenu.prefixId+"_"+this.n;
}

oMenu.prototype.addLink = function(vname, vtitle, vlink, vclass, div) {
		if (div) {
			if (vname) {
				var a = document.createElement("A");
				a.href = "";
				a.onclick = new Function(this.setOnClick(vlink));
				a.title = vtitle || "";
				a.className = vclass || "";
				a.appendChild(document.createTextNode(vname));
				div.appendChild(a);
			} else {
				div.appendChild(document.createTextNode(oMenu.separator));
			}
		} else {
			if (vname) return '<a href="" onclick="'+this.setOnClick(vlink)+'" title="'+(vtitle || "")+'" class="'+(vclass || "")+'">'+vname+'</a>';
			else return oMenu.separator;
		}
}

oMenu.prototype.setOnClick = function(vlink) {
	if (vlink) return vlink+"; oMenu["+this.n+"].close(); return false";
	else return "return false";
}

oMenu.prototype.setPos = function(obj, currel, el) {
	var x = p.bw.screenFindPosX(currel);
	var y = p.bw.screenFindPosY(currel);

	switch (obj.parent.stylesFor.align) {
		case "top":
			y-=el.offsetHeight;
		break;
		case "right":
			x+=currel.offsetWidth;
		break;
		case "bottom":
			y+=currel.offsetHeight;
		break;		
		case "left":
			x-=el.offsetWidth;
		break;
	}
	el.style.top = y+"px";
	el.style.left = x+"px";
}

oMenu.prototype.rollClass = function(start, end, vClass) {
	for (var i=start;i<end;i++) {
		if (this.workels[i]) {
			with (this.workels[i].firstChild) {
				if (vClass) className+="selected";
				else className = className.replace("selected", "");
			}
		}
	}
}

oMenu.prototype.clearTimeouts = function() {
	if (this.showTimeout) window.clearTimeout(this.showTimeout);
	if (this.closeTimeout) window.clearTimeout(this.closeTimeout);
}

oMenu.prototype.over = function(currel, id, timeout) {
	this.closeOnClick = false;
	this.clearTimeouts();

	if (this.isopen && currel==this.workels[1]) {
		if (this.open=="onclick") this.close();
	} else if (currel) {
		if (this.items[id].voices) {
			if (this.workels[0]) this.rollClass(1, 3);
			this.workels[1] = currel;
		} else if (this.items[id].subvoices) {
			if (this.workels[0]) this.rollClass(2, 3);
			this.workels[2] = currel;
		}
		this.workels[0] = currel;
		this.showTimeout = window.setTimeout("oMenu["+this.n+"].show('"+id+"')", (timeout || oMenu.showTimeout));
	}
}

oMenu.prototype.show = function(id) {
	var el;
	var obj = this.items[id];

	for (el in this.menus) {
		if (this.menus[el].parent != undefined) {
			with (this.menus[el].parent) {
				if (evnt.id!=obj.parent.evnt.id && evnt.id!=this.id) this.hide(evnt.id);
			}
		}
	}

	if (el = p.bw.getById(id)) {
		this.setPos(this.menus[id], this.workels[0], el);
		this.rollClass(0, 1, true);
		el.style.display = "block";

		if (p.bw.ie) {
			if (!oMenu.allToHide) oMenu.allToHide = p.bw.getByTagName("SELECT");
			for (i=0;i<oMenu.allToHide.length;i++) {
				oMenu.allToHide[i].style.visibility = "hidden";
			}
		}

		this.isopen = true;
	}
}

oMenu.prototype.hide = function(id) {
	var el;

	if (id) {
		if (el = p.bw.getById(id)) el.style.display = "none";
	} else {
		if (this.isopen) {
			for (el in this.menus) {
				if (this.menus[el].parent != undefined) {
					with (this.menus[el].parent) this.hide(evnt.id);
				}
			}
			this.rollClass(0, 3);

			if (p.bw.ie) {
				for (i=0;i<oMenu.allToHide.length;i++) {
					oMenu.allToHide[i].style.visibility = "visible";
				}
			}
			this.isopen = false;
		}
	}
}

oMenu.prototype.close = function(timeout, close) {
	window.clearTimeout(this.closeTimeout);

	if (close=="onclick") this.closeOnClick = true;
	else {
		this.closeOnClick = true;
		this.closeTimeout = window.setTimeout("oMenu["+this.n+"].hide()", (timeout || 0));
	}
}