var menuEffects = new Class({
	Implements: Options,
	options: {
		subElement: false,
		subElementSelector: 'a',
		slideIn: false,
		slideInDelayed: false,
		slideInDelay: 150,
		slideInProperty: 'margin-top',
		slideInRange: ['80', '40'],
		hoverProperty: 'margin-top',
		hoverRange: ['40', '36', '38']
	},
	initialize: function(selector, options) {
		this.setOptions(options);
		this.selector = selector;
		this.currTimer = 500;
		if (this.options.slideIn)
		{
			document.getElement(selector).getChildren().each(function(el){
				if ($type(el) == 'element')
				{
					el = $(el);
					if (this.options.subElement)
						el = el.getElement(this.options.subElementSelector);
					this.slideIn(el);
					this.addSpecialEffects(el);
				}
			}, this);
		}
		if (this.options.slideOut)
		{
			document.getElements('a').each(function (element) {
				if (!(element.hasClass('QE_Link') | element.hasClass('download') | element.hasClass('internal') | element.hasClass('headerlink')))
				{
					element.addEvent('click', function(el) {
						loadLink = function ()
						{
							document.location = this.href;
						}
						mainContentEffect = new Fx.Tween(document.getElement(this.selector), {
							duration: 300,
							onComplete: loadLink.bind(el)
						});
						mainContentEffect.start('opacity', 1, 0);
						return false;
					}.pass(element, this));
				}
			}.bind(this));
		}
		document.getElements(selector + ' li ul').each(function(el) {
			el.setStyles({
				'display': 'block',
				'opacity': 0
			});
			elParent = $(el.parentNode);
			
			currentMenu = new Fx.Tween(el);
			elParent.addEvents({
				'mouseover': function(submenu, myParent) {
					myParent.addClass('hover');
//					submenu.clearTimer();
//					submenu.start('opacity', 1);
					submenu.morph({'opacity': 1});
				}.pass([el, elParent]),
				'mouseout': function(submenu, myParent) {
					myParent.removeClass('hover');
//					submenu.clearTimer();
//					submenu.start('opacity', 0);
					submenu.morph({'opacity': 0});
				}.pass([el, elParent])
			})
			
		}.bind(this));
	},
	slideIn: function(el)
	{
		$(el).setStyle(this.options.slideInProperty, this.options.slideInRange[0] + "px");
		//el.setStyle('zIndex', 1);
//		var effect = new Fx.Tween(el, {transition: Fx.Transitions.elasticOut, duration: 500});
		el.set('tween', {transition: Fx.Transitions.elasticOut, duration: 500});
		if (this.options.slideInDelayed)
		{
			el.tween.delay(this.currTimer, el, [this.options.slideInProperty, this.options.slideInRange[0], this.options.slideInRange[1]]);
			this.currTimer += this.options.slideInDelay;
		} else
		el.tween.delay(this.options.slideInDelay, el, [this.options.slideInProperty, this.options.slideInRange[0], this.options.slideInRange[1]]);
	},
	slideOut: function(el)
	{
		var effect = el.effect(this.options.slideOutProperty, {transition: Fx.Transitions.elasticOut, duration: 1000});
		window.addEvent('beforeunload', function (effect) {
			effect.custom.pass(this.options.slideInRange, effect);
			}.pass(effect, this)
		);
	},
	addSpecialEffects: function(el)
	{
		if (!$(el.parentNode).hasClass('active'))
		{
//			el.setStyles({
//				'borderTopWidth': '0px',
//				'borderTopColor': '#000',
//				'borderTopStyle': 'solid'
//			});
//			currentItem = new Fx.Tween(el, {transition: Fx.Transitions.backOut, duration: 300});
			el.set('tween', {transition: Fx.Transitions.backOut, duration: 300});
			$(el.parentNode).addEvents({
				'mouseenter': function(item) {
					el.tween(this.options.hoverProperty, this.options.hoverRange[1]);
				}.pass(el, this),
				'mouseleave': function(item) {
					el.tween(this.options.hoverProperty, this.options.hoverRange[0]);
				}.pass(el, this)
			});
		} else {
			el.tween.delay(2000, el, [this.options.hoverProperty, this.options.hoverRange[2]]);
		}
	}
});
function processMenuEffects (){
	var myMenus = new menuEffects('#mainmenu', {
		slideIn: true,
		slideInDelayed: true,
		slideOut: true,
		subElement: true
	});
}
window.addEvent('domready', processMenuEffects);

