var Menu = new Class({
	
	items:new Array(),
	selected:null,
	curtain:null,
	width_connected: 865,
	width_open: 745,
	width_default: 924,
	_home: false,
	
	initialize:function(curtain, _home) {
		if(window.console) console.log('Menu Class Initialized');
		this.curtain = curtain;
		this._home = _home;
		$('menu1').getElements('div 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, new Array(menu_item.gets, menu_item) );
			this.items.push(menu_item);
		}, this);
		
		$('menu2').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, new Array(menu_item.gets, menu_item) );
			this.items.push(menu_item);
		}, this);
		
		
		this.movable = new Movable($('menu1'));
		this.movable.property = 'width';
		this.movable.src = this.width_default;
		this.movable.dest = 235;
		
		this.movable.fx = new Fx.Tween(this.movable.cont, {
			duration: 500,
			link: 'cancel',
			transition:Fx.Transitions.Sine.easeOut
		});
		
	},
	
	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(gets, menu_item, posts, iframe) {
		if(this.curtain != null) menu_item.removeEvents();
		if(this.selected != null) {
			this.selected.minimize();
			this.selected.cont.addEvents({
				mouseenter: this.selected.bound_over,
				mouseleave: this.selected.bound_out
			});
			this.selected.is_select = false;	
		}
		if(this.curtain != null) this.curtain.contenu.fx.removeEvent('complete', this.curtain.contenu.bound);
		if(iframe == null) {
			if(this.curtain != null) this.curtain.contenu.bound = this.curtain.change_type_content.bindWithEvent(this.curtain, new Array( gets, menu_item, posts ) );
		}
		else {
			if(this.curtain != null) this.curtain.contenu.bound = this.curtain.closeIFrame.bindWithEvent(this.curtain, new Array( gets, menu_item, posts ) );
		}
		if(this.curtain != null) this.curtain.contenu.fx.addEvent('complete', this.curtain.contenu.bound);
		
		if(this.curtain != null) this.curtain.contenu.open();
		this.open();
	},
	
	changeMenuItem:function(menu_item) {

		if(this.selected._parent.cont.hasClass('movable')) this.selected._parent.cont.setStyle('float', 'left');
		this.selected = menu_item;
		if(this.selected._parent.cont.hasClass('movable')) this.selected._parent.cont.setStyle('float', 'right');
		menu_item.cont.removeEvents({
			mouseenter: menu_item.bound_over,
			mouseleave: menu_item.bound_out
		});
		menu_item.open();
		menu_item.is_select = true;
		
	},
	
	open:function() {
		this.movable.open();
	},
	
	close:function() {
		this.movable.close();
	}
});
