var RuzafaMenu = Class.create(
{
	node:		false,
	parent:		false,
	header:		false,
	sala:		false,
	lastOpen:	false,
	animating:	false,
	
	initialize: function(node)
	{
		this.header		=	$(document.body).down(".header");
		this.node		=	node;
		this.parent		=	this.node.up();
		this.lastOpen	=	false;
		this.animating	=	false;
		
		this.setupMouseHandler(this.node.select("h3.title a.loaded")); 
		this.defaultOpen();
	},
	
	defaultOpen: function()
	{
		var open = this.node.down(".layer_open");
		if(open){
			var height = this.calculateOpenHeight();
			open.setStyle({height:height+"px"});
			this.startWatcher();
		}
	},
	
	setupSala: function(sala)
	{
		this.sala = sala;
	},
	
	getUnloaded: function()
	{
		return this.node.select("h3.title a.unloaded");
	},
	
	calculateOpenHeight: function()
	{
		var padding			=	70;
		var container		=	$(document.body).down(".contenedor");
		var headerHeight	=	this.header.getDimensions().height + padding;
		var maxHeight		=	container.getDimensions().height - headerHeight;
		var optionHeight	=	container.down(".menu h3").getDimensions().height;
		var menuHeight		=	container.select(".menu h3").length * optionHeight;
		
		return maxHeight - optionHeight - menuHeight;
	},
	
	openPart: function(node)
	{
		if(this.lastOpen) this.closePart(this.lastOpen);
		
		this.animating = true;
		
		var po = this;
		
		var height = this.calculateOpenHeight();
		
		var scrollbar = node.retrieve("scrollbar");
		scrollbar.hide();
		
		if(height > 0){
			node.morph("height:"+height+"px",{
				duration: 1,
				after: function(){
					scrollbar.reset();
					
					node.removeClassName("layer_closed").addClassName("layer_open").setStyle({overflow:""});
					
					po.lastOpen		=	node;
					po.animating	=	false;
					po.startWatcher();
				}
			});
		}
	},
	
	closePart: function(node)
	{
		this.animating = true;
		
		var po = this;
		
		var sb = node.retrieve("scrollbar")
		if(!sb) return;
		
		sb.hide();
		
		node.morph("height:0px",{
			duration: 1, 
			before: function(){
				node.setStyle({overflow:"hidden"});
			},
			after: function(){
				node.removeClassName("layer_open").addClassName("layer_closed");
				po.animating = false;
			}
		});
	},
	
	startWatcher: function()
	{
		var po = this;
		
		Event.observe(document.onresize ? document : window, "resize", function() {
			var node	=	$(document.body).down(".menu .layer_open");
			var height	=	po.calculateOpenHeight();
			
			if(node) node.setStyle({height: height+"px"});
			else po.stopWatcher();
		}); 
	},
	
	stopWatcher: function()
	{
		$(document.body).stopObserving("resize");
	},
	
	setupMouseHandler: function(activeNodes)
	{
		this.lastOpen = this.node.down(".layer_open");
		
		var po = this;
		
		activeNodes.invoke("observe","click",function(event){
			if(!po.animating){
				po.animating = true;
				
				var part = this.up("h3.title").next("div.layer");
				
				//	If sala is open, you need to use that route when opening the menu instead
				if(po.sala.getState()){
					po.lastOpen = part;
					po.sala.hide();
				}else{
					if(part){
						if(part.hasClassName("layer_closed")){
							po.openPart(part);						
						}else{
							po.closePart(part);
						}
					}
				}
			}
			
			event.stop();
			return false;
		});
	},
	
	show: function()
	{
		var po = this;
		this.node.morph("opacity:1",{
			duration: 1,
			before: function(){
				this.node.setStyle({display:"block"});
			}.bind(this),
			after: function(){
				//	if a menu option was open, show it and forget it
				if(this.lastOpen){
					this.openPart(this.lastOpen);
					this.lastOpen = false;
				}	
				
				if(!$(document.body).hasClassName("page_sala")){
					this.header.select("a").invoke("setStyle",{backgroundPosition:"0 100px"});
					//	NOTE:	This is because on IE for some reason morph doesnt work correctly, 
					//			you have to override it especially so it works properly, what a shithouse.....
					if(Prototype.Browser.IE) this.header.select(".normal_logo").invoke("setStyle",{visibility:"visible"});
				}
			}.bind(this)
		});
		
		if(!$(document.body).hasClassName("page_sala")){
			this.header.select(".normal_logo").invoke("morph","opacity:1",2);
		}
	},
	
	hide: function()
	{
		//	if a menu option was open, remember which, and hide it
		this.lastOpen = this.node.down(".layer_open");
		
		if(this.lastOpen) this.closePart(this.lastOpen);
		
		var po = this;
		this.node.morph("opacity:0",{
			duration: 1,
			after: function(){
				this.node.setStyle({display:"none"});
				//	NOTE:	This is because on IE for some reason morph doesnt work correctly, 
				//			you have to override it especially so it works properly, what a shithouse.....
				if(Prototype.Browser.IE) this.header.select(".normal_logo").invoke("setStyle",{visibility:"hidden"});
			}.bind(this)
		});
		
		if(!$(document.body).hasClassName("page_sala")){
			this.header.select("a").invoke("setStyle",{backgroundPosition:"0 0"});
			this.header.select(".normal_logo").invoke("morph","opacity:0",2);
		}
	}
});
