var Overlay = {
	'externalElements': new Array(),
	
	'zIndex': 1000,  

	'display': function () {
		var pageSize = Overlay.getPageSize();
	
		var objBody = document.getElementsByTagName("body").item(0);
	
		var objOverlay = document.getElementById('overlay');
		if (objOverlay) {
			objOverlay.style.display = 'block';
		}
		else {
			objOverlay = document.createElement("div");
			objOverlay.setAttribute('id','overlay');
			objOverlay.style.position = 'absolute'; 
			objOverlay.style.top = '0px';
			objOverlay.style.left = '0px';
			objOverlay.style.width = pageSize.width;
			objBody.appendChild(objOverlay);
	
			// Purpose of the iframe is to shadow window elements like flash files
			var frame = document.createElement("iframe");
			objOverlay.appendChild(frame);
			
			frame.style.position = "absolute";
			frame.style.top = '0';
			frame.style.left = '0';
			frame.style.width = pageSize.width;
			frame.style.height = pageSize.height;
			frame.style.opacity = ".6";
			frame.style.filter = "alpha(opacity=60)";
	
			// The actual overlay
			var objOverlayInternal = document.createElement("div");
			objOverlay.appendChild(objOverlayInternal);
			
			objOverlayInternal.onclick = function () { return false;}
			objOverlayInternal.style.position = 'absolute';
			objOverlayInternal.style.top = '0';
			objOverlayInternal.style.left = '0';
			objOverlayInternal.style.zIndex = Overlay.zIndex;
			objOverlayInternal.style.width = pageSize.width;
			objOverlayInternal.style.height = pageSize.height;
			objOverlayInternal.style.background = '#000000';
			objOverlayInternal.style.opacity = ".6";
			objOverlayInternal.style.filter = "alpha(opacity=60)";
			objOverlayInternal.style.cursor = "default";
		}
	},
	
	'hide': function () {
		var objOverlay = document.getElementById('overlay');
		if (objOverlay) {
			objOverlay.style.display = 'none';
			
			// Remove external elements from the overlay
			var removedObj;
			while (Overlay.externalElements.length > 0) {
				removedObj = objOverlay.removeChild(Overlay.externalElements.pop());
				
				// Hide the removed object and place it at the bottom of document body
				removedObj.style.display = 'none';
				document.body.appendChild(removedObj);
			}
		}
	},
	
	// obj: The object to be appended into the overlay
	// top: y-position of obj relative to the visible window
	//		Defaults to 200px.
	// left: x-position of obj relative to the visible window
	//		 If left is not defined, obj will be centered.
	'append': function(obj, top, left) {
		var pageScroll = Overlay.getPageScroll();
		
		// Change position of obj to 'relative' in order to position it relavtively 
		// to its parent DIV which is positioned 'absolute'.
		obj.style.position = 'relative'; 
		obj.style.top = pageScroll.y + (top ? top : 200); 
		if (left) obj.style.left = pageScroll.x + left;
		obj.style.display = 'block';
		obj.style.zIndex = Overlay.zIndex + 1;
		
		var appendObj = obj;
		if (typeof(left) == 'undefined') {
			var center = document.createElement('center');
			center.appendChild(obj);
			
			appendObj = center;
		}

		var objOverlay = document.getElementById('overlay');
		objOverlay.appendChild(appendObj);

		// Keep track of external elements being added onto overlay
		Overlay.externalElements.push(appendObj);		
	}, 
	
	// Returns array with page width, height and window width, height
	'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;
		}
	
		return {'width': pageWidth, 'height': pageHeight, 'windowWidth': windowWidth, 'windowHeight': windowHeight};
	},
	
	'getPageScroll': function (){
		var xScroll, yScroll;
	
		if (self.pageYOffset) {
			xScroll = self.pageXOffset;
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			xScroll = document.documentElement.scrollLeft;
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			xScroll = document.body.scrollLeft;
			yScroll = document.body.scrollTop;
		}
		
		return {'x': xScroll, 'y': yScroll};
	}
}
