/*---------------------------------------------------------------------------
 *    Copyright (c) 2009, LiquidPixels, Incorporated. All Rights Reserved.
 *
 *   This file represents Intellectual Property which is proprietary and
 *  confidential to LiquidPixels, Incorporated. Any disclosure or 
 *  reproduction, in whole or in part, is prohibited without express 
 *  written permission.
 *
 *  $Id: $
# -------------------------------------------------------------------------*/

var MenuShade = new Class.create({
	element:			undefined,
	options:			undefined,
	moving:				undefined,
	open:				undefined,
	timer:				undefined,
	untouchedTimer:		undefined,
	state:				undefined,

	
	initialize: function(e, options) {
		MenuShade.status = {
			open:	undefined
		};
		
		this.element = $(e);
		
		var dims = this.element.getDimensions();

		this.options = Object.extend({
			height:		310,
			width:		dims.width
		}, options);
	
		if(this.options.trigger)
			this.attach(this.options.trigger);
			
		this.moving = false;
		this.open = false;
		
		this.element.observe('mouseover', function() {
			clearTimeout(this.untouchedTimer);
			this.clearCloseTimer()
		}.bind(this));
		this.element.observe('mouseout', function() {
			this.setCloseTimer()
		}.bind(this));
	},
	
	
	attach: function(e) {
		$(e).observe('mouseover', function() {
			if(this.state != 'mom') {
				clearTimeout(this.untouchedTimer);
				this.untouchedTimer = setTimeout(function() {
					this.hide();
				}.bind(this), 500);
	
				this.show();
			}
			
			this.state = 'mom';
		}.bind(this));

		$(e).observe('mouseout', function() {
			this.state = '';
		}.bind(this));
},
	
	
	show: function() {
		if(this.moving || this.open) return;

		if(MenuShade.status.open && MenuShade.status.open != this)
			MenuShade.status.open.hide();

		this.clearCloseTimer();

		Effect.BlindDown(this.element, {
			scaleContent: 	false,
			duration: 		0.7,
			scaleMode: 		{
				originalHeight:	this.options.height,
				originalWidth:	this.options.width},
			restoreAfterFinish: false,
			transition: Effect.Transitions.spring,
			afterFinish: 	function() {
				this.moving = false;
				this.open = true;
			}.bind(this),
			afterSetup: 	function() {
				this.element.show();
				this.moving = true;
				MenuShade.status.open = this;
			}.bind(this)
		});		
	},
	
	
	hide: function() {
		if(this.moving || !this.open) return;
			
		Effect.BlindUp(this.element, {
			scaleContent: 	false,
			duration: 		0.5,
			scaleMode: 		{
				originalHeight:	this.options.height,
				originalWidth:	this.options.width},
			restoreAfterFinish: false,
			afterFinish: 	function() {
				this.moving = false;
				this.open = false;
				this.element.hide();
			}.bind(this),
			afterSetup: 	function() {
				this.moving = true;
			}.bind(this)
		});
	},
	
	
	setCloseTimer: function() {
		this.clearCloseTimer();
		this.timer = setTimeout(function() {this.hide()}.bind(this), 500);
	},
	
	
	clearCloseTimer: function() {
		clearTimeout(this.timer);
		this.timer = undefined;
	}
});
