var MenuItem = new Class({
	
	bound_over:null,
	
	Extends:MenuItemBase,
	Implements: [Options, Events],
	options:{
		container:null
	},
	
	initialize:function(options) {
		this.setOptions(options);
		debug('MenuItem.initialize');
		
		this.options.container.setStyle('display', 'block');
		
		if(this.options.container.getElements('li').length > 0) {
			this.sous_menu = new SousMenu({
				container: this.options.container.getFirst('.submenu')
			});
			this.sous_menu.addEvents({
				'SousMenu:open_menu': function(e) {
					this.fireEvent('MenuItem:open_menu', {index:this.options.index});
				}.bind(this),
				'SousMenu:over': function() {
					this.clearTimer();
				}.bind(this),
				'SousMenu:goto': function(e) {
					this.fireEvent('MenuItem:goto', e);
				}.bind(this)
			});
		}
		
		this.is_select = this.options.container.hasClass('select');

		this.initEvents();
				
		this.initFxs();
		
		if(!is_home) {
			this.initType();
			this.addEvent('openComplete', this.add_events.bind(this));
			this.addEvent('closeComplete', this.remove_events.bind(this));
		}
		else {
			this.initEvents();
			this.add_events();
		}
		
		this.addEvent('close', this.closeSousMenu.bind(this));
				
		this.link = this.options.container.getFirst('a').get('href');
		
	},
	
	initType:function() {		
		this.move_fx = new Fxable({
			target:this.options.container,
			type: 'left',
			src: - this.options.container.getSize().x,
			dest: this.options.container.getCoordinates(this.options.container.getParent()).left,
			duration:700
		});
		if(this.is_select) this.move_fx.options.src = this.move_fx.options.dest;
	},
	
	initPosition:function() {
		if(!is_home) {
			this.options.container.setStyles({
				'position': 'absolute',
				'z-index':'100'
			});
			this.move_fx.setClose();
		}
		
		this.bound_open = this.openComplete.bind(this);
		this.bound_close = this.closeComplete.bind(this);

		if(this.options.container.hasClass('select')) this.select();
	},
	
	initFxs:function() {
		this.options.container.getFirst('a').setStyle('width', this.options.container.getFirst('a').getSize().x - 16);
		this.options.container.getElements('span').each(function(el) { 
			el.setStyle('position', 'absolute');
		});

		this.over_fx = new Fxable({
			target:this.options.container.getFirst('a .over'),
			'type': 'opacity',
			src:0,
			dest:1,
			duration:300
		});
		this.out_fx = new Fxable({
			target:this.options.container.getFirst('a .out'),
			'type': 'opacity',
			src:0,
			dest:1,
			duration:300
		});
		this.select_fx = new Fxable({
			target:this.options.container.getFirst('a .select'),
			'type': 'opacity',
			src:0,
			dest:1,
			duration:300
		});

		this.over_fx.setClose();
		if(!this.is_select) {
			this.select_fx.setClose();
		}
		else {
			this.out_fx.setClose();
		}
		
	},
	
	click:function(e) {
		e.stop();
		if(true) {
			window.location.href = this.link;
		}
		if(!this.is_able) return false;
		this.fireEvent('MenuItem:click', e);
	},
	
	over:function(e) {
		this.clearTimer();
		this.fireEvent('MenuItem:over', {item: this});
		this.options.container.getFirst('a').removeEvent('mouseenter', this.bound_over);
		this.options.container.addEvent('mouseleave', this.bound_out);
		if(!this.is_select) {
			if(!is_home) {
				this.over_fx.open();
			}
			else {
				this.select_fx.open();
			}
			this.out_fx.close();
		}
		if(this.sous_menu) this.sous_menu.open();
	},
	
	out:function(e) {
		this.fireEvent('MenuItem:out', {item: this});
		this.options.container.getFirst('a').addEvent('mouseenter', this.bound_over);
		this.options.container.removeEvent('mouseleave', this.bound_out);
		if(!is_home) {
			this.timer = this.outDelay.delay(2000, this);
		}
		else {
			this.outDelay();
		}
	},
	
	outDelay:function() {
		this.fireEvent('MenuItem:outDelay', {item: this});
		
		this.clearTimer();
		
		if(!this.is_select) {
			if(!is_home) {
				this.over_fx.close();
			}
			else {
				this.select_fx.close();
			}
			this.out_fx.open();
		}
		if(this.sous_menu) this.sous_menu.close();
	},
	
	clearTimer:function() {
		if(typeof this.timer == "number") {
			clearTimeout(this.timer);
		}
	},
	
	closeSousMenu:function() {
		this.clearTimer();
		if(this.sous_menu) this.sous_menu.close(true);
	}
	/*
	select:function() {
		debug('SousMenu:select index = '+this.options.index);
		this.fireEvent('MenuItem:select', {target:this});
		this.is_select = true;
		this.opacity_fx.options.src = 1;
	},
	
	unselect:function() {
		this.is_select = false;
		this.opacity_fx.options.src = 0;
	}
	*/
	
});
