function SonataMenu()
{
	this.__setup = function()
	{
		var menu = $("menu");
		var animate = menu.getElementsByClassName("animate");
		
		for(var a=0;a<animate.length;a++){
			var lists = animate[a].parentNode.getElementsByTagName("ul");
			
			for(var l=0;l<lists.length;l++){
				if(animate[a].parentNode == lists[l].parentNode.parentNode){
					animate[a].onclick = this.__animate(lists[l].parentNode,lists[l],true);
				}
			}
			
			if(animate[a].parentNode.className == "selected"){
				var productMenu = $(animate[a].parentNode).getElementsByClassName("productmenu");
				if(productMenu.length == 1 && lists.length == 1){
					productMenu = productMenu[0];
					lists = lists[0];
					lists.style.display = "block";
					
					productMenu.style.display = "block";
					productMenu.style.height = lists.offsetHeight+"px";
					SonataMenu.current = lists;
				}
			}
		}
	}
	
	this.__animate = function(container,list,openMenu)
	{
		var obj = this;
		
		return(function(){
			obj.__deselectMenu();				
			
			if(SonataMenu.current != null){
				var close = obj.__mainloop(SonataMenu.current.parentNode,SonataMenu.current,false);
				close();
			}else{
				var open = obj.__mainloop(container,list,true);
				open();
			}
			
			SonataMenu.current = list;
			
			return false;
		});
	}
	
	this.__deselectMenu = function()
	{
		var collectionMenu = $("collection-menu");
		
		if(collectionMenu){
			var submenu = collectionMenu.getElementsByClassName("submenu");
			if(submenu.length == 1){
				submenu = submenu[0];
				
				for(var item=0;item<submenu.childNodes.length;item++)
				{
					submenu.childNodes[item].className = "not-selected";
				}
			}
		}
	}
	
	this.__mainloop = function(container,list,openMenu)
	{
		var obj = this;
		
		return(function(){
			container.style.display = (container.style.display == "block") ? "block" : "none";
			
			if(openMenu){
				container.style.display = "block";
				container.style.height = "1px";
				container.parentNode.className = "selected";
			}else{
				container.parentNode.className = "not-selected";
			}
			
			var interval = setInterval(function(){
				var step = (openMenu) ? 5 : -5;
					
				if((openMenu == true && container.offsetHeight < list.offsetHeight) || (openMenu == false && container.offsetHeight > -step)){
					var h = parseInt(container.offsetHeight)+step;
					
					container.style.height = h+"px";
				}else{			
					clearInterval(interval);
					
					if(openMenu == false){
						container.style.height = "0px";
						container.style.display = "none";
						
						if(SonataMenu.current && SonataMenu.current != list){
							var func = obj.__mainloop(SonataMenu.current.parentNode,SonataMenu.current,true);
							func();
						}
					}else{
						container.style.height = list.offsetHeight+"px";
					}
				}
			},10);
		});
	}
	
	var obj = this;
	addEvent(window,"load",function(){ obj.__setup(); });
}
SonataMenu.current = null;

var menu = new SonataMenu();