if(!$chk(slvr)) {
	var slvr = {};
}
/**
 * slvr.PageOverLay
 *
 * @classDescription slvr.PageOverLay
 */
slvr.PageOverLay = new Class({

	/**
	 * Options
	 * 
	 * @type {Object}
	 */
	options: {
		opacity: 0.75,
		color: '#cccccc',
		onShowComplete: Class.empty,
		onHideComplete: Class.empty,
		onStartShow: Class.empty,
		onStartHide: Class.empty
	},
	
	overlay: null,
	
	isVisible: false,
	
	appearFx: null,
	
	/**
	 * slvr.PageOverLay Constructor
	 * 
	 * @param {Object} options
	 */
	initialize: function(options) {
		this.setOptions(options);
		this._setUpOverlay();
	},
	
	/**
	 * @private
	 */
	_setUpOverlay: function() {
		
		if($chk(this.overlay)) {
			this.overlay.remove();
			this.overlay = null;
		}
		
		this.overlay = new Element('iframe');
		
		this.overlay.setStyles({
								'display': 'none',
								'position': 'absolute',
								'border': 'none',
								'opacity': 0,
								'top': 0,
								'left': 0,
								'backgroundColor': this.options.color,
								'z-index': 900
							   });

		this.overlay.setStyles({
								'width': this._overlayWidth(),
								'height': this._overlayHeight()
							   });

		this.overlay.injectInside(window.document.body);
		
		window.addEvent('resize', this._windowResizeHandler.bindWithEvent(this));

	},
	
	/**
	 * @private
	 */
	_appearFx: function() {
		if(!$chk(this.appearFx)) {
			this.appearFx = new Fx.Morph(this.overlay, {duration: 800, transition: Fx.Transitions.Sine.easeOut});
		}
		return this.appearFx;
	},
	
	/**
	 * @private
	 */
	_overlayWidth: function() {
		return Math.max(window.getScrollSize().x, window.getSize().x);
	},
	
	/**
	 * @private
	 */
	_overlayHeight: function() {
		return Math.max(window.getScrollSize().y, window.getSize().y);		
	},

	/**
	 * @private
	 */
	_windowResizeHandler: function() {
		if(this.isVisible) {
			this.overlay.setStyle('width', this._overlayWidth());
			this.overlay.setStyle('height', this._overlayHeight());
		}
	},
	
	/**
	 * @private
	 */
	_showCompleteHandler: function() {
		this.isVisible = true;
		if(this.options.onShowComplete != Class.empty) {
			this.options.onShowComplete();
		}
	},
	
	/**
	 * @private
	 */
	_hideCompleteHandler: function() {
		this.isVisible = false;
		this.overlay.setStyles({'display': 'none'});
		this.fireEvent('onHideComplete', this);
	},
	
	appear: function(onComplete) {
		this.isVisible = true;
		this._appearFx().cancel();
		this.overlay.setStyles({
								'width': this._overlayWidth(),
								'height': this._overlayHeight(),
								'display': 'block'
							});

		this._appearFx().removeEvents('onComplete');
		this.appearFx.addEvent('onComplete', this._showCompleteHandler.bind(this));
		this.options.onShowComplete = ($chk(onComplete)) ? onComplete : Class.empty;
		this.options.onHideComplete = Class.empty;
	
		this.fireEvent('onStartShow', this);
		this._appearFx().start({
								'opacity': [this.overlay.getStyle('opacity'), this.options.opacity]
							});
	},
	
	disappear: function(onComplete) {
		this.isVisible = false;
		this._appearFx().cancel();
		this._appearFx().removeEvents('onComplete');
		this._appearFx().addEvent('onComplete', this._showCompleteHandler.bind(this));
		
		this.options.onHideComplete = ($chk(onComplete)) ? onComplete : Class.empty;		
		this.options.onShowComplete = Class.empty;
		
		this.fireEvent('onStartHide', this);
		this._appearFx().start({
								'opacity': [this.overlay.getStyle('opacity'), 0]
							});		
	},
	
	toggle: function() {
		if(this.isVisible) {
			this.disappear();
		} else {
			this.appear();
		}
	},
	
	show: function() {
		this._appearFx().cancel();
		this.isVisible = true;
		this.overlay.setStyles({
								'width': this._overlayWidth(),
								'height': this._overlayHeight(),
								'display': 'block',
								'opacity': this.options.opacity
							   });
	},
	
	hide: function() {
		this._appearFx().cancel();
		this.isVisible = false;
		this.overlay.setStyles({
								'display': 'none',
								'opacity': 0
							   });		
	}

});
slvr.PageOverLay.implement(new Options);
slvr.PageOverLay.implement(new Events);

slvr.sharedPageOverLay = function() {
	if(!$chk(slvr.sharedPageOverLayInstance)) {
		slvr.sharedPageOverLayInstance = new slvr.PageOverLay();
	}
	return slvr.sharedPageOverLayInstance;
}
