var Menu = new Class({
	
	items:new Array(),
	selected:null,
	curtain:null,
	
	initialize:function(curtain) {
		this.curtain = curtain;
		$('menu_cat').getElements('li a').each(function(li) {
			var menu_item = new MenuItem(li, this.curtain);
			if(menu_item.is_select) this.selected = menu_item;
			menu_item.clickHandlerCallback = this.menuItemClickHandler.bind(this, menu_item);
			this.items.push(menu_item);
		}, this);
		
		this.movable = new Movable($('menu_cat'));
		this.movable.property = 'right';
		this.movable.src = this.movable.cont.getStyle(this.movable.property);
		this.movable.dest = 0;
		this.movable.cont.getElements('li').each(function(el) {
			if(el.getSize().x > Math.abs(this.movable.dest)) this.movable.dest = - el.getSize().x;
		}, this);
		this.movable.fx = new Fx.Tween(this.movable.cont, {
			link: 'cancel'
		});
	},
	
	getItemById:function(id) {
		for(var i = 0; i < this.items.length; i++) {
			if(this.items[i].cat == id) return this.items[i];
		}
		return false;
	},
	
	menuItemClickHandler:function(menu_item) {
		this.selected.close();
		this.selected.cont.addEvents({
			mouseenter: this.selected.bound_over,
			mouseleave: this.selected.bound_out
		});
		this.selected.is_select = false;
 
		this.selected = menu_item;
		if(!menu_item.is_open) menu_item.open();
		menu_item.cont.removeEvents({
			mouseenter: menu_item.bound_over,
			mouseleave: menu_item.bound_out
		});
		menu_item.is_select = true;
		
		if(this.curtain.diapo_left.is_open) {
		  this.curtain.diapo_left.fx.removeEvent('complete', this.curtain.left.bound);
		  this.curtain.diapo_left.bound = this.curtain.change_type_content.bind(this.curtain, menu_item.cat);
		  this.curtain.diapo_left.fx.addEvent('complete', this.curtain.diapo_left.bound);
		  this.curtain.diapo_left.close();
		  this.curtain.diapo_right.close();
		  $$('.cursor')[0].setStyle('background-image', 'url(/images/cursor_open.gif)');
		}
		else {
		  this.curtain.left.fx.removeEvent('complete', this.curtain.left.bound);
		  this.curtain.left.bound = this.curtain.change_type_content.bind(this.curtain, menu_item.cat);
		  this.curtain.left.fx.addEvent('complete', this.curtain.left.bound);
		  this.curtain.left.open();
		}
	},
	
	open:function() {
		this.movable.open();
	},
	
	close:function() {
		this.movable.close();
	}
});
