/* Global functions/objects ................................................................................................ */

/* Element shortcuts */
function $(id){ return document.getElementById(id) }
function $show(id){ $(id).style.display = ''; }
function $hide(id){ $(id).style.display = 'none'; }
function $showhide(id){ $(id).style.display = ($(id).style.display == 'none') ? '' : 'none'; }


/* Onload object */
function objOnload()
{
	this.aFunc = [];
	this._run = function(){
		for(i=0;i<this.aFunc.length;i++){
			this.aFunc[i]._init();
		}
	}
}
oLoad = new objOnload();


/* xmlhttp object */
var xmlhttp;
function createXMLHttp(){
	xmlhttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}


/* xmlhttp post to specified URL with querystring then run handler when ready. */
function ajaxPagePost(url, qs, handle){
	createXMLHttp();
	xmlhttp.open("POST", url, true);
	xmlhttp.onreadystatechange = handle;
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(qs);
}


/* Simple xmlhttp request - grabs html from a page and puts it in specified element. */
function simpleAjaxGet(url, id){
	ajaxPagePost(url, "", simpleAjaxHandle);
	return false;

	function simpleAjaxHandle(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				$(id).innerHTML = xmlhttp.responseText;
			}
		}
	}
}


/* Page & Screen dimensions object [ihart 20/11/06] */
function objBrDimensions()
{
	dE = document.documentElement;
	dB = document.body;

	/* Dimensions of viewport (includes scrollbars, if any) */
	this.viewportW = (self.innerWidth) ? self.innerWidth : ((dE && dE.clientWidth) ? dE.clientWidth : dB.clientWidth);
	this.viewportH = (self.innerHeight) ? self.innerHeight : ((dE && dE.clientHeight) ? dE.clientHeight : dB.clientHeight);	

	/* Dimensions of full page */
	this.pageW = (dB.scrollWidth > dB.offsetWidth) ? dB.scrollWidth : dB.offsetWidth;
	this.pageH = (dE && dE.scrollHeight > dE.offsetHeight) ? dE.scrollHeight : ((dB.scrollHeight > dB.offsetHeight) ? dB.scrollHeight : dB.offsetHeight);

	/* Scrolling offset */
	this.pageScrollX = (self.pageXOffset) ? self.pageXOffset : ((dE && dE.scrollLeft) ? dE.scrollLeft : dB.scrollLeft);
	this.pageScrollY = (self.pageYOffset) ? self.pageYOffset : ((dE && dE.scrollTop) ? dE.scrollTop : dB.scrollTop);

	/* Window Position (relative to screen resolution minus taskbars etc) */
	this.windowX = (self.screenLeft) ? self.screenLeft : self.screenX;
	this.windowY = (self.screenTop) ? self.screenTop : self.screenY;

	/* Screen resolution */
	this.screenW = self.screen.width;
	this.screenH = self.screen.height;

	/* Screen resolution minus taskbars etc */
	this.screenAvailW = self.screen.availWidth;
	this.screenAvailH = self.screen.availHeight;
}


/* Opaque Overlay Object */
function objOverlay(oID, cID, cCol, oBG, oOpac, imgUrl)
{
	this.oID = oID;
	this.cID = cID;
	this.cCol = cCol;
	this.oBG = oBG;
	this.oOpac = oOpac;
	this.imgId = "overlayLoading";
	this.imgUrl = imgUrl;

	this.init();
}

objOverlay.prototype.init = function()
{
	myBrDim = new objBrDimensions; // Create objBrDimensions Object.

	if(!$(this.oID)){

		// Create the overlay.
		eD = document.createElement("div");
		eD.setAttribute("id", this.oID);
		// Set the styles.
		eDs = eD.style; eDs.display = "none"; eDs.width = "100%"; eDs.height = myBrDim.pageH + "px"; eDs.position = "absolute"; eDs.top = "0px"; eDs.left = "0px"; eDs.zIndex = 1000; eDs.opacity = this.oOpac/100; eDs.filter = 'alpha(opacity='+this.oOpac+')'; eDs.background = this.oBG;
		document.body.appendChild(eD);
	
		// Create the container on top of the overlay.
		if(this.cCol != ""){
			// Why use a table? For pain-free vertical alignment!
			eT = document.createElement("table");
			eB = document.createElement("tbody"); // Need <tbody> for IE!
			eR = document.createElement("tr");
			eC = document.createElement("td");
			eT.setAttribute("id", this.cID);
			eC.setAttribute("id", this.cCol);
			// Set the styles.
			eTs = eT.style; eTs.display = "none"; eTs.width = "100%"; eTs.height = myBrDim.viewportH + "px"; eTs.position = "absolute"; eTs.top = "0px"; eTs.left = "0px"; eTs.zIndex = 1010; eTs.textAlign = "center"; eTs.borderCollapse = "collapse";
			eR.appendChild(eC);
			eB.appendChild(eR);
			eT.appendChild(eB);	
			document.body.appendChild(eT);
		}else{
			// If you aren't bothered about vertical alignment, just use a div.
			eD = document.createElement("div");
			eD.setAttribute("id", this.cID);
			// Set the styles.
			eDs = eD.style; eTs.display = "none"; eDs.width = "100%"; eDs.position = "absolute"; eDs.top = "0px"; eDs.left = "0px"; eDs.zIndex = 1010;
			document.body.appendChild(eD);
		}
	
		// Create loading image.
		this.createLoading();

	}
}

objOverlay.prototype.createLoading = function()
{
	eI = document.createElement("img");
	eI.setAttribute("src", this.imgUrl);
	eI.setAttribute("id", this.imgId);
	$(this.cCol).appendChild(eI);
}

objOverlay.prototype.setPosition = function()
{
	myBrDim = new objBrDimensions; // Create objBrDimensions Object.

	$(this.oID).style.height = myBrDim.pageH + "px";
	$(this.cID).style.height = myBrDim.viewportH + "px";

	// Only scroll if we haven't reached the bottom of the page (otherwise the page will grow taller).
	if(myBrDim.viewportH+myBrDim.pageScrollY < myBrDim.pageH){
		$(this.cID).style.top = myBrDim.pageScrollY + "px";
	}
}

objOverlay.prototype.openPage = function(url, qs, autopos)
{
	o = this;
	cCol = this.cCol;
	
	$show(this.oID);
	$show(this.cID);

	o.setPosition();

	if(autopos == true){
		window.onscroll = function(){o.setPosition()};
		window.onresize = function(){o.setPosition()};
	}

	ajaxPagePost(url, qs, ajaxHandle);

	function ajaxHandle(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				$(cCol).innerHTML = xmlhttp.responseText;
				if(autopos == true) o.setPosition();
			}
		}
	}
}

objOverlay.prototype.closePage = function()
{
	$hide(this.oID);
	$hide(this.cID);
	$(this.cCol).innerHTML = "";
	this.createLoading();
}


