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;
					
					if (!Browser.Engine.webkit419) {
						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
				});
				
				try {
					clearInterval(btn.menuInterval);
				} catch (e) {}
		
				if (this.subMenu.fx) {
					this.subMenu.get('tween', {
						property: 'height', 
						duration: _self.options.timeShow
					}).start(this.subMenu.orgH);
				} else {
					this.subMenu.setStyle("height", this.subMenu.orgH);
				}

				this.subMenu.getElements("a").each(function(aLink, index) {
					aLink.addEvent("mouseover", function(evt){
						
						new Event(evt).stop();
					
						if (btn.subMenu.fx) {
							this.addClass('submenuhover');
						}
						
						btn.isMouseOver = true;
						btn.addClass(_self.options.overClass);	
						try { 
							clearInterval(btn.menuInterval);
						} catch(e) {}						
					});
					
					aLink.addEvent("mouseout", function(evt){
						new Event(evt).stop();
						
						if (btn.subMenu.fx) {
							this.removeClass('submenuhover');
						}
						
						try {
							clearInterval(btn.menuInterval);
						} catch (e) {}

						btn.menuInterval = setInterval(function() {
							btn.isMouseOver = false;
							_self.hideMenu(btn);
						}, 100);
					});
				});
			});	
			
			btn.addEvent("mouseout", function(evt){
				new Event(evt).stop();
												
				try {
					clearInterval(btn.menuInterval);
				} catch (e) {}

				btn.menuInterval = setInterval(function() {				
					btn.isMouseOver = false;
					_self.hideMenu(btn);
				}, 100);					
			});		
		});
	},
	
	hideMenu: function(menu) {	
		if (!menu.isMouseOver && menu.subMenu) {			
			menu.removeClass(this.options.overClass);
			try {
				clearInterval(menu.menuInterval);
			} catch (e) {}
			
			if (menu.subMenu.fx) {
				menu.subMenu.get('tween', {
					property: 'height', 
					duration: this.options.timeHide
				}).start(0);
			} else {
				menu.subMenu.setStyle("height", 0);
			}
		}
	}
});	

window.addEvent("domready", function(){
	new menu({timeShow: 400, timeHide: 150, overClass: 'current'});
});