var loadingImage = 'loading.gif';		
var closeButton = 'close.gif';	

function popup_callAHAH(url, pageElement, callMessage)
{
	var req; // we have multiple instances at the same time
	
	document.getElementById(pageElement).innerHTML = callMessage;
	
	try {
		req = new XMLHttpRequest();	/* fire fox */
	} catch(e) {
		try { 
			req = new ActiveXObject("Msxml2.XMLHTTP") /* some versions of IE */
		} catch(e){
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP") /* other versions of IE */
			} catch(E) {
				req = false;
			}
		}
	}
	// test to see it xmlHttp has been created
	if (req==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	req.onreadystatechange = function(){popup_responseAHAH(pageElement, req);}
	req.open("GET", url, true);
	req.send(null);
}

function popup_responseAHAH(pageElement, req)
{
	var output = '';
	
	if(req.readyState == 4) {
		if(req.status == 200)
		{
			//alert(pageElement); debug
			output = req.responseText;
			document.getElementById(pageElement).innerHTML = output;
		}
		if(req.status == 500)
		{
			//alert(pageElement); debug
			output = req.responseText;
			document.getElementById(pageElement).innerHTML = output;
		}
		if(req.status == 404)
		{
			//alert(pageElement); debug
			output = "404 not found";
			document.getElementById(pageElement).innerHTML = output;
		}
	}
}

function popup(x, y, url)
{
	// get the content from the url
	popup_callAHAH(url, "popup_content", "Loading");
	// stop page scrolling
	//window.onscroll=function() { window.scrollTo(0,0) }
	
	// Determine how much the visitor had scrolled
	var scrolledX, scrolledY;
	var arrayPageSize;
			
	if( self.pageYOffset ) 
	{
	  scrolledX = self.pageXOffset;
	  scrolledY = self.pageYOffset;
	} 
	else if( document.documentElement && document.documentElement.scrollTop ) {
	  scrolledX = document.documentElement.scrollLeft;
	  scrolledY = document.documentElement.scrollTop;
	} 
	else if( document.body ) {
	  scrolledX = document.body.scrollLeft;
	  scrolledY = document.body.scrollTop;
	}
	
	// Determine the coordinates of the center of the page
	
	var centerX, centerY;
	
	if( self.innerHeight ) 
	{
	  centerX = self.innerWidth;
	  centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) 
	{
	  centerX = document.documentElement.clientWidth;
	  centerY = document.documentElement.clientHeight;
	} else if( document.body ) 
	{
	  centerX = document.body.clientWidth;
	  centerY = document.body.clientHeight;
	}
	
	// determine the correct center position
	var leftOffset = scrolledX + (centerX - x) / 2;
	var topOffset = scrolledY + (centerY - y) / 2;
	// screen the background
	arrayPageSize = getPageSize();
	document.getElementById("overlay").style.height = (arrayPageSize[1] + 'px');
	document.getElementById("overlay").style.width = (arrayPageSize[0] + 'px');
	document.getElementById("overlay").style.display = "block";
	
	// display the popup
	// set the size of the two inner elements
	document.getElementById("popup_top_bar").style.width = x + "px";
	document.getElementById("popup_content").style.width = x + "px";
	// set the size and location of the main window
	document.getElementById("popup_container").style.width = x + "px";
	document.getElementById("popup_container").style.height = y + "px";
	document.getElementById("popup_container").style.top = topOffset + "px";
	document.getElementById("popup_container").style.left = leftOffset + "px";
	document.getElementById("popup_container").style.display = "block";
	
}
function close_popup()
{
	//window.onscroll=null;
	document.getElementById("overlay").style.display = "none";
	document.getElementById("popup_container").style.display = "none";
}
function getPageSize(){
	
	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;
}
function getPageScroll(){

	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;
}
function initpopup()
{

	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay" class="popup_translucent" style="display: none; background-color: gray; 
	//	width: 100%; height: 100%; position: absolute; left: 0px; top: 0px; z-index: 50000;" onclick="close_popup()">&nbsp;</div>
	//		
	// <div id="popup_container" class="popup_container">
	//	<div id="popup_top_bar" class="popup_top_bar" style="height: 20px; text-align: right;">
	//		<a href="javascript:;" onClick="close_popup(); return false;">CLOSE</a>&nbsp;&nbsp;
	//	</div>
	//	<div id="popup_content" class="popup_content"></div>
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {close_popup(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '50000';
 	objOverlay.style.width = '100%';
	objOverlay.className = "popup_translucent";
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	// create the popup div to display content
	var objpopup_container = document.createElement("div");
	objpopup_container.setAttribute('id','popup_container');
	objpopup_container.style.display = 'none';
	objpopup_container.style.position = 'absolute';
	objpopup_container.style.zIndex = '50001';
	objpopup_container.className = "popup_container";
	objBody.insertBefore(objpopup_container, objBody.firstChild);
	
	// create the popup_top_bar
	var objpopup_top_bar = document.createElement("div");
	objpopup_top_bar.setAttribute('id','popup_top_bar');
	objpopup_top_bar.className = "popup_top_bar";
	objpopup_top_bar.innerHTML = "<a href=\"javascript:;\" onClick=\"close_popup(); return false;\">CLOSE WINDOW</a>&nbsp;&nbsp;";
	objpopup_container.appendChild(objpopup_top_bar);
	
	// create the popup_content
	var objpopup_content = document.createElement("div");
	objpopup_content.setAttribute('id','popup_content');
	objpopup_content.className = "popup_content";
	objpopup_container.appendChild(objpopup_content);
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

addLoadEvent(initpopup);