function kawaiiMenu(id) {
	this.id = id
	this.current = null;
	this.back = null;
	this.tween = null;
	this.menu = null;
	this.target = null;
	
	this.onItemEnter = null;
	this.onItemLeave = null;
	
	this.init = function() {
		//alert("init");
		this.menu = document.getElementById(this.id);
		this.back = document.createElement("div");
		//alert(back);
		this.back.id = "menuBack";
		this.back.style.position = "absolute";
		this.back.style.width = "100px";
		//this.back.style.top = (this.menu.offsetTop+5)+"px";
		//this.back.style.height = (this.menu.offsetHeight-10)+"px"; //"42px";
		this.back.style.backgroundColor = "#006ab2";
		this.back.style.border = "solid 1px #ffffff";
		this.menu.appendChild(this.back);
		
		//alert(members(this.menu));
		var lis = this.menu.getElementsByTagName("LI");
		var li;
		
		if (lis.length > 0) {
			this.back.style.width = "0px";
			this.back.style.height = lis[0].offsetHeight+"px";
			this.back.style.top = posy(lis[0])+"px";
			this.back.style.left = "-1px";			
		}
		for (var i=0;i<lis.length;i++) {
			li = lis[i];
			if (li.className == "current") {
				this.current = this.target = li;
				this.back.style.width = li.offsetWidth+"px";
				this.back.style.height = li.offsetHeight+"px";
				this.back.style.top = posy(li)+"px";
				this.back.style.left = posx(li)+"px";
			}
			li.onmouseover = Pack(this, this.onMouseOver);
			
		}
		
		// chromium
		/*
		var li = this.menu.firstElementChild;
		//alert(li.textContent);
		//alert(li);
		while (li && li.nodeName == "LI") {
			//alert(0);
			if (li.className == "current") {
				this.current = li;
				this.back.style.width = li.offsetWidth+"px";
				this.back.style.left = li.offsetLeft+"px";
			}
			li.onmouseover = Pack(this, this.onMouseOver);
			//li = li.nextElementSibling;
			li = li.nextSibling;
		}
		*/
		
		this.menu.onmouseout = Pack(this, this.onMouseOut);
	}
	
	this.onMouseOver = function(evt) {
		//alert(evt);
		if (!evt) var evt = window.event;
		//alert(members(evt));

		// non ripeto l'evento se non cambio destinazione
		var tg = (window.event) ? evt.srcElement : evt.target;

		if (tg && tg.nodeName != "A") return true;
		var li = tg;
		while (li && li.nodeName != "LI") li = li.parentNode;
		if (!li) return;
		
		//var reltg = (evt.relatedTarget) ? evt.relatedTarget : evt.toElement;
		//if (tg == reltg) return true;
		//alert(reltg);
		this.moveTo(li);
		
		if (this.onItemEnter)
			this.onItemEnter(li);
			
	}

	this.moveTo = function(target) {
		var back = this.back; //document.getElementById("menuBack");
		
		if (target != this.target) {
			if (this.tween)
				this.tween.stop();
			this.tween = new Parallel();
			this.tween.addChild(new Tween(back.style, "left", Tween.backEaseOut, posx(back), posx(target), 0.8, "px"));
			this.tween.addChild(new Tween(back.style, "width", Tween.regularEaseOut, back.offsetWidth, target.offsetWidth, 0.5, "px"));
			this.tween.start();
			
			this.target = target;
		}
		return false;
	}
			
	this.onMouseOut = function(e) {
		// http://www.quirksmode.org/js/events_mouse.html con modifiche
		if (!e) var e = window.event;
		var tg = (window.event) ? e.srcElement : e.target;
		if (tg && tg.nodeName != 'UL') return;
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		while (reltg && reltg != tg )
			reltg= reltg.parentNode
		if (reltg == tg) return;		
		// http://www.quirksmode.org/js/events_mouse.html

		this.backHome();
		if (this.onItemLeave)
			this.onItemLeave();
	}
	
	this.backHome = function() {
		this.moveTo(this.current);
	}

	this.init();
}

