/**
* @package			overlay.js
* @dependence		prototype.js - v1.5.1.1
* @version			0.1.2
* browser test		FF 2.0.0.x and IE 6.0 and 7.0
* @license			GNU GPL 2.0
* @author 			Alvaro A. Lima Jr <alvarolimajr@gmail.com>
* @copyright 		Alvaro Junior! <http://alvarojunior.net>
* FIX BUG : onresize
*/
if(typeof(Prototype) == 'undefined' || parseFloat(Prototype.Version) < 1.5)
	throw("overlay.js requires the Prototype JavaScript framework >= 1.5.0");

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
		// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
}
var Overlay = {};

/**
* Usage :
*  - Overlay.show(options);
*  -- options = {color : '#fffccc', opacity: 0.4};
*/
Overlay = {
	/**
	* @access public
	*/
	version: '0.1.2',

	/**
	* @access protected
	*/
	_isInitialized : false,

	/**
	* document.body
	* @access protected
	*/
	_body : null,

	/**
	* obj div
	* @access protected
	*/
	_overlay : null,

	/**
	* Método construtor
	* @access private
	*/
	__init__: function(options) {
		this._setOptions(options);
		this._createDivOverlay();
		this._resize();
		this._isInitialized = true;
	},

	/**
	* @access protected
	*/
	_setOptions: function(options) {
		this.options = {
			color: '#000',
			opacity: '0.6'
		};
		Object.extend(this.options, options || {});
	},

	/**
	* @access protected
	*/
	_createDivOverlay: function() {
		if(!$('overlay')){
			this._body = document.getElementsByTagName('body').item(0);
			this._overlay = $(document.createElement('div'));
			this._overlay.setAttribute('id','overlay');
			this._overlay.setStyle({
				display : 'none',
				backgroundColor : this.options.color,
				position : 'absolute',
				top : '0px',
				left : '0px'
			});
			this._body.insertBefore(this._overlay, this._body.firstChild);
		} else {
			this._overlay = $('overlay');
		}
	},

	/**
	* @access public
	*/
	show: function(options) {
		if(!this._isInitialized)
			this.__init__(options);

		var pageSize = getPageSize();
		this._overlay.show();
		var w = Prototype.Browser.IE ? pageSize.pageWidth + 'px' : '100%';

		this._overlay.setOpacity(this.options.opacity);
		this._overlay.setStyle({height: pageSize.pageHeight + 'px', width: w, zIndex: '998'});
	},

	/**
	* @access public
	*/
	hide: function() {
		this._overlay.hide();
	},

	/**
	* @access protected
	*/
	_resize: function() {
		window.onresize = function(){
			if(Overlay._overlay.getStyle('display') == 'block')
				Overlay.show();
		}
	}
};
