$ = function(id) { return document.getElementById(id); }

if(typeof addEvent == "undefined")
	addEvent = function(o, evType, f, capture) {
		if(o == null) return false;
		if(o.addEventListener) {
			o.addEventListener(evType, f, capture);
			return true;
		} else if (o.attachEvent) {
			var r = o.attachEvent("on" + evType, f);
			return r;
		} else {
			try{ o["on" + evType] = f; }catch(e){}
		}
	};

if(typeof removeEvent == "undefined")
	removeEvent = function(o, evType, f, capture) {
		if(o == null) return false;
		if(o.removeEventListener) {
			o.removeEventListener(evType, f, capture);
			return true;
		} else if (o.detachEvent) {
			o.detachEvent("on" + evType, f);
		} else {
			try{ o["on" + evType] = function(){}; }catch(e){}
		}
	};


function Menu(ul, parent, opener) {
	this.ul=ul;
	this.opener=opener;
	this.children=new Array();

	if (parent) {
		parent.addChild(this);
	}

	this.ul.onmouseover=this.over;
	this.ul.onmouseout=this.hideAll;
	this.ul.menuItem=this;
}

Menu.prototype.hideAll = function(ev) {
	var target;
	if (ev) target=window.event.target; else target=window.event.srcElement;
	while (target!=null && (target.tagName==null || target.tagName.toLowerCase()!="li")) {
		target=target.parentNode;
	}
	if (target!=null) target.className="";

	this.menuItem.RootMenu().timer();
}

Menu.prototype.timer = function() {
	if (this.clearer!=null) clearTimeout(this.clearer);
//	log("set timeout");
	this.clearer=setTimeout("document.getElementById('" + this.ul.id + "').menuItem.hideAllAllAll();", 400);
}

Menu.prototype.resetTimeout = function() {
	if (this.clearer!=null) {
//		log("clear timeout");
		clearTimeout(this.clearer);
	}
	this.clearer=null;
}

Menu.prototype.addChild = function(child) {
	if (child.parent) return;
	this.children[this.children.length]=child;
	child.parent=this;
	child.ul.id=this.ul.id + ":" + this.children.length;
	log("addChild[" + this.children.length + "] : " + this.ul.id + "  <--  " + child.ul.id);
}

Menu.prototype.over = function(ev) {
	this.menuItem.RootMenu().resetTimeout();

	var target;
	if (ev) window.event=ev;
	if (ev) target=window.event.target; else target=window.event.srcElement;

	while (target!=null && (target.tagName==null || target.tagName.toLowerCase()!="li")) {
		target=target.parentNode;
	}

	if (target==null || target.tagName.toLowerCase()!="li") return true;

	this.menuItem.hideOther();
	this.menuItem.showMenu();

	//document.getElementById("log").innerHTML+="<p>Over: " + this.tagName + "(" + this.id + ") " + target.childNodes.length + "\n" + window.event + "\n" + target.tagName + "\n" + target.innerHTML.replace(/</ig, "&lt;") + "</p>";

	for (var i=0; i<target.childNodes.length; i++) {
		//alert(target.childNodes[i]);

		x=target.childNodes[i];
		if (x.tagName && x.tagName.toLowerCase()=="ul") {
			if (!x.menuItem) x.menuItem=new Menu(x, this.menuItem, target);
			x.menuItem.showMenu();
//			x.style.display="block";
//			x.onmouseout=kiviout;
//			x.onmouseover=kiviover;
		}
	}
	target.className="over";

	window.event.cancelBubble=true;
	return false;
}

Menu.prototype.out = function(ev) {
	var target;
	if (ev) window.event=ev;
	if (ev) target=window.event.target; else target=window.event.srcElement;

	while (target.tagName==null || target.tagName.toLowerCase()!="li") {
		target=target.parentNode;
	}

	if (target.tagName.toLowerCase()!="li") return true;
	target.className="";

//	document.getElementById("log").innerHTML+="<p>Out: " + this.tagName + "(" + this.id + ") " + target.className + " " + target.childNodes.length + "\n" + window.event + "\n" + target.tagName + "\n" + target.innerHTML.replace(/</ig, "&lt;") + "</p>";

	this.menuItem.hideMenu();

	window.event.cancelBubble=true;
	return false;
}

Menu.prototype.RootMenu = function() {
	var r=this;
	while (r.parent!=null)
		r=r.parent;
	return r;
}

Menu.prototype.showMenu = function() {
	if (this.parent) this.parent.hideOther();

	log("showMenu: " + this.ul.id + " " + (this.opener==null ? "null" : this.opener.className));
	this.ul.style.display="block";

	/*if(document.all)
	{
		if(!this.ul.iframe)
		{
			var iframe=document.createElement("IFRAME");

			iframe.src="";
			iframe.width=this.ul.clientWidth+2;
			iframe.height=this.ul.clientHeight+1;

			iframe.frameBorder=0;

			iframe.style.overflow="hidden";
			iframe.style.border="solid 0px";
			iframe.style.position="absolute";
			iframe.style.top=getStyle(this.ul,"top");
			iframe.style.left=getStyle(this.ul,"left");
			iframe.style.backgroundColor="Red";
			iframe.style.zIndex="-1";


			//document.body.appendChild(iframe);
			this.ul.parentNode.appendChild(iframe);
			this.ul.iframe=iframe;


			//this.ul.style.display="none";
		}
		else
			this.ul.iframe.style.display="block";
	}*/


	if (this.opener!=null) this.opener.className="path";
}

Menu.prototype.hideMenu = function() {
	this.ul.style.display="none";
	if(this.ul.iframe!=null)
		this.ul.iframe.style.display="none";
	if (this.opener!=null) this.opener.className="";
}

Menu.prototype.hide = function() {
	this.hideOther();
	this.hideMenu();
}


Menu.prototype.hideAllAllAll = function() {
	log("hideAllAllAll : " + this.ul.id + " children[" + this.children.length + "]");
	for (var i=0; i<this.children.length; i++) {
		this.children[i].hideAllAllAll();
		this.children[i].hideMenu();
	}
}


Menu.prototype.hideOther = function() {
	for (var i=0; i<this.children.length; i++)
		this.children[i].hideMenu();
}

function getStyle(x,styleProp)
{
	var x;
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function log(t) {
//	document.getElementById("log").innerHTML+="<p>" + t + "</p>";
}
