/**
 * @author Jonathan Schemoul
 */

var menuEffects = new Class({
	initialize: function(selector, options) {
		this.options = Object.extend({
			subElement: false,
			subElementSelector: 'a',
			slideIn: false,
			slideInDelayed: false,
			slideInDelay: 150,
			slideInProperty: 'opacity',
			slideInRange: [0, 1],
			slideOut: false
		}, options || {});
		this.selector = selector;
		this.currTimer = 500;
		this.actives = $A([]);
		this.resetDelay = "";
		if (this.options.slideIn)
		{
			$A($E(selector).childNodes).each(function(el){
				if ($type(el) == 'element')
				{
					el = $(el);
					if (this.options.subElement)
						el = el.getElement(this.options.subElementSelector);
					this.slideIn(el);
				}
			}, this);
		}
		if (this.options.slideOut)
		{
			$ES('a').each(function (element) {
				if (!(element.hasClass('QE_Link') | element.hasClass('download')))
				{
					element.onclick = function() {
						loadLink = function ()
						{
							document.location = this.href;
						}
						mainContentEffect = new Fx.Style($E('.navigation ul'), 'opacity', {
						duration: 150,
						onComplete: loadLink.bind(this)
						});
						mainContentEffect.custom(1, 0);
						return false;
					};
				}
			});
		}
		$E(selector).addEvents({
			'mouseover': function ()
			{
				$clear(this.resetDelay);
			}.bind(this),
			'mouseout': function ()
			{
				this.initResetDelays();
			}.bind(this)
		});
		$ES(selector + ' li ul').each(function(el) {
			elParent = $(el.parentNode);
			
			el.setStyles({
				'display': 'block',
				'opacity': 0
			});
			
			currentMenu = new Fx.Style(el, 'opacity');
			
			if (elParent.hasClass('active'))
			{
				this.actives.extend([currentMenu]);
				this.initResetDelays();
			}
			
			elParent.addEvents({
				'mouseover': function(submenu, myParent) {
					myParent.addClass('hover');
					submenu.clearTimer();
					submenu.element.setStyle('zIndex', 1001);
					submenu.custom(1);
					if (!myParent.hasClass('active'))
						this.actives.each(function(effect) {
							effect.custom(0);
						});
				}.pass([currentMenu, elParent], this),
				'mouseout': function(submenu, myParent) {
					myParent.removeClass('hover');
					submenu.clearTimer();
					submenu.element.setStyle('zIndex', 1000);
					submenu.custom(0);
					this.initResetDelays();
				}.pass([currentMenu, elParent], this)
			})
			
		}.bind(this));
	},
	initResetDelays: function()
	{
		$clear(this.resetDelay);
		this.resetDelay = this.resetState.delay(500, this);
	},
	resetState: function()
	{
		this.actives.each(function(effect) {
			effect.custom(1);
		});
	},
	slideIn: function(el)
	{
		$(el).setStyle(this.options.slideInProperty, this.options.slideInRange[0] + "px");
		//el.setStyle('zIndex', 1);
		var effect = el.effect(this.options.slideInProperty, {duration: 1000});
		if (this.options.slideInDelayed)
		{
			effect.custom.delay(this.currTimer, effect, this.options.slideInRange);
			this.currTimer += this.options.slideInDelay;
		} else
		effect.custom.delay(this.options.slideInDelay, effect, this.options.slideInRange);
	},
	slideOut: function(el)
	{
		var effect = el.effect(this.options.slideOutProperty, {duration: 1000});
		window.addEvent('beforeunload', function (effect) {
			effect.custom.pass(this.options.slideInRange, effect);
			}.pass(effect, this)
		);
	}
});

function processMenuEffects (){
	var myMenus = new menuEffects('ul#topmenu', {
		slideIn: false
	});
}


window.onDomReady(processMenuEffects);