// hg
// provides httpget functionality (also could be known as ajax dialogs)
// Author: Jansen Price (sumpygump)
// Date: 2007-08-15

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createRequestObject();
var type = "dialog";

function hg(url) {
	http.open('get', url, true);
	http.onreadystatechange = handleResponse;
	http.send(null);
}
function hg_l(url,id) {
	//id is the id of the element where we want to put the "loading" gif.
	if(id) {
		showDialog("<div class=\"loading\"><img src=\"img/loading.gif\" /></div>");
		//document.getElementById(id).innerHTML = "<div class=\"loading\"><img src=\"img/loading.gif\" /></div>";
	}
	http.open('get', url, true);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function hg_simple(url) {
	type = "simple";
	http.open('get', url, true);
	http.onreadystatechange = handleResponse;
	http.send(null);
}
function hg_simple_l(url,id) {
	type = "simple";
	//id is the id of the element where we want to put the "loading" gif.
	if(id) {
		document.getElementById(id).innerHTML = "<span class=\"loading\"><img src=\"img/loading.gif\" /></span>";
	}
	http.open('get', url, true);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function send_post_simple_l(url,id,parameters) {
	type = "simple";
	//id is the id of the element where we want to put the "loading" gif.
	if(id != "") {
		document.getElementById(id).innerHTML = "<span class=\"loading\"><img src=\"img/loading.gif\" /></span>";
	}
	http.onreadystatechange = handleResponse;
	http.open('POST', url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", parameters.length);
	http.setRequestHeader("Connection", "close");
	http.send(parameters);
}

//for backwards compatibility
function sndReq(url) {
	http.open('get', url, true);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function handleResponse() {
	if(http.readyState == 4) {
		var response = http.responseText;
		var update = new Array();

		if(response.indexOf('|' != -1)) {
			update = response.split('|');
			if(type=="simple") {
				var objhgElement = document.getElementById(update[0]);
				objhgElement.style.display = 'none';
				objhgElement.innerHTML = update[1];
				if(typeof ( Effect ) != "undefined") { //if scriptaculous effects available.
					new Effect.SlideDown(objhgElement,{duration:.2});
				} 
				else {	
					objhgElement.style.display = 'block';
				}
				//document.getElementById(update[0]).innerHTML = update[1];
			} else {
				showDialog(update[1]);
			}
		}
	}
}

function showDialog(content) {
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objhgDialog = document.getElementById('hg_dialog');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var hwDivisor = 2;
	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	//show the dialog 
	objhgDialog.innerHTML = content;
	if(typeof ( Effect ) != "undefined") { //if scriptaculous effects available.
		new Effect.SlideDown(objhgDialog,{duration:.2});
		hwDivisor = 3.28; //for some reason using these effects messes up the centering commands below.
	} 
	else {	
		objhgDialog.style.display = 'block';
	}
	
	//center the dialog
	var hgDialogTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objhgDialog.clientHeight) / hwDivisor);
	var hgDialogLeft = ((arrayPageSize[0] - 20 - objhgDialog.clientWidth) / hwDivisor);
	objhgDialog.style.top = (hgDialogTop < 0) ? "0px" : hgDialogTop + "px";
	objhgDialog.style.left = (hgDialogLeft < 0) ? "0px" : hgDialogLeft + "px";
	
	//if there is a form element with the id of "focus", set the focus to that element.
	if (document.getElementById('focus')) {
		document.forms[0].focus.focus();
	}
	
	//if there was nothing returned from the httpget, then hide the dialog.
	if(content=="") {
		hideDialog();
	}
}

function hideDialog() {
	// get objects
	objOverlay = document.getElementById('overlay');
	objhgDialog = document.getElementById('hg_dialog');
	// hide lightbox and overlay
	if(typeof ( Effect ) != "undefined") { //if scriptaculous effects available.
		new Effect.SlideUp(objhgDialog,{duration:.4});
	}
	else {
		objhgDialog.style.display = 'none';
	}
	objOverlay.style.display = 'none';
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
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;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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 inithg() {
	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 () {hideDialog(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	// create hg div, same note about styles as above
	var objhgDialog = document.createElement("div");
	objhgDialog.setAttribute('id','hg_dialog');
	objhgDialog.style.display = 'none';
	objhgDialog.style.position = 'absolute';
	objhgDialog.style.zIndex = '100';	
	objBody.insertBefore(objhgDialog, objOverlay.nextSibling);
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(inithg);	// run inithg onLoad