var originalSize = 0;
var addlPreLoad = new Array();
var ajax;

function init(){
	originalSize = getElementHeight("body");
	resize();
	resizeSideNav();
	window.onresize = resize;
	//alert(addlPreLoad.length);
	//for(var i=0; i<addlPreLoad.length; ++i){
		
	//}
	if(addlPreLoad.length > 0){
		var args = "'" + addlPreLoad.join("', '") + "'";
		MM_preloadImages(eval(args));
	}
}

function resize(){
	var pageHeight = getPageHeight();
//	var pageHeight1 = document.getElementsByTagName('BODY')[0].offsetHeight;
	//alert(pageHeight);
	if(pageHeight > 0){
		var mainText = document.getElementById("body");
		var sideNav = document.getElementById("sideMenu");
		
		var otherHeights = getHeights();
		if(mainText != undefined){
			var newHeight = pageHeight - otherHeights - 10;
			if(newHeight != mainText.offsetHeight){
				if(newHeight > originalSize){
					mainText.style.height = newHeight + "px";
					resizeSideNav()
				}
			}
		}
	}
}

function resizeSideNav(){
	var mainText = document.getElementById("body");
	var sideNav = document.getElementById("sideMenu");
	if(mainText != undefined && sideNav != undefined){
		var amt = getElementHeight("footer") - 65;
		//For long pages, when the navigation resizes, the page actually gets longer.
		//Running the nav resize three times ensures that the nav is long enough
		sideNav.style.height = (mainText.offsetHeight - amt) + "px";
		sideNav.style.height = (mainText.offsetHeight - amt) + "px";
		sideNav.style.height = (mainText.offsetHeight - amt) + "px";
		sideNav.style.height = (mainText.offsetHeight - amt) + "px";
		sideNav.style.height = (mainText.offsetHeight - amt) + "px";
	}					
}

function resetAndResize(){
	document.getElementById("body").style.height = "";
	document.getElementById("innerBody").style.height = "";
	document.getElementById("sideMenu").style.height = "";
	originalSize = getElementHeight("body");
	resizeSideNav();
	resize();
}

function getPageHeight(){
	var pageHeight = 0;
	if (self.innerHeight){
		pageHeight = self.innerHeight;
	}else if (document.documentElement && document.documentElement.clientHeight){
		pageHeight = document.documentElement.clientHeight;
	}else if (document.body){
		pageHeight = document.body.clientHeight;
	}
	var bodyHeight = document.getElementsByTagName('BODY')[0].offsetHeight;
	var h = ((bodyHeight > pageHeight) ? bodyHeight : pageHeight);
	return ((h < 900) ? 900 : h);
}
function getHeights(){
	var total = 0;
	total += getElementHeight("header");
	total += getElementHeight("header2");
	total += getElementHeight("topMenu");
	total += getElementHeight("footerEnd");
	return total;
}
function getElementHeight(elementId){
	var obj = document.getElementById(elementId);
	if(obj != undefined){
		return obj.offsetHeight;
	}
	return 0;	
}

function fadeIn(objId){
	theObject = document.getElementById(objId);
	setTimeout(function(){ setOpacity(theObject, 75);theObject=null}, 500);
}
function fadeInObject(objId){
	theObject = document.getElementById(objId);
	setOpacity(theObject, 25);
}
function fadeInObjectRecord(objId, ths){
	fadeInObject(objId);
	saveVar(ths);
}

function saveVar(ths){
	ajax = getAjaxObject();
	ajax.open("GET", "record.php?name=" + ths.name + "&value=" + ths.value);
	ajax.send(null);
}

function setOpacity(theObject, delay){
	theObjectOpacity = (isNaN(theObject.style.opacity) ? 0 : theObject.style.opacity * 100) + 5;
	if(theObjectOpacity <= 100){
		theObject.style.opacity = (theObjectOpacity / 100);      //W3C
		theObject.style.KhtmlOpacity = (theObjectOpacity / 100); //Safari
		theObject.style.MozOpacity = (theObjectOpacity / 100);   //FireFox
		theObject.style.filter = "alpha(opacity=" + theObjectOpacity + ");";	//IE
		setTimeout(function(){ setOpacity(theObject, delay);theObject=null}, delay);
	}
}


function MM_preloadImages() { //v3.0 keep
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0 keep
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01 keep
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0 keep
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var feedBackWindow = null;
function openFeedBackWindow(){
	var w = 500;
	var h = 400;
	lp = (screen.width) ? (screen.width-w)/2 : 0;
	tp = (screen.height) ? (screen.height-h)/2 : 0;
	feedBackWindow = window.open("feedback.php","feedback",'height='+h+',width='+w+',top='+tp+',left='+lp+',toolbar=no,resizable=yes,location=no,menubar=no,directories=no');
}

function getAjaxObject(){
	try{// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	}catch (e){// Internet Explorer
		try{
			return new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				return false;
			}
		}
	}
}

