var SpringTo = new Class({
	parent : null,
	element : null,
    overlay : null,
	eventPosition : null,
	elementSize : null,
	
	initialize : function(parent,element) {
		this.parent = $(parent);
		this.element = $(element);
		
		if (!this.parent || !this.element) return false;
		
		this.elementSize = this.element.getSize();
		
		this.parent.addEvent('click',this.display.bindWithEvent(this));
		
		this.element.addEvent('mouseenter',this.onMouseEnter.bindWithEvent(this));		
		this.element.addEvent('mouseleave',this.hide.bindWithEvent(this));

		this.position();
		this.element.setStyles({'display' : 'none','opacity' : '0','z-index' : '1000'});
		
		this.overlay = new Element('div').inject(document.body);
		this.overlay.setStyles({'position': 'absolute','left': '0','width': '100%','cursor': 'pointer','opacity' : '0','z-index' : '100'});
		this.overlay.addClass('overlay');
		
		this.eventPosition = this.position.bind(this);
	},
	position : function() {
	    //console.log('resized');		
		var parentPosition = this.parent.getPosition();
		var parentSize = this.parent.getSize();
		
		var top = parentPosition.y + parentSize.y;
		var left = (parentPosition.x + parentSize.x) - this.elementSize.x;
		
		this.element.setStyles({'top' : top + 'px','left' : left + 'px'});
	},
	onMouseEnter : function() {
		this.overlay.removeEvents('click');
	},
	display : function (event) {
	    if (!window.ie) {
			event = new Event(event);
			event.preventDefault();
		}else {
		    event.cancelBubble = true;
		    event.returnValue = false;
		}
		
		this.overlay.addEvent('click',this.hide.bindWithEvent(this));
		this.overlay.setStyles({top: '0px', height: Window.getScrollHeight()+'px'});
		
		var nextEffect = this._displayActualMenu.bind(this);
		
		var fx = new Fx.Morph(this.overlay,{duration: 500, transition: Fx.Transitions.Quart.easeIn});
		fx.addEvent('complete',nextEffect);
			
		fx.start({'opacity': .8});
	},
	_displayActualMenu : function() {		
		window.addEvent('resize', this.eventPosition);

		this.position();
		this.element.setStyles({'display' : 'block'});
		
		var fx = new Fx.Morph(this.element,{duration: 1000, transition: Fx.Transitions.linear});
		fx.start({'opacity': 1});
	},
	hide : function(event) {
	    if (!window.ie) {
			event = new Event(event);
			event.preventDefault();
		}else {
		    event.cancelBubble = true;
		    event.returnValue = false;
		}
		
		var nextEffect = this._hideOverlay.bind(this);
				
		var fx = new Fx.Morph(this.element,{duration: 1000, transition: Fx.Transitions.linear });
		fx.addEvent('complete',nextEffect);
		
		fx.start({'opacity': 0});
		
	},
	_hideOverlay : function() {
		this.element.setStyles({'display' : 'none'});
		
		window.removeEvent('resize', this.eventPosition);
		
	    var fx = new Fx.Morph(this.overlay,{duration: 500, transition: Fx.Transitions.Quart.easeIn });
		fx.start({'opacity': 0});
	}
});

window.addEvent('load',function() {
	var springto = new SpringTo('lnkSpringTo','springto');
});