
var Menu = Class.create({
	initialize: function(element, direction) {
		this.element = $(element);
		var liste = this.element.next();
		if(liste !== null && liste.hasClassName('liste')) this.liste = liste; else return false;
		this.element.pos = this.element.positionedOffset();
		this.element.dim = this.element.getDimensions();
		this.parentHeight = this.element.getOffsetParent().getHeight();
		this.direction = direction || 'down' // 'up','down'
		
		this.liste.dim = this.liste.getDimensions();
		this.liste.wrapper = this.liste.select('div')[0];
		this.liste.setStyle({
			position:'absolute',
			left: this.element.pos.left + 'px',
			backgroundColor: this.element.getStyle('background-color'),
			display:'none',
			visibility:'visible'
		});
		
		if(this.direction == 'down') {
			this.liste.setStyle({
				top: (this.element.pos.top + this.element.dim.height) + 'px'
			});
			this.effectDown = Effect.SlideDown;
			this.effectUp = Effect.SlideUp;
		} else {
			this.liste.setStyle({
				bottom: (this.parentHeight - this.element.pos.top) + 'px'
			});
			this.effectDown = Effect.BlindDown;
			this.effectUp = Effect.BlindUp;
		}
		this.timer = null;
		this.state = 'hidden';
		this.element.observe('mouseover', this.show.bind(this));
		this.liste.observe('mouseover', this.show.bind(this));
		this.element.observe('mouseout', this.hide.bind(this));
		this.liste.observe('mouseout', this.hide.bind(this));
	},
	show: function(e) {
		clearTimeout(this.timer);
		if(this.state != 'visible' && this.state != 'showing') this.timer = setTimeout(this.showEffect.bind(this), 10);
		e.stop();
	},
	hide: function(e) {
		clearTimeout(this.timer);
		if(this.state != 'hidden' && this.state != 'hiding') this.timer = setTimeout(this.hideEffect.bind(this), 300);
		e.stop();
	},
	showEffect: function() {
		this.effectDown(this.liste, {
			duration: 0.3,
			beforeStart: function() { this.state = 'showing'; this.hackTraine(); }.bind(this),
			afterFinish: function() { this.state = 'visible'; this.hackTraine(); }.bind(this),
			queue: { position: 'end', scope: this.element.id, limit: 2 }
		});
	},
	hideEffect: function() {
		this.effectUp(this.liste, {
			duration: 0.15,
			beforeStart: function() { this.state = 'hiding'; this.hackTraine(); }.bind(this),
			afterFinish: function() { this.state = 'hidden'; this.hackTraine(); }.bind(this),
			queue: { position: 'end', scope: this.element.id, limit: 2 }
		});
	},
	hackTraine: function() {
		if(this.direction == 'down') this.liste.wrapper.setStyle({bottom:'0'});
	}
});
