/*
common.js
(C) 2006 Covagre Ltd
http://covagre.co.uk
*/

var Common = {	
	loadingImage: "gfx/loading.gif",
	
	init: function() {
		// create overlay
		this.overlay = document.createElement("div");
		this.overlay.setAttribute("id", "overlay");
		this.overlay.style.display = "none";
		document.body.appendChild(this.overlay);
		
		// create loading progress bar
		this.loadingLink = document.createElement("a");
		this.loadingLink.style.display = "none";
		this.loadingLink.setAttribute("href", "#");
		this.overlay.appendChild(this.loadingLink);
		this.loading = document.createElement("img");
		this.loading.setAttribute("id", "Loading");
		this.loading.setAttribute("src", this.loadingImage);
		this.loadingLink.appendChild(this.loading);
		var img1 = new Image();
		img1.src = "/gfx/loading.gif";
		var img2 = new Image();
		img2.src = "/gfx/overlay.png";
	},
	
	//
	// showOverlay, hideOverlay
	//
	showOverlay: function() {
		this.overlay.style.display = "block";
		// resize the overlay to fit whole screen
		var sizeAndScroll = this.getSizeAndScroll();
		this.overlay.style.width = sizeAndScroll[0]+'px';
		this.overlay.style.height = '720px';
	},
	
	hideOverlay: function() {
		this.overlay.style.display = "none";
	},
	
	//
	// showLoading, hideLoading
	//
	showLoading: function() {
		this.loadingLink.style.display = "block";
		var sizeAndScroll = this.getSizeAndScroll();
		this.loading.style.top = sizeAndScroll[4] + (sizeAndScroll[3]-this.loading.height)/2 + 'px';
		this.loading.style.left = (sizeAndScroll[0] - this.loading.width)/2 + 'px';
	},
	
	hideLoading: function() {
		this.loadingLink.style.display = "none";
		this.loadingLink.onclick = function() { return false; }
	},
	
	//
	// Code from quirksmode.com
	//
	getSizeAndScroll: 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, pageWidth, pageHeight;
		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;
		}
		
		var yScroll2;
		if (self.pageYOffset) {
			yScroll2 = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll2 = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll2 = document.body.scrollTop;
		}
		result = new Array(pageWidth,pageHeight,windowWidth,windowHeight, yScroll2) 
		return result;
	},
	
	onLoadEvent: function(event) {
		var old = window.onload;
		if(typeof(old)!= 'function')
			window.onload = event;
		else
			window.onload = function() { old(); event(); }
	}
}

Common.onLoadEvent(Common.init.bind(Common));

