var menutext = "";
var mainmenudiv;

function drawMenu() {
	preloadImages();
	buildMenu();
	mainmenudiv = document.getElementById("mainmenu");
	mainmenudiv.innerHTML = menutext;
	openCurrentPageDiv();
	mainmenudiv.style.display = "";	
}

var menuoff;
var menuon;

function preloadImages() {
	menuoff = new Image();
	menuon = new Image();

	menuoff.src = "../images/menu-back.gif";
	menuon.src = "../images/menu-backselected.gif";
}	

function openCurrentPageDiv() {
	var atags = document.getElementById("mainmenu").getElementsByTagName("A");
	var i
	var currenthref = window.location.href;
	for (i=0; i < atags.length; i++) {
		if (atags[i].href == currenthref) {
		// need to open all divs from parent
			atags[i].className = "cmenuitemselected";
			parentdiv = atags[i].parentNode.parentNode;
			while (parentdiv != mainmenudiv) {
				showdivsindiv(parentdiv, true);
				parentdiv = parentdiv.parentNode;
			}
		}
	}	
}

var menus = 0;
var items = 0;

var indentlevel = 0;
var indentpx = 10;

function debug(x) {
	document.getElementById("debugBox").innerHTML += ":: " + x + "<br />";
}

function output(sometext) {
	menutext += sometext + "\n";
}

function menu(title) {
	if (title) {
		strindent = "margin-left: " + ((indentlevel > 0) ? indentpx : "0") + "px";
		strdisplay = (indentlevel > 0) ? "; display: none" : "";
		strsub = (indentlevel > 0) ? "sub" : "";

		var winstatustitle = title.replace(/'/g,"\\\'").replace(/"/g,"\\\'\\\'");

		var a = "<a href=\"javascript:void(0)\" onmouseover=\"window.status='" + winstatustitle + "'; return true;\" " +
			   "onmouseout=\"window.status=''; return true;\" class=\"c" + strsub + "menuheader\" " +
			   "onclick=\"toggleMenu(" + menus + "); window.status='" + winstatustitle + "'; return false;\">";

		output("<div class='cmenublock' style='" + strindent + strdisplay +
			   "' id='chmenu" + menus + "'>");
		output(a + title + "</a>");
		menus++;
		indentlevel++;
	} else {
		indentlevel--;
		output("</div>");
	}
}

function item(title, href, target) {
	var strtarget = "";
	if (target) {
		strtarget = " target=\"" + target + "\"";
	}
	strindent = " margin-left: " + ((indentlevel > 0) ? indentpx : "0") + "px";
	strdisplay = (indentlevel > 0) ? "display: none;" : "";

	winstatustitle = title.replace(/'/g,"\\\'").replace(/"/g,"\\\'\\\'");

	var a = "<a class=\"cmenuitem\" onmouseover=\"window.status='" + winstatustitle + "'; return true;\" " +
		"onmouseout=\"window.status=''; return true;\" href=\"" + href + "\" target=" + strtarget + ">";


	output("<div style=\"" + strdisplay + strindent + "\" id=\"chitem" + items + "\">");

	output(a);

	output("<img border=\"0\" src=\"../images/arrow-right.gif\"> " + title + "</a></div>");

	items++;
}

function showdivsindiv(o, show) {
	// shows or hides all divs within the element "o"
	var f = o.getElementsByTagName("A")[0];
	if (f.className == "cmenuheader" + (show ? "" : "selected")) {
		f.className = "cmenuheader" + (show ? "selected" : "");
	}
	if (f.className == "csubmenuheader" + (show ? "" : "selected")) {
		f.className = "csubmenuheader" + (show ? "selected" : "");
	}
	
	o.className = "cmenublock" + (show ? "selected" : "");
	children = o.childNodes;
	for (var j = 0; j < children.length; j++) {
		if (children[j].tagName == "DIV") {
			children[j].style.display = (show ? "block" : "none");
		}
	}
}

function toggleMenu(id) {
	var menudiv = document.getElementById("chmenu" + id);
	if (menudiv.getElementsByTagName("A")[0].className == "cmenuheaderselected" || menudiv.getElementsByTagName("A")[0].className == "csubmenuheaderselected") {
		showdivsindiv(menudiv, false);
	} else {
		showdivsindiv(menudiv, true);
	}
}

