var SousMenuItem = new Class({
	
	Extends:MenuItemBase,
	Implements: [Options, Events],
	options:{
		container:null
	},
	
	initialize:function(options) {
		this.setOptions(options);
		debug('SousMenuItem.initialize');

		this.initFxs();
		
		this.initEvents();
		
		this.add_events();
		
		this.is_select = false;
	},
	
	initFxs:function() {
		this.options.container.setStyle('display', 'block');
		
		var ref = !is_home ? $$('.menu_cont')[0] : this.options.container.getParent();
		
		this.move_fx_src = -this.options.container.getSize().x
		
		this.move_fx = new Fxable({
			target: this.options.container,
			type: 'left',
			src: this.move_fx_src,
			dest: this.options.container.getPosition(ref).x,
			transition: Fx.Transitions.Quad.easeOut,
			duration:700
		});
		
		this.options.container.getFirst('a').setStyle('width', this.options.container.getFirst('a').getSize().x - 16);
		this.options.container.getElements('a 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,
			transition: Fx.Transitions.Quad.easeOut
		});
		
		this.out_fx = new Fxable({
			target: this.options.container.getFirst('a .out'),
			type: 'opacity',
			src: 0,
			dest: 1,
			transition: Fx.Transitions.Quad.easeOut
		});
		
		this.over_fx.setClose(),
		
		this.bound_open_menu = this.openMenu.bind(this);
				
	},
	
	initEvents:function() {
		this.bound_over = this.over.bind(this);
		this.bound_out = this.out.bind(this);
		this.bound_click = this.click.bind(this);
		
		this.options.container.addEvent('mouseenter', this.bound_over);
		this.options.container.getFirst('a').addEvent('click', this.bound_click);
	},
	
	initPosition:function() {
		this.setZindex();

		if(this.options.container.hasClass('select')) this.select();

		this.options.container.setStyle('position', 'absolute');

		this.setCloseItem(this.is_select ? true : false);
		
		this.bound_open = this.openComplete.bind(this);
		this.bound_close = this.closeComplete.bind(this);
		
	},
		
	click:function(e) {
		e.stop();
		var link = this.options.container.getFirst('a').get('href');
		obj = getGets(link);
		obj.index = this.options.index;
		obj.link = link;
		this.fireEvent('SousMenuItem:click', obj);
		this.select();
	},
	
	over:function(e) {
		this.fireEvent('SousMenuItem:over');
		this.removeEvent('SousMenuItem:over', this.bound_open_menu);
		this.options.container.removeEvent('mouseenter', this.bound_over);
		this.options.container.addEvent('mouseleave', this.bound_out);
		if(!this.is_select) {
			this.over_fx.open();
			this.out_fx.close();
		}
	},
	
	out:function(e) {
		this.fireEvent('SousMenuItem:out');
		this.options.container.addEvent('mouseenter', this.bound_over);
		this.options.container.removeEvent('mouseleave', this.bound_out);
		if(!this.is_select) {
			this.over_fx.close();
			this.out_fx.open();
		}
	},
	
	closeItem:function(select) {
		if(select != true) this.move_fx.options.src = this.move_fx_src;
		else this.move_fx.options.src = 0;
		this.close();
	},
	
	setCloseItem:function(select) {
		if(select != true) this.move_fx.options.src = this.move_fx_src;
		else this.move_fx.options.src = 0;
		this.setClose();
	},
	
	select:function() {
		debug('SousMenuItem:select index = '+this.options.index);
		this.fireEvent('SousMenuItem:select', {target:this});
		this.setOverable();
		this.is_select = true;
		this.over_fx.open();
		this.out_fx.close();
		this.setZindex(true);
	},
	
	unselect:function() {
		this.removeEvent('SousMenuItem:over', this.bound_open_menu);
		this.is_select = false;
		this.over_fx.close();
		this.out_fx.open();
		this.setZindex();
	},
	
	openMenu:function() {
		debug('SousMenuItem:openMenu');
		this.fireEvent('SousMenuItem:open_menu');
	},
	
	setOverable:function() {
		this.addEvent('SousMenuItem:over', this.bound_open_menu);
	}, 
	
	setZindex:function(is_select) {
		if(is_select) {
			zindex = 300;
		}
		else {
			zindex = 200 + parseInt(this.options.index);
		}
		this.options.container.setStyle('z-index', zindex);
	}
	
});
