var Menu = new Class({
	
	Extends:MenuBase,
	Implements: [Options, Events],
	options:{
		container:null
	},
	
	initialize:function(options) {
		this.setOptions(options);
		debug('Menu.initialize');
		
		this.current_item = null;
		
		this.initOthers();
		
		this.initItems();
				
	},
	
	initOthers:function() {
		if($$('.other_menu').length > 0) {
			this.other_container = $$('.other_menu')[0];
			this.bound_over_other = this.overOther.bind(this);
			this.bound_out_other = this.outOther.bind(this);
			this.other_container.addEvent('mouseenter', this.bound_over_other);
			
			if(this.other_container.getFirst('li').hasClass('select')) {
				this.select_item = new MenuItem({
					container:this.other_container.getFirst('li'),
					index:-1,
					is_select: true
				});
				this.other_container.setStyles({
					position:'absolute',
					'z-index': 300
				});
				this.select_item.addEvents({
					'MenuItem:over': function(e) {
						if(this.current_item) this.current_item.outDelay();
						if(this.select_item) this.select_item.outDelay();
						this.current_item = e.item;
					}.bind(this),
					'MenuItem:goto': function(e) {
						debug('MenuItem:goto');
						this.fireEvent('Menu:goto', e);
					}.bind(this),
					'MenuItem:open_menu': function(e) {
						this.overOther(e.index);
					}.bind(this)
				});
			}
			
			this.other_fx = new Fxable({
				target:this.other_container,
				'type': 'margin-left',
				dest: 0,
				src: -this.other_container.getSize().x,
				duration:700
			});
			
			if(this.select_item) this.other_fx.options.src = this.other_fx.options.dest;
			
			this.options.container.setStyles({
				'position': 'absolute',
				'z-index': 100
			});
			
			this.arrow_container = this.other_container.getFirst('.arrow');
			this.arrow_container.getFirst('a').setStyle('width', this.arrow_container.getFirst('a').getSize().x - 16);
			this.arrow_out = this.arrow_container.getFirst('a .out');
			this.arrow_over = this.arrow_container.getFirst('a .over');
			this.arrow_over.setStyle('display', 'block');
			this.arrow_container.getElements('span').each(function(el) {
				el.setStyle('position', 'absolute');
			});
			this.arrow_over_fx = new Fxable({
				target:this.arrow_over,
				'type': 'opacity',
				src:0,
				dest:1,
				duration:300
			});
			this.arrow_out_fx = new Fxable({
				target:this.arrow_out,
				'type': 'opacity',
				src:0,
				dest:1,
				duration:300
			});
			this.arrow_over_fx.setClose();
		}
		
		this.options.container.setStyle('left', 0);
	},
	
	initItems:function() {
		this.items = new Array();
		this.options.container.getElements('> li').each(function(el, index) {
			it = new MenuItem({
				container:el,
				index:index
			});
			it.addEvents({
				'MenuItem:over': function(e) {
					if(this.current_item) this.current_item.outDelay();
					if(this.select_item) this.select_item.outDelay();
					this.current_item = e.item;
				}.bind(this),
				'MenuItem:open_menu': function(e) {
					this.overOther(e.index);
				}.bind(this),
				'MenuItem:select': function(e) {
					//this.select_item = e.target;
				}.bind(this)
			});
			if(this.select_item) it.move_fx.options.dest += this.other_container.getSize().x;
			this.items.push(it);
		}, this);
		

		for( var i = 0; i < this.items.length; i++) {
			this.items[i].initPosition();
			this.items[i].options.container.setStyle("z-index", 100 + parseInt(this.items.length - i));
		}
		
	},
	
	overOther:function(index) {
		if(typeof index != "number") index = 0;
		if(this.select_item && this.select_item.sous_menu) this.select_item.sous_menu.select_item.closeItem();
		this.other_container.removeEvent('mouseenter', this.bound_over_other);
		$$('.menu_cont')[0].addEvent('mouseleave', this.bound_out_other);
		if(!this.select_item) this.items[index].over();
		else {
			this.select_item.over();
		}
		this.open();
		this.otherClose();
		this.arrow_over_fx.open();
		this.arrow_out_fx.close();
	},
	
	outOther:function() {
		if(this.select_item && this.select_item.sous_menu) this.select_item.sous_menu.select_item.setOverable();
		this.other_container.addEvent('mouseenter', this.bound_over_other);
		$$('.menu_cont')[0].removeEvent('mouseleave', this.bound_out_other);
		this.close();
		if(this.select_item && this.select_item.sous_menu) this.select_item.sous_menu.close(true);
		this.otherOpen();
		this.arrow_over_fx.close();
		this.arrow_out_fx.open();
	},
	
	otherOpen:function() {
		this.other_fx.open();
	},
	
	otherClose:function() {
		this.other_fx.close();
	},
	
	open:function() {
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].open();
		}
	},
	
	close:function() {
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].close();
		}
	},
	
	goSelect:function(e) {
		debug('Menu:goSelect');
		this.select_item.sous_menu.goSelect(e);
	}
	
});
