var CollectionsMenu = Class.create({
	initialize: function(element) {
		this.element = $(element);
		if (!this.element) return;

		this.options = Object.extend({
			delay: 0.3,
			duration: {
				show: 0.23,
				hide: 0.2
			},
			zIndex: 300
		}, arguments[1] || {});
		
		this.prepare();
	},
	
	prepare: function() {
		// IE needs help
		this.element.setStyle({
			zoom: 1,
			zIndex: this.options.zIndex
		});
		
		this.shift = this.element.down('.shift');
		this.element.observe('mousemove', this.show.bindAsEventListener(this))
		.observe(Lindavalkeman.useEvent['mouseleave'], Lindavalkeman.capture(this.hide).bindAsEventListener(this));
		
		this.link = $(this.options.link);
		this.link.observe(Lindavalkeman.useEvent['mouseenter'], Lindavalkeman.capture(this.show).bindAsEventListener(this))
		.observe(Lindavalkeman.useEvent['mouseleave'], Lindavalkeman.capture(this.hide).bindAsEventListener(this))
		.observe('click', function(event) { event.stop(); });
	},
	
	toggle: function(event) {
		this.element.toggle();
		return false;
	},
	
	clearHideTimer: function() {
		if (this._hideTimer) {
			window.clearTimeout(this._hideTimer);
			this._hideTimer = null;
		}
	},
	
	hide: function(event) {
		this.clearHideTimer();
		this._hideTimer = window.setTimeout(function() {
			if (this._effect) this._effect.cancel();
			this._effect = new Effect.Morph(this.shift, { style: {
					left: '-195px'
				},
				afterFinish: function() {
					this.element.hide();
					this.shift.setStyle({ left: '195px' });
				}.bind(this),
				duration: this.options.duration.hide
			});
			
		}.bind(this), this.options.delay * 1000);
	},

	show: function(event) {
		this.clearHideTimer();
		if (this._effect) this._effect.cancel();
		this.element.show();
		this._effect = new Effect.Morph(this.shift, { style: {
			left: '0px'
		}, duration: this.options.duration.show }); 
	}
});

document.observe('dom:loaded', function() {
	window.CollectionsMenu = new CollectionsMenu('collectionsMenu', { link: 'collectionsMenuLink' });
});