function kowaiMenu(id) {
	this.id = id;
	this.menu = null;
	this.submenu=null;
	this.back=null;
	this.tween = null;
	
	this.init = function() {
		this.menu = new kawaiiMenu(id);
		this.menu.onItemEnter = Pack(this, this.onItemEnter);
		this.menu.onItemLeave = Pack(this, this.onItemLeave);
	}
	
	this.removeMenu = function() {
		if (this.submenu) {
			this.menu.menu.removeChild(this.submenu);
			var subUL = this.submenu.getElementsByTagName("UL");
			if (subUL.length > 0) {
				subUL = subUL[0];
				this.submenu.removeChild(subUL);
				subUL.style.display = "none";
				document.body.appendChild(subUL);
				subUL.removeChild(this.back);
			}
			this.submenu = null;
			this.back = null;
		}
	}
	
	this.onItemEnter = function(target) {
		var submenu = this.submenu;
		var back;
		if (submenu) {
			this.removeMenu();
		}
		
		var menuTitle;
		if (target.textContent)
			menuTitle = target.textContent;
		else
			menuTitle = target.innerText;
		
		//alert(menuTitle);
		//alert(escape(menuTitle)==escape("Società"));
		
		subUL = document.getElementById("sub_"+menuTitle);
		if (subUL) {
			this.submenu = submenu = document.createElement("div");
			submenu.className = "submenu";
			submenu.style.position = "absolute";
			//submenu.style.backgroundColor = "#000000";
			submenu.style.width = "auto";
			submenu.style.height = "auto";
			submenu.style.overflow = "hidden";
			submenu.style.left = posx(target)+"px";
			submenu.style.top = (posy(this.menu.menu)+this.menu.menu.offsetHeight-4)+"px";
			submenu.style.border = "solid 1px #ffffff";
			
			this.back = back = document.createElement("div");
			back.style.position = "absolute";
			back.style.width = "100%";
			back.style.height = (target.offsetHeight+2)+"px";
			back.style.top = -(target.offsetHeight+2)+"px";
			back.style.left = "0px";
			back.style.backgroundColor = "#006ab2";
			
			var lis = subUL.getElementsByTagName("LI");
			var li;
			for (var m=0;m<lis.length;m++) {
				li = lis[m];
				a = li.getElementsByTagName("A")[0];
				//alert(li.innerText);
				a.onmouseover = Pack2(this, this.onSubItemOver, target.offsetHeight*m);
			}
			submenu.appendChild(subUL);
			subUL.style.display = "";
			
			subUL.appendChild(back);
			
			this.menu.menu.appendChild(submenu);
			var tween = new Tween(submenu.style, "height", Tween.regularEaseOut, 0, submenu.offsetHeight, 0.5, "px")
			tween.start();
		}

		/*if (menuTitle=="Società" || menuTitle=="Società ") {
			this.submenu = submenu = document.createElement("div");
			submenu.className = "submenu";
			submenu.style.position = "absolute";
			//submenu.style.backgroundColor = "#000000";
			submenu.style.width = "auto";
			submenu.style.height = "auto";
			submenu.style.overflow = "hidden";
			submenu.style.left = posx(target)+"px";
			submenu.style.top = (posy(this.menu.menu)+this.menu.menu.offsetHeight-4)+"px";
			submenu.style.border = "solid 1px #ffffff";
			
			this.back = back = document.createElement("div");
			back.style.position = "absolute";
			back.style.width = "100%";
			back.style.height = (target.offsetHeight+2)+"px";
			back.style.top = -(target.offsetHeight+2)+"px";
			back.style.left = "0px";
			back.style.backgroundColor = "#006ab2";
			
			var ul = document.createElement("ul");
			//ul.className = "submenu";
			for (var i=0;i<4;i++) {
				var li = document.createElement("li");
				var a = document.createElement("a");
				a.href="#";
				a.appendChild(document.createTextNode("menumenu"+i));
				a.onmouseover = Pack2(this, this.onSubItemOver, target.offsetHeight*i); //new Function("back.style.top = '"+(target.offsetHeight*i)+"px';");
				li.appendChild(a);
				
				ul.appendChild(li);
			}
			submenu.appendChild(ul);
			
			ul.appendChild(back);
			
			this.menu.menu.appendChild(submenu);
			var tween = new Tween(submenu.style, "height", Tween.regularEaseOut, 0, submenu.offsetHeight, 0.5, "px")
			tween.start();
		} */
	}
	
	this.onSubItemOver = function(top, evt) {
		//this.back.style.top = top+"px";
		if (this.tween)
			this.tween.stop();
		this.tween = new Tween(this.back.style, "top", Tween.backEaseOut, this.back.offsetTop, top, 0.5, "px");
		this.tween.start();
	}
	
	
	this.onItemLeave = function() {
		if (this.submenu) {
			this.removeMenu();
		}
	}
	
	this.init();
	
}

