
function initPage() {
	
	// get requested content
	var content = getUrlParameter(document.URL, "content");
	var menu = getUrlParameter(document.URL, "menu");
	if(content == null) content = "overview";
	if(menu == null) menu = "about";
	
	// load header and footer
	sendHtmlRequest("header.html", setDivHtml, "div-header");
	sendHtmlRequest("footer.html", setDivHtml, "div-footer");
	
	// show overview in about menu
	updateSubmenu(menu, false, true);
	showContent(content);
}

/**
 * Show given content
 * and hide all others.
 * @param contentName - name of content div (e.g. resources to identify 'content-resources')
 */
function showContent(contentName) {
	
	// get content div
	var contentDivId = "div-content-" + contentName;
	var contentDiv = document.getElementById(contentDivId);

	// if not loaded yet, trigger load in background
	if(contentDiv.innerHTML.substring(0, 7) == "Loading") {
		sendHtmlRequest("content-" + contentName + ".html", setDivHtml, contentDivId);
	}
	
	// loop over all children of main div
	// and hide all content elements	
	var mainDiv = document.getElementById("div-main");
	for( var index = 0; index < mainDiv.childNodes.length; index++ ) {
		var node = mainDiv.childNodes[index];
		
		// ignore text rows
		// and hide all content divs
		if(node.tagName != null && node.tagName.toLowerCase() == "div") {
			if(node.style.display != "none") node.style.display = "none";
		}
	}
	
	// show single content div
	contentDiv.style.display = "block";
}

/**
 * Callback function which stores the given HTML into the given div.
 * 
 * @param htmlText - HTML to set as content of div.
 * @param divId - id of div to use.
 */
function setDivHtml(htmlText, divId) {
	//alert("Setting HTML of div: " + divId);
	document.getElementById(divId).innerHTML = htmlText;
}


var imgOpen   = new Image(); imgOpen.src   = "images/arrow_open.gif";	
var imgClosed = new Image(); imgClosed.src = "images/arrow.gif";

/**
 * Update the visibility of a submenu.
 * @param submenuName - name of submenu (e.g. 'about').
 * @param toggle - toggle or not.
 * @param show - true to show, false to hide.
 */
function updateSubmenu(menuName, toggle, show) {

	// arrow image
	var img = document.getElementById("img-menu"+menuName);
	if(toggle) {
		if(img.alt == "open") {
			img.src = imgClosed.src;
			img.alt = "closed";
		} else {
			img.src = imgOpen.src;
			img.alt = "open";
		}
	} else {
		if(show) {
			img.src = imgOpen.src;
			img.alt = "open";
		} else {
			img.src = imgClosed.src;
			img.alt = "closed";
		}
	}
	
	// for each child of sidebar list
	var sidebarUl = document.getElementById("ul-sidebar");
	for( var index = 0; index < sidebarUl.childNodes.length; index++ ) {
		var node = sidebarUl.childNodes[index];
		
		// ignore text rows
		// and work on all li elements
		if(node.tagName != null && node.tagName.toLowerCase() == "li") {
			var clazz = node.getAttribute("class");
			
			// type of li is stored in class attribute
			if(clazz != null && clazz.indexOf(menuName) != -1) {
				if(toggle) {
					var style = node.style;
					style.display = (style.display == "block") ? "none" : "block";
					img.src = (img.src == "images/arrow.gif") ? "images/arrow_open.gif" : "images/arrow.gif";
				} else {
					if(show) {
						if(node.style.display != "block") node.style.display = "block";
						if(img.src != "images/arrow_open.gif") img.src = "images/arrow_open.gif";
					} else {
						if(node.style.display != "none") node.style.display = "none";
						if(img.src != "images/arrow.gif") img.src = "images/arrow.gif";
					}
				}
			}
		}
	}
	
	
}






function getBlank (form, stdValue) {
	if (form.value == stdValue){
		form.value = '';
	}
	return true;
}




/* --- Private Stuff ----------------------------------------------------- */

/**
 * Find given parameter in given URL and return its value or null if parameter not found.
 * @return parameter value or null if parameter not found.
 */
function getUrlParameter(url, paramname) {
	var pos = document.URL.indexOf(paramname + "=");
	if(pos != -1) {
		// find beginning and end of parameter value
		var posValueBegin = pos + paramname.length + 1;
		var posAmp = document.URL.indexOf("&", posValueBegin);
		if(posAmp == -1) {
			// no ampersand found after parameter value
			// return everything from the beginning of the parameter value
			return document.URL.substring(posValueBegin);
		} else {
			return document.URL.substring(posValueBegin, posAmp)
		}
	}
	return null;
}

function toggleDiv(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = (style2.display == "none") ? "block":"none";
	} else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	} else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}
