function XulMenu(g)
	{
	this.type="horizontal";
	this.position=
		{
		"level1":
			{
			"top":0,"left":0
		}
		,"levelX":
			{
			"top":0,"left":0
		}
	};
	this.zIndex=
		{
		"visible":1,"hidden":-1
	};
	this.arrow1=null;
	this.arrow2=null;
	this.timeout=400;
	this.timer=false;
	this.browser=
		{
		"ie":Boolean(document.body.currentStyle),"ie5":(navigator.appVersion.indexOf("MSIE 5.5")!=-1||navigator.appVersion.indexOf("MSIE 5.0")!=-1)
	};
	if(!this.browser.ie)
		{
		this.browser.ie5=false
	}
	this.init=function()
		{
		if(!document.getElementById(this.id))alert("Element '"+this.id+"' does not exist in this document. XulMenu cannot be initialized.");
		if(this.type!="horizontal"&&this.type!="vertical")
			{
			return alert("XulMenu.init() failed. Unknown menu type: '"+this.type+"'")
		}
		document.onmousedown=click;
		if(this.browser.ie&&this.browser.ie5)
			{
			this.fixWrap()
		};
		this.fixSections();
		this.parse(document.getElementById(this.id).childNodes,this.tree,this.id)
	};
	this.fixSections=function()
		{
		var a=document.getElementById(this.id).getElementsByTagName("div");
		var b=new Array();
		var c=new Array();
		for(var i=0;
		i<a.length;
		i++)
			{
			if(a[i].className=="section")
				{
				b.push(a[i])
			}
		};
		for(var i=0;
		i<b.length;
		i++)
			{
			c.push(this.getMaxWidth(b[i].childNodes))
		};
		for(var i=0;
		i<b.length;
		i++)
			{
			b[i].style.width=(c[i])+"px"
		};
		if(h.browser.ie)
			{
			for(var i=0;
			i<b.length;
			i++)
				{
				this.setMaxWidth(b[i].childNodes,c[i])
			}
		}
	};
	this.fixWrap=function()
		{
		var a=document.getElementById(this.id).getElementsByTagName("a");
		for(var i=0;
		i<a.length;
		i++)
			{
			if(/item/.test(a[i].className))
				{
				a[i].innerHTML='<div nowrap="nowrap">'+a[i].innerHTML+'</div>'
			}
		}
	};
	this.getMaxWidth=function(a)
		{
		var b=0;
		for(var i=0;
		i<a.length;
		i++)
			{
			if(a[i].nodeType!=1||a[i].className=="section")
				{
				continue
			};
			if(a[i].offsetWidth>b)b=a[i].offsetWidth
		};
		return b
	};
	this.setMaxWidth=function(a,b)
		{
		for(var i=0;
		i<a.length;
		i++)
			{
			if(a[i].nodeType==1&&/item/.test(a[i].className)&&a[i].currentStyle)
				{
				if(this.browser.ie5)
					{
					a[i].style.width=(b)+"px"
				}
				else
					{
					a[i].style.width=(b-parseInt(a[i].currentStyle.paddingLeft)-parseInt(a[i].currentStyle.paddingRight))+"px"
				}
			}
		}
	};
	this.parse=function(a,b,c)
		{
		for(var i=0;
		i<a.length;
		i++)
			{
			if(a[i].nodeType!=1)
				{
				continue
			};
			switch(a[i].className.substr(0,4))
				{
				case"butt":a[i].id=c+"-"+b.length;
				b.push(new Array());
				a[i].onmouseover=buttonOver;
				a[i].onfocus=buttonOver;
				a[i].onclick=buttonClick;
				a[i].onmouseout=buttonOut;
				break;
				case"item":a[i].id=c+"-"+b.length;
				b.push(new Array());
				a[i].onmouseover=itemOver;
				a[i].onfocus=itemOver;
				a[i].onmouseout=itemOut;
				a[i].onclick=itemClick;
				break;
				case"sect":a[i].id=c+"-"+(b.length-1)+"-section";
				var d=document.getElementById(c+"-"+(b.length-1));
				var e=document.getElementById(a[i].id);
				var f=new Element(d.id);
				if(f.level==1)
					{
					if(this.type=="horizontal")
						{
						e.style.top=(d.offsetTop+d.offsetHeight+this.position.level1.top)+"px";
						if(this.browser.ie5)
							{
							e.style.left=(this.position.level1.left)+"px"
						}
						else
							{
							e.style.left=(d.offsetLeft+this.position.level1.left)+"px"
						};
						e.style.paddingLeft="1px"
					}
					else if(this.type=="vertical")
						{
						e.style.top=(d.offsetTop+this.position.level1.top)+"px";
						if(this.browser.ie5)
							{
							e.style.left=(d.offsetWidth+this.position.level1.left)+"px"
						}
						else
							{
							e.style.left=(d.offsetLeft+d.offsetWidth+this.position.level1.left)+"px"
						}
					}
				}
				else
					{
					e.style.top=(d.offsetTop+this.position.levelX.top-2)+"px";
					e.style.left=(d.offsetLeft+d.offsetWidth+this.position.levelX.left+10)+"px";
					e.style.paddingLeft="10px"
				};
				break;
				case"arro":a[i].id=c+"-"+(b.length-1)+"-arrow";
				break
			};
			if(a[i].childNodes)
				{
				if(a[i].className=="section")
					{
					this.parse(a[i].childNodes,b[b.length-1],c+"-"+(b.length-1))
				}
				else
					{
					this.parse(a[i].childNodes,b,c)
				}
			}
		}
	};
	this.hideAll=function()
		{
		for(var i=this.visible.length-1;
		i>=0;
		i--)
			{
			this.hide(this.visible[i])
		}
	};
	this.hideHigherOrEqualLevels=function(n)
		{
		for(var i=this.visible.length-1;
		i>=0;
		i--)
			{
			var a=new Element(this.visible[i]);
			if(a.level>=n)
				{
				this.hide(a.id)
			}
			else
				{
				return
			}
		}
	};
	this.hide=function(a)
		{
		var b=new Element(a);
		document.getElementById(a).className=(b.level==1?"button":"item");
		if(b.level>1&&this.arrow2)
			{
			document.getElementById(a+"-arrow").src=this.arrow1
		};
		document.getElementById(a+"-section").style.visibility="hidden";
		document.getElementById(a+"-section").style.zIndex=this.zIndex.hidden;
		if(this.visible.contains(a))
			{
			if(this.visible.getLast()==a)
				{
				this.visible.pop()
			}
			else
				{
				throw"XulMenu.hide("+a+") failed, trying to hide element that is not deepest visible element"
			}
		}
		else
			{
			throw"XulMenu.hide("+a+") failed, cannot hide element that is not visible"
		}
	};
	this.show=function(a)
		{
		var b=new Element(a);
		document.getElementById(a).className=(b.level==1?"button-active":"item-active");
		if(b.level>1&&this.arrow2)
			{
			document.getElementById(a+"-arrow").src=this.arrow2
		};
		document.getElementById(a+"-section").style.visibility="visible";
		document.getElementById(a+"-section").style.zIndex=this.zIndex.visible;
		this.visible.push(a)
	};
	function click(e)
		{
		var a;
		if(e)
			{
			a=e.target.tagName?e.target:e.target.parentNode
		}
		else
			{
			a=window.event.srcElement;
			if(a.parentNode&&/item/.test(a.parentNode.className))
				{
				a=a.parentNode
			}
		};
		if(!h.visible.length)
			{
			return
		};
		if(!a.onclick)
			{
			h.hideAll()
		}
	};
	function buttonOver()
		{
		h.hideAll();
		var a=new Element(this.id);
		if(a.hasChilds())
			{
			h.show(this.id)
		};
		if(h.timer!=false)
			{
			window.clearTimeout(h.timer);
			h.timer=false
		}
	};
	function buttonOut()
		{
		h.timer=window.setTimeout("menu1.hideAll()",h.timeout)
	};
	function buttonClick()
		{
		this.blur();
		if(h.visible.length)
			{
			h.hideAll()
		}
		else
			{
			var a=new Element(this.id);
			if(a.hasChilds())
				{
				h.show(this.id)
			}
		}
	};
	function itemOver()
		{
		if(h.timer!=false)
			{
			window.clearTimeout(h.timer);
			h.timer=false
		};
		var a=new Element(this.id);
		h.hideHigherOrEqualLevels(a.level);
		if(a.hasChilds())
			{
			h.show(this.id)
		}
	};
	function itemOut()
		{
		var a=new Element(this.id);
		if(!a.hasChilds())
			{
			document.getElementById(this.id).className="item"
		}
		h.timer=window.setTimeout("menu1.hideAll()",h.timeout)
	};
	function itemClick()
		{
		this.blur();
		var a=new Element(this.id);
		h.hideHigherOrEqualLevels(a.level);
		if(a.hasChilds())
			{
			h.show(this.id)
		}
	};
	function Element(a)
		{
		this.getLevel=function()
			{
			var s=this.id.substr(this.menu.id.length);
			return s.substrCount("-")
		};
		this.hasChilds=function()
			{
			return Boolean(document.getElementById(this.id+"-section"))
		};
		if(!a)
			{
			throw"XulMenu.Element(id) failed, id cannot be empty"
		}
		this.menu=h;
		this.id=a;
		this.level=this.getLevel()
	};
	this.id=g;
	var h=this;
	this.tree=new Array();
	this.visible=new Array()
};
if(typeof Array.prototype.contains=="undefined")
	{
	Array.prototype.contains=function(s)
		{
		for(var i=0;
		i<this.length;
		i++)
			{
			if(this[i]===s)
				{
				return true
			}
		};
		return false
	}
};
if(typeof Array.prototype.getLast=="undefined")
	{
	Array.prototype.getLast=function()
		{
		return this[this.length-1]
	}
};
if(typeof String.prototype.substrCount=="undefined")
	{
	String.prototype.substrCount=function(s)
		{
		return this.split(s).length-1
	}
};
