var MSTNiceMenu = new Class(
{
	initialize:function(menu,options)
	{
	 	Implements: [Options],

		this.setOptions(
		{
			transition: Fx.Transitions.sineInOut,
			duration: 500,
			wait: false,
			onClick: Class.empty,
			onEnterItem: Class.empty,
			onLeaveItem: Class.empty,
			opacity: 1,
			mode: 'move',
			slideOffset: 30,
			itemSelector: 'li.level1',
			activeSelector: 'li.active',
			dropdownSelector: 'div.dropdown',
			link: 'cancel'
		},options);
		if(!$(menu))
			return;
		var index = 0;
		this.menu = $(menu);
		this.items = [];
		this.div = [];
		this.current = this.menu.getElement(this.options.activeSelector);
		this.menu.getElements(this.options.itemSelector).each(function(item,i)
		{
			var leaveevent = item.getElement(this.options.dropdownSelector) ? 'dropdownleave' : 'mouseleave';
			item.addEvent('click',function(event)
			{
				this.clickItem(event, item)
			}.bind(this));
			item.addEvent('mouseenter',function()
			{
				this.mouseenterItem(item, i)
			}.bind(this));
			item.addEvent(leaveevent,function()
			{
				this.mouseleaveItem(item, i)
			}.bind(this));
			if(this.options.mode!='move')
			{
				this.createBackground(i, i+1)
			}
			if(this.options.mode == 'move' && this.current == item)
			{
				index=i
			}
		}.bind(this));
		if(this.options.mode == 'move')
		{
			this.createBackground(0, index+1);
			if(this.current)
			{
				this.setCurrent(this.current)
			}
			else
			{
				var first = this.menu.getElement('li');
				first.addClass('active');
				first.addClass('current');
				this.setCurrent(first)
			}
		}
	},

	createBackground:function(i,bg)
	{
		var css = 'mstnice '+'bg'+bg;
		this.div[i] = new Element('div',{'class':'mstnice-1'}).adopt(new Element('div',{'class':'mstnice-2'}).adopt(new Element('div',{'class':'mstnice-3'})));
		this.div[i].fx = new Fx.Morph(this.div[i], this.options);
		this.items[i] = new Element('div',{'class':css}).adopt(this.div[i]).inject(this.menu, 'inside');
		this.items[i].fx = new Fx.Morph(this.items[i], this.options)
	},

	setCurrent:function(item)
	{
		this.items[0].setStyles({'left':item.offsetLeft,'width':item.offsetWidth,'visibility':'visible','opacity':this.options.opacity});
		this.current=item
	},

	clickItem:function(event,item)
	{
		if(!this.current)
			this.setCurrent(item);
		this.current=item;
		this.options.onClick(new Event(event),item)
	},

	mouseenterItem:function(item,i)
	{
		if(item._fancyactive)
			return;
		item._fancyactive=true;
		switch(this.options.mode)
		{
		case'fade':
			this.fadeFx(item,i,true);
			break;
		case'slide':
			this.slideFx(item,i,true);
			break;
		default:
			this.moveFx(item,0)
		}
		if(this.options.onEnterItem != Class.empty)
			this.fireEvent('onEnterItem',[item,i])
	},

	mouseleaveItem:function(item,i)
	{
		item._fancyactive=false;
		switch(this.options.mode)
		{
		case'fade':
			this.fadeFx(item,i,false);
			break;
		case'slide':
			this.slideFx(item,i,false);
			break;
		default:
			this.moveFx(this.current,0)
		}
		if(this.options.onEnterItem != Class.empty)
			this.fireEvent('onLeaveItem',[item,i])
	},

	moveFx:function(item,i)
	{
		if(!this.current)
			return;
		this.items[i].fx.start({'left':[this.items[i].offsetLeft,item.offsetLeft],'width':[this.items[i].offsetWidth,item.offsetWidth]})
	},

	fadeFx:function(item,i,show)
	{
		if(show)
		{
			this.items[i].fx.setOptions(this.options);
			this.items[i].fx.set({'left':item.offsetLeft,'width':item.offsetWidth});
			this.items[i].fx.start({'opacity':[0,1]})}else{var dur=this.options.duration*2;this.items[i].fx.setOptions({duration:dur});
			this.items[i].fx.start({'opacity':[1,0]})
		}
	},

	slideFx:function(item,i,show)
	{
		var offset=this.options.slideOffset;
		if(show)
		{
			this.items[i].fx.set({'opacity':1,'left':item.offsetLeft,'width':item.offsetWidth});
			this.div[i].fx.set({'margin-top':offset});
			this.div[i].fx.start({'margin-top':[offset,0]})
		}
		else
		{
			this.div[i].fx.set({'margin-top':0});
			this.div[i].fx.start({'margin-top':[0,offset]})
		}
	}
});

MSTNiceMenu.implement(new Options);

