var CMenuTop = Class.create();
CMenuTop.prototype = {
	initialize: function() {
		this.submenuLayer = $('submenu');
		this.submenuHolder = $('menu_top_submenu_current');
		this.timeout = 500;
		var obj = this;

		this.processSubmenus($('menu_top_submenus'));
		
		// container events
		Event.observe(obj.submenuLayer, 'mousemove', function(e) {
			if(obj.timer) window.clearTimeout(obj.timer);
		});
		
		Event.observe(obj.submenuLayer, 'mouseout', function(e) {
			obj.wnd = obj.submenuLayer;
			if(obj.timeout) obj.timer = window.setTimeout( function(){
				return function(){
					obj.hideByTimer();
				}
			}(), obj.timeout);
		});
	},//---------------------------------------------------------------------
	
	processSubmenus: function(container) {
		var obj = this;
		container.getElementsBySelector('div.submenu').each( function(elem){
			var arr = elem.id.split('-'),
				mainItem = $('mt-'+arr[1]+'-'+arr[2]);
			obj.mainItemSetEvents(mainItem, elem);
		} );
	},//---------------------------------------------------------------------
	
	mainItemSetEvents: function(switcher, container){
		var obj = this;
		// Switcher events
		Event.observe(switcher, 'mousemove', function(e){
			if(obj.switcher) obj.switcher.className = 'item-with-submenu';
			obj.switcher = switcher;
			switcher.className = 'active';
			obj.submenuHolder.update( container.innerHTML );
			obj.show(e, switcher);
		});
		Event.observe(switcher, 'mouseout', function(e){
			obj.hide(e);
		});
	},//---------------------------------------------------------------------
	
	show: function(e, switcher){
		if(this.wnd) this.hideByTimer();
		if (!this.submenuLayer.visible()) {
			// отображаем меню
			this.submenuHolder.style.left = '0px';
			this.submenuLayer.show();
			// позиционируем меню
			var dx = pageLocation(this.submenuLayer, 'Left'),
				pageWidth = this.submenuLayer.getWidth();
				x = pageLocation(switcher, 'Left') - dx,
				menuWidth = this.submenuHolder.getWidth();
			if( (menuWidth + x) > pageWidth ) x -= menuWidth + x - pageWidth;
			this.submenuHolder.style.left = x + 'px';
		}
	},//---------------------------------------------------------------------
	
	hide: function(e){
		if(this.submenuLayer.visible()) {
			this.wnd = this.submenuLayer;
			if(this.timeout) {
				this.timer = window.setTimeout( function(obj){
					return function(){
						obj.hideByTimer();
					}
				}(this), this.timeout);
			}
		}
	},//---------------------------------------------------------------------
	
	hideByTimer: function() {
		this.wnd.hide();
		this.wnd = null;
		this.switcher.className = 'item-with-submenu';
		if(this.timer) {
			window.clearTimeout(this.timer);
			this.timer = null;
		}
	},//---------------------------------------------------------------------
	
	submenuLayer: null, submenuHolder: null,
	timeout: 0,
	timer: null,		// таймер по кот-му скрывается слой
	switcher: null,		// пункт меню, для которого выведено подменю
	wnd: null			// слой с подменюшками
};
/////////////////////////////////////////////////////////////////////////////

Event.onReady( function(){
	try {
		new CMenuTop();
	}
	catch(e) {}
} );
//---------------------------------------------------------------------------