screen_overlay = {};

ScreenOverlay = Class.create();
ScreenOverlay.prototype = {
	
	initialize: function(options) {
		
		
		
		this.id = 'screen_overlay';
		this.overlay = $(this.id);
	
		var size = this.getPageSize();
		this.overlay.style.width = size[0] + "px";
		this.overlay.style.height = size[1] + "px";
		
		
		//var body = document.getElementsByTagName('body')[0];
		//body.insertBefore(this.overlay, body.firstChild);
		//var div = document.createElement('div');
		//div.id = 'overlay_status';
		
		this.options = options;
		
		//this.overlay.appendChild(div);
		new Effect.Opacity(this.id, {from:0.85,to:0.85});
		
		var s_col = this.options.color || status_color;
		
		this.status = new StatusComponent('overlay_status', s_col);
		this.registry = [];
		
	
	},
	
	getPageScroll: function() {
		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	
	getPageSize: function() {
		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;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 
		return arrayPageSize;	
	},
	
	setSize: function() {
		var size = this.getPageSize();
		this.overlay.style.width = size[0] + "px";
		this.overlay.style.height = size[1] + "px";
	},
	
	show: function() {
		
		if (console) console.log('Overlay Show');
		
		if (this.registry.length) {
			for(var i=0; i<this.registry.length; ++i) {
				this.registry[i].hide(true);	
			}
		}
		
		this.setSize();

		this.resize();
		this.overlay.style.display = '';
		
		this.centerStatus();
		
		this.status.self.style.display = '';
		
		var me = this;
		window.onresize = function() {
			me.resize();
			if (me.parent && me.parent.resize) me.parent.resize();
		}
	
	},
	
	centerStatus: function() {
		
		this.status.self.style.visibility = 'hidden';
		this.status.self.style.display = '';
		
		var pageScroll = ApexUtil.getPageScroll();
		
		var w = this.status.self.offsetWidth;
		var h = this.status.self.offsetHeight;
		
		var left = ((this.arrayPageSize[2] - w) / 2);
		var top = ((this.arrayPageSize[3] - h) / 2);

		//if (console) console.log('page coords: width:'+w+', height:'+h);
		
		this.status.self.style.top = (top < 0) ? "0px" : (pageScroll[1]+top) + "px";
		this.status.self.style.left = (top < 0) ? "0px" : left + "px";
		
		this.status.self.style.display = 'none';
		this.status.self.style.visibility = '';
	},
	
	resize: function() {
		this.arrayPageSize = this.getPageSize();
		this.arrayPageScroll = this.getPageScroll();
		
		if (console) console.log("PAGE SIZE: "+this.arrayPageSize);
		if (console) console.log("PAGE SCROLL: "+this.arrayPageScroll);
	
		// set height of Overlay to take up whole page and show
		this.overlay.style.width = (this.arrayPageSize[0] + 'px');
		this.overlay.style.height = (this.arrayPageSize[1] + 'px');
	},
	
	hide: function() {
		this.overlay.style.display = 'none';
		this.overlay.style.cursor = '';
		if (this.parent) this.parent.hide();	
		//window.onresize = '';
		if (this.registry.length) {
			for(var i=0; i<this.registry.length; ++i) {
				this.registry[i].show(true);	
			}
		}
	
	},
	
	register: function(ob) {
		this.registry.push(ob);	
	}
	
	
	
};