
var Overlay = Class.create();


Overlay.prototype = {
	
	
	initialize: function() {
		
		var objBody = $$('body')[0];
		
		objBody.insert(Element('div', { 'id': 'overlay' }));
		objBody.insert(Element('div', { 'id': 'loader' }));
		
		$('overlay').hide({
			queue: { scope: 'overlay' }
		});
		
		$('loader').hide({
			queue: { scope: 'overlay' }
		});
		
		$('loader').setStyle({
			width: 400+'px',
			height: 250+'px'
		});
		
	},
	
	
	
	start: function(message) {
		
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
		
		// stretch overlay to fill page and fade in
		var arrayPageSize = this.getPageSize();
		
		$('overlay').setStyle({
			width: arrayPageSize[0]+'px',
			height: arrayPageSize[1]+'px'
		});
		
		
		$('overlay').show({
			queue: { scope: 'overlay' }
		});
		
		var arrayPageScroll = document.viewport.getScrollOffsets();
		var loaderTop = arrayPageScroll[1] + (document.viewport.getHeight() / 2);
		
		
		$('loader').insert(message);
		
		$('loader').setStyle({
			top: loaderTop+'px'
		});
		
		$('loader').show({
			queue: { scope: 'overlay' }
		});
		
	},
	
	
	
	end: function() {
		
		$('loader').fade({
			queue: {
				position: 'end',
				scope: 'overlay'
			}
		});
		
		$('overlay').fade({
			queue: {
				position: 'end',
				scope: 'overlay'
			}
		});
		
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
	},
	
	
	
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if(window.innerHeight && window.scrollMaxY) {
			
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
			
		} else if(document.body.scrollHeight > document.body.offsetHeight) {
			
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			
		} else {
			
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
			
		}
		
		var windowWidth, windowHeight;
		
		
		if(self.innerHeight) {
			if(document.documentElement.clientWidth) {
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			
			windowHeight = self.innerHeight;
			
		} else if(document.documentElement && document.documentElement.clientHeight) {
			
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
			
		} else if(document.body) {
			
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
			
		}
		
		
		if(yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		
		return [pageWidth,pageHeight];
	}
}
