var menu = new Class({
	
	Implements: Options,
	
	options: {
		timeShow: 400,
		timeHide: 400,
		overClass: null
	},
	
	initialize: function(options) {		
		if (menu.hasCreated) {
			return;
		}
		
		menu.hasCreated = true;
		
		this.setOptions(options);
		this.initMenu();
	},
	
	initMenu: function() {		
		var _self = this;
		
		$$("a.menuBtn").each(function(btn, i){											
			btn.addEvent("mouseover", function(evt){
				new Event(evt).stop();
				this.isMouseOver = true;
				
				if (!this.subMenu) {
					this.subMenu = this.getNext().clone().inject($$("body")[0]);			
					this.subMenu.orgH = this.subMenu.getCoordinates().height;
					this.subMenu.fx = new Fx.Tween(this.subMenu).set('height', 0);
					this.subMenu.subUL = this.subMenu.getElements("ul");
				}			
				
				this.subMenu.setStyles({
					left: this.getPosition().x + 10,
					opacity: 0.9
				});
				
				clearInterval(this.menuInterval);				
				this.subMenu.get('tween', {
					property: 'height', 
					duration: _self.options.timeShow
				}).start(this.subMenu.orgH);
				this.subMenu.getElements("a").setStyle('opacity', 1);
				this.subMenu.getElements("a").addEvents({
					"mouseover": function(evt){
						new Event(evt).stop();
					
						btn.isMouseOver = true;
						btn.addClass(_self.options.overClass);	
						
						try { 
							clearInterval(btn.menuInterval);
						} catch(e) {}						
					},
					
					"mouseout": function(evt){
						new Event(evt).stop();
						
						try {
							clearInterval(btn.menuInterval);
							btn.menuInterval = setInterval(function() {
								btn.isMouseOver = false;
								_self.hideMenu(btn);
							}, 100);
						} catch(e) {}
					}
				});				
			});
			
			btn.addEvent("mouseout", function(evt){
				new Event(evt).stop();
								
				clearInterval(this.menuInterval);
				this.menuInterval = setInterval(function() {				
					btn.isMouseOver = false;
					_self.hideMenu(btn);
				}, 100);					
			});		
		});
	},
	
	hideMenu: function(menu) {	
		if (!menu.isMouseOver && menu.subMenu) {			
			menu.removeClass(this.options.overClass);
			clearInterval(menu.menuInterval);
							
			var subOpenedMenu = menu.subMenu;
			subOpenedMenu.get('tween', {
				property: 'height', 
				duration: this.options.timeHide
			}).start(0);
				
			subOpenedMenu.get('tween').addEvent('complete', function() {
				menu.subMenu.destroy();
				menu.subMenu = null;
			});
		}
	}
});	

window.addEvent("domready", function(){
	new menu({timeShow: 400, timeHide: 150, overClass: 'current2'});
});
