var MainMenu = new Class({
	Implements: [Options],
	container: null,
	items: [],
	options: {
		linksSuffix: '-links',
		duration: 400,
		delay: 1000,
		transition: Fx.Transitions.Sine.easeOut,
		className: 'mootrans',
		readyClassName: 'mootransready',
		defaultClass: 'default',
		baseZi: 1000,
		'mode': 'vertical',
		rollOut: true
	},

	initialize: function(container, options) {
		var first = false;
		this.setOptions(options);
		this.container = $(container);
		this.currentZi = this.options.baseZi;
		this.items = this.container.getChildren();
		this.items.each(function(elem) {
			var links = $(elem.get('id') + this.options.linksSuffix);
			elem.dropDownMenu = links || false;
			elem.hoverLock = 0;
			if (links) {
				var linkFx = new Fx.Slide(links, {'mode': this.options.mode}).hide();
				if (!first && !this.options.rollOut) {
					this.currentElem = elem;
					linkFx.show();
					first = true;
				}
				links.getParent().setStyle('z-index', this.options.baseZi);
				links.setStyle('display', 'block').addEvents({
					'mouseover': function(){
						elem.hoverLock++;
					},
					'mouseout': function(){
						elem.hoverLock--;
						elem.fireEvent('hoverlockchanged', null, elem.dropDownMenu ? this.options.delay : 0);
					}.bind(this)
				});
				elem.addEvents({
					'mouseover': function(){
						linkFx.cancel();
						if (elem.dropDownMenu) {
							elem.dropDownMenu.getParent().setStyle('z-index', this.currentZi++);
						}
						if (!this.options.rollOut && this.currentElem != elem) {
							linkFx.hide();
						}
						this.currentElem = elem;
						linkFx.slideIn();
					}.bind(this),
					'hoverlockchanged': function(){
						elem.hoverLock = Math.max(elem.hoverLock, 0);
						if (elem.hoverLock == 0 && this.options.rollOut) {
							linkFx.cancel();
							linkFx.slideOut();
						}
					}.bind(this)
				});
			}
			if (elem.hasClass(this.options.className)) {
				elem.setStyle('opacity', 0.001);
				new Element('div',{
					'class' : this.options.defaultClass,
					'text' : elem.get('text') || elem.get('value')
				}).setStyle('background-image', 'none').injectBefore(elem).adopt(elem);
				elem.removeClass(this.options.className).addClass(this.options.readyClassName);
				var elemFx = new Fx.Morph(elem, {duration: this.options.duration, transition: this.options.transition});
				elem.addEvents({
					'mouseover' : function(){
						elem.hoverLock++;
						this.items.each(function(itm) {
							if (itm != elem) itm.fireEvent('hoverlockchanged');
						});
						elemFx.cancel();
						elemFx.start({opacity:1});
					}.bind(this),
					'mouseout' : function(){
						elem.hoverLock--;
						elem.fireEvent('hoverlockchanged', null, elem.dropDownMenu ? this.options.delay : 0);
					}.bind(this),
					'hoverlockchanged' : function(){
						if (elem.hoverLock == 0) {
							elemFx.cancel();
							elemFx.start({opacity:0.001});
						}
					}.bind(this)
				});
			}
		}.bind(this));
	}
});