var menu = {
	
	currentMenu: '',
	timer: null,

	init: function(){
		
		$$('.submenu').each(function(el){
			el.addEvent('mouseover', function(){
				clearTimeout(menu.timer);
			});
			el.addEvent('mouseout', function(){
				menu.currentMenu = this.id;
				menu.timer = setTimeout('menu.hideSubMenuNow()', 1000);
			});
		});
		
		$$('li.menu a').each(function(el){			
			el.addEvent('mouseover', function(){
				var buttonPos = (this.getPosition().x - $('menu').getPosition().x)+6;
								
				clearTimeout(menu.timer);
				var name = this.id.substr(0, this.id.length);
				
				$('sub'+name).setStyle('display', 'block');
				
				var submenuPos = this.getPosition().x - $('sub'+name).getPosition().x - 3;
				
				if (submenuPos + $('sub'+name+'_list').getSize().size.x > 590){
					submenuPos = 590 - $('sub'+name+'_list').getSize().size.x;
				}
				
				$('sub'+name+'_list').setStyle('left', submenuPos+'px');
				menu.hide(this.id);
			});
			
			el.addEvent('mouseout', function(){
				menu.currentMenu = 'sub'+this.id.substr(0, this.id.length);
				menu.timer = setTimeout('menu.hideSubMenuNow()', 200);
			});
		});
	},
	
	hide: function(current){		
		$$('li.menu a').each(function(el){
			var name = el.id.substr(0, el.id.length);
			if(el.id != current){
				$('sub'+name).setStyle('display', 'none');
			}
		});
	},
	
	hideSubMenuNow: function(){
		$(menu.currentMenu).setStyle('display','none');
	}
	
}

window.addEvent('domready', menu.init);