/**************************************************************

	Script		: Overlay
	Version		: 1.2
	Author		: Samuel birch
	Modified	: Danny Hui
	Desc		: Covers the window with a semi-transparent layer.
	Licence		: Open Source MIT Licence

**************************************************************/

var Overlay = new Class({
	
	getOptions : function(){
		return {
			colour: '#000',
			duration: 500,
			opacity: 0.7,
			zIndex: 1,
			container: document.body,
			onClick : Class.empty,
			onCompleteIn : Class.empty,
			onCompleteOut : Class.empty
		};
	},

	initialize : function(options){
		this.setOptions(this.getOptions(), options);
		this.options.container = $(this.options.container);
	},
	
	position : function () { 
		if(this.options.container == document.body){ 
			var h = window.getScrollHeight()+'px'; 
			this.overlay.setStyles({top: '0px', height: h}); 
		}else{ 
			var myCoords = this.options.container.getCoordinates(); 
			this.overlay.setStyles({
				top: myCoords.top+'px', 
				height: myCoords.height+'px', 
				left: myCoords.left+'px', 
				width: myCoords.width+'px'
			}); 
		} 
	},
	
	show : function () {
		this.overlay = new Element('div').setProperty('id', 'overlay').setStyles({
			position: 'absolute',
			left: '0px',
			top: '0px',
			width: '100%',
			zIndex: this.options.zIndex,
			backgroundColor: this.options.colour
		}).injectInside(this.options.container);
		
		this.overlay.addEvent('click', function(){
			this.options.onClick();
			this.hide();
		}.bind(this));
		
		this.fade = new Fx.Style(this.overlay, 'opacity', { duration: this.options.duration }).set(0);
		
		this.fade.addEvent('onComplete', function() {
			this.oncomplete();
		}.bind(this));
		
		this.position();
		
		this.fade.start(0,this.options.opacity);
	},
	
	hide : function () {
		this.fade.start(this.options.opacity,0 );
	},
	
	oncomplete : function () {
		el = $(this.overlay);
		if (el.style.opacity == 0) {
			window.removeEvent('resize', this.position.bind(this));
			el.remove();
			this.options.onCompleteOut();
		} else {
			window.addEvent('resize', this.position.bind(this));
			this.options.onCompleteIn();
		}
	}
	
});
Overlay.implement(new Options);

/*************************************************************/
