var whsPopmargin = 2;
var whsPoploading = false;
var tags=new Array(	
	// absolute, top left full size
	Array(-1, "table","id='whsPopOTable'","style='position:absolute; top:0px; left:0px; width:100%; height:100%; border:solid "+whsPopmargin+"px #CC0000; display:none;'","cellpadding='0'","cellspacing='0'", "onClick='whsUnpop();'"),
	Array(0, "tbody", "id='whsPopOBody'"),														//1
	Array(1, "tr", "id='whsPopORow'"),															//2
	// align whatever in the center/middle
	Array(2, "td", "id='whsPopOCell'", "style='text-align:center; vertical-align:middle;'"),	//3
	Array(3, "span", "style='color:#CC0000;'"),													//4
	Array(4, "+", "Click to return"),															//5
	Array(3, "br"),																				//6
	Array(3, "img", "src='images/a_loading.gif'", "id='whsPopLoading'"),						//7
	Array(3, "img", "id='whsPopImage'", "onload='whsImageLoaded();'", "style='position:absolute;top:-9999px; left:-9999px;'"),		// 8 (load the image off left - for IE)
	Array(-1, "table","id='whsPopMTable'","style='position:absolute; top:0px; left:0px; width:100%; height:100%; display:none;'","cellpadding='0'","cellspacing='0'", "onClick='whsUnpop();'"),
	Array(9, "tbody", "id='whsPopMBody'"),														//10
	Array(10, "tr", "id='whsPopMRow'"),															//11
	Array(11, "td", "id='whsPopMCell'", "style='text-align:center; vertical-align:middle;'"),	//12
	Array(12, "img", "src='images/blank.gif'", "id='whsPopMask'"));								//13
	
	
	var whsRimg = 8;					// item to reload
	var whsRParent = "whsPopOCell";		// the parent id of the element to reload

function whsPopinit() {
// load the html elements

	var elem = new Array(tags.length);
	for (var i=0;i<tags.length ;i++ ) {
		elem[i] = whsPopCreate(tags[i]);
		if (tags[i][0] == -1){
			document.body.appendChild(elem[i]);
		} else {
			elem[tags[i][0]].appendChild(elem[i]);
		}
	}		
}

function whsPopCreate(tag) {
	if( typeof( window.innerWidth ) == 'number' ) {	// non IE
		var p, p1, p2;
		var elem;
		if (tag[1] == "+") {
			elem = document.createTextNode(tag[2]);
		} else {
			elem = document.createElement(tag[1]);
			for (var j=2; j<tag.length ;j++ ) {
				p = tag[j].indexOf("=");
				p1 = tag[j].substr(0,p);
				p2 = tag[j].substr((p+1));
				// strip outter quotes 
				p2 = p2.substr(1,(p2.length-2));
				elem.setAttribute(p1, p2);
			}
		}
	} else {				// Do it this way for IE (damnit!)
		var s;
		if (tag[1] == "+") {
			elem = document.createTextNode(tag[2]);
		} else {
			s = "<"+tag[1];
			for (j=2; j<tag.length; j++) {
				s = s+" "+tag[j];
			}
			s = s+">";
			elem = document.createElement(s);
		}
	}
	return elem;
}
function whsImageLoaded() {
	var ip = document.getElementById("whsPopImage");
	var lp = document.getElementById("whsPopLoading");

	if (whsPoploading) {
		var sz = whsGetWindowSize();

		var	mw = sz[0] - (8 * whsPopmargin);
		var mh = sz[1] - (8 * whsPopmargin);
		var rfh = 1.0, rfw = 1.0;

// work out the biggest reduction factor (other dimension is reduced by the same factor)
		if (ip.width > mw)	{
			rfw = mw / ip.width;
		}
		if (ip.height > mh)	{
			rfh = mh / ip.height;
		}
		if (rfh < rfw) {
			ip.height = ip.height * rfh;
		} else {
			ip.width = ip.width * rfw;
		}
		lp.style.display = "none";		// hide the loading image

		ip.style.top = "auto";			// top left position normal
		ip.style.left = "auto";
		ip.style.position = "relative";

		whsPoploading = false;			// and set not loading any more

	}
}

function whsPop(url) {
	// initialise if we need to
	if (!document.getElementById("whsPopOTable")) {
		whsPopinit();
	}

	var limg = document.getElementById("whsPopLoading");			// loading image
	var rimg = document.getElementById("whsPopImage");				// requested image
	var rparent = document.getElementById(whsRParent);				// requested image parent
//	var szdiv = document.getElementById("whsPopDiv");				// the sizing division
	var poptable = document.getElementById("whsPopOTable");			// the popup main element
	var maintable = document.getElementById("whspage");				// the basic page to hide
	var masktable = document.getElementById("whsPopMTable");		// this prevents right click on the image


	// set us in "loading" mode
	whsPoploading = true;

	// get the viewport size & work out yhe max image size
	var sz = whsGetWindowSize();
	var mw = sz[0] - (8 * whsPopmargin);
	var mh = sz[1] - (8 * whsPopmargin);
	
	limg.style.display = "";				// make the loading image visible
	
	rparent.removeChild(rimg);				// remove the image from before
	rimg = whsPopCreate(tags[whsRimg]);		// create a new element
	rparent.appendChild(rimg);					// append it
	
//	rimg.style.display = "none";			// hide the loading image
	
	maintable.style.display = "none";		// hide the base page
	poptable.style.display = "";			// show the image frame
	masktable.style.display = "";
	rimg.src=url;							// load the image
}

function whsUnpop() {
	var poptable = document.getElementById("whsPopOTable");
	var maintable = document.getElementById("whspage");
	var masktable = document.getElementById("whsPopMTable");		// this prevents right click on the image

	poptable.style.display = "none";	// hide the image popup
	masktable.style.display = "none";
	maintable.style.display = "";		// show the base page
}

function whsGetWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var w = new Array();
  w[0] = myWidth;
  w[1] = myHeight;
  return w;
}
