var PopupCover = Class.create({
	initialize: function() {
		
		// Get options
		this.options = Object.extend({
			color:				'#aaaaaa',
			opacity:			0.7,
			zIndex:				50,
			closeCallback:		null
		}, arguments[0] || {} );
		
		// Bind event functions
		this.bonClick = this.hide.bindAsEventListener(this);
		this.bonShow  = this.show.bindAsEventListener(this, 'resize');
		
		// Make the structure
		this.cover = new Element('div', { style: 'display:none;position:absolute;left:0px;top:0px;background-color:' + this.options.color });
		$(document.body).insert(this.cover);
		
		// Attach events
		this.cover.observe('click', this.bonClick);
		Event.observe(window, 'resize', this.bonShow);
	},
	
	show: function(event, resize) {
		
		// Only run a rezise if it was already visible
		if(resize && resize == 'resize' && this.cover.visible() === false) {
			return; }
		
		// Get the window dimensions
		this.windowDim = $(document.body).getDimensions();
		
		// Get the viewport dimensions
		this.viewportDim = document.viewport.getDimensions();
		this.viewportPos = document.viewport.getScrollOffsets();
		
		// Set height from viewport if it is larger
		if(this.viewportDim.height > this.windowDim.height) {
			this.windowDim.height = this.viewportDim.height; }
		
		// Adjust width by viewport offset
		this.windowDim.width += this.viewportPos.left;
		
		// Set styles for the cover and show it
		this.cover.setStyle({zIndex: this.options.zIndex, width: this.windowDim.width + 'px', height: this.windowDim.height + 'px'});
		this.cover.setOpacity(this.options.opacity);
		this.cover.show();
	},
	
	hide: function(event) {
		
		// Hide the cover
		this.cover.hide();
		
		// Run the close callback
		if(event && this.options.closeCallback != null) {
			this.options.closeCallback(); }
	}
});