/**************************************************************

	Script		: Overlay
	Version		: 1.2
	Authors		: Samuel birch
	Desc		: Covers the window with a semi-transparent layer.
	Licence		: Open Source MIT Licence

**************************************************************/

var Overlay = new Class({

	'getOptions': function(){
		return {
			'colour': '#FFF',
			'opacity': 0.7,
			'zIndex': 99,
			'container': document.body,
			'onClick': new Class()
		};
	},

	'initialize': function(options){
		this.setOptions(this.getOptions(), options);

		this.options.container = $(this.options.container);

		this.container = new Element('div').setProperty('id', 'OverlayContainer').setStyles({
			'position': 'absolute',
			'left': '0px',
			'top': '0px',
			'width': '100%',
			'visibility': 'hidden',
			'overflow': 'hidden',
			'zIndex': this.options.zIndex
		}).inject(this.options.container);

		this.iframe = new Element('iframe').setProperties({
			'id': 'OverlayIframe',
			'name': 'OverlayIframe',
			'src': '/blank.html',
			'frameborder': 1,
			'scrolling': 'no'
		}).setStyles({
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'width': '100%',
			'height': '100%',
			'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)',
			'opacity': 0,
			'zIndex': 1
		}).inject(this.container);

		this.overlay = new Element('div').setProperty('id', 'Overlay').setStyles({
			'position': 'absolute',
			'left': '0px',
			'top': '0px',
			'width': '100%',
			'height': '100%',
			'zIndex': 99,
			'backgroundColor': this.options.colour
		}).inject(this.container);

		this.container.addEvent('click', function(){
			this.options.onClick();
		}.bind(this));

		this.fade = new Fx.Morph(this.container, 'opacity').set(0);
	},

	'position': function(){
		if(this.options.container == document.body){
			this.container.setStyles({'height': ($(document.body).getSize().y + $(document.body).getScroll().y)+'px'});
		}else{
			var myCoords = this.options.container.getCoordinates();
			this.container.setStyles({
				'top': myCoords.top+'px',
				'height': myCoords.height+'px',
				'left': myCoords.left+'px',
				'width': myCoords.width+'px'
			});
		}
	},

	'show': function(){
		this.position();
		this.fade.start({'opacity': this.options.opacity});

		window.addEvent('scroll', this.position.bind(this));
		window.addEvent('resize', this.position.bind(this));
	},

	'hide': function(){
		this.fade.start({'opacity': 0});

		window.removeEvents('scroll');
		window.removeEvents('resize');
	}

});
Overlay.implement(new Options);

/*************************************************************/

