
var gapFilled = false;
var isXVisible = false;
var xmlHttp;
var updateDiv;
var pageToGoBack;
var backPageSize;

function fillGap() {
	//useful in the onload and onresize handlers for a page to keep
	//the yellow gap continuous across a very wide page
	if((document.getElementById("main").offsetLeft > 300) || gapFilled) {
		gapFilled = true;
		document.getElementById("filler").style.width = (document.getElementById("main").offsetLeft + 5) + "px";
	}
}

function goBack() {
	hideX();
	var prevSize = backPageSize;
	loadPage(pageToGoBack, updateDiv, false, '');
	document.getElementById("bottom_left").style.top = prevSize;
	document.getElementById("bottom_right").style.top = prevSize;
}

function hideX() {
	//hide the x graphic
	document.getElementById("back_x").style.display = "none";
	isXVisible = false;
	adjustContentDiv();
}

function showX() {
	//show the x graphic
	var x_style = document.getElementById("back_x").style;
	x_style.display = "block";
	isXVisible = true;
	adjustContentDiv();
}

function shortBox(size) {
	//move the bottom brackets up
	if(size < 200) {
		size = 200;
	}
	if(size > 524) {
		size = 524;
	}
	document.getElementById("bottom_left").style.top = (size - 13) + "px";
	document.getElementById("bottom_right").style.top = (size - 13) + "px";
}

function longBox() {
	//move the bottom brackets to their max distance down
	document.getElementById("bottom_left").style.top = "509px";
	document.getElementById("bottom_right").style.top = "509px";
}

function adjustContentDiv() {
	//adjust the scrollbars/padding/etc of the content div after its content
	//has been changed
	cont_div = document.getElementById("content");
	if(cont_div == null) {
		return;
	}
	if(cont_div.scrollHeight > cont_div.offsetHeight) {
		// have a scroll bar
		cont_div.style.overflowY = "auto";
		cont_div.style.overflowX = "hidden";
	}
	if(isXVisible) {
		cont_div.style.paddingRight = "31px";
		cont_div.style.width="491px";
		var x_style = document.getElementById("back_x").style;
		if(cont_div.scrollHeight > cont_div.offsetHeight) {
			x_style.left = "898px";
		} else {
			x_style.left = "913px";
		}
	} else {
		cont_div.style.paddingRight = "10px";
		cont_div.style.width="512px";
	}
}

function getXmlHttp() {
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// no async support
				return null;
			}
		}
	}
	return xmlHttp;
}

function loadPage(page, divname, shouldShowX, backPage, pageSize) {
	updateDiv = divname;
	pageToGoBack = backPage;
	backPageSize = document.getElementById("bottom_left").style.top;
	if(shouldShowX) showX();
	if(pageSize == undefined) {
		longBox();
	} else {
		shortBox(pageSize);
	}
	xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange = updatePage;
	xmlHttp.open("GET", page, true);
	xmlHttp.send(null);
}

function updatePage() {
	if(xmlHttp.readyState == 4) {
		var toUpd = document.getElementById(updateDiv);
		if(toUpd == null) {
			// this shouldn't happen
			return;
		}
		toUpd.innerHTML = xmlHttp.responseText;
	}
}
