window.onload = init;

function init() {
	toggleMoreInit();
	ajahMenuInit();
}

function toggleMoreInit() {
	var moreText = "";
	var sec = document.getElementById("blog_content");
	var a = sec.getElementsByTagName("a");
	for (c=0; c<a.length; c++) {
		if (a[c].className.match("toggle_more")) {
			a[c].onclick = toggleMore;
		}
	}
}

function ajahMenuInit() {
	var sec = document.getElementById("menu");
	var a = sec.getElementsByTagName("a");
	for (c = 0; c < a.length; c++) {
		var p = a[c].parentNode;
		if (p.className.match("toggle")) {
			a[c].onclick = toggleMenu;
		}
		if (a[c].className.match("toggle")) {
			a[c].onclick = toggleMenu;
		}
	}
}

function toggleMenu() {
	var href = this.getAttribute("href");
	var array1 = href.split("?");
	var args = array1[1];
	var array2 = args.split("&");
	var n = array2.length - 2;
	var array3 = array2[n].split("=");
	var menu = array3[1];
	if (args.match("show")) {
		var where = document.getElementById("menu_"+menu);
		var text = document.createTextNode("loading...");
		var div = document.createElement("div");
		div.appendChild(text);
		div.className = "loading";
		where.appendChild(div);
	}

	var dest = "ajah.php?what=menuToggle&js=y&"+args;
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
		return true;		// no ajax?
	}

	xmlhttp.onreadystatechange = function() {menuTriggered(menu)};
	xmlhttp.open("GET", dest, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(null);

	return false;
}

function menuTriggered(menu) {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var menuText = xmlhttp.responseText;
		var menu_where = "menu_"+menu;
		document.getElementById(menu_where).innerHTML = menuText;
	}
	ajahMenuInit();
}

function toggleMore() {
	var toggle = this.getAttribute("id");
	var entry = toggle.substring(6);
	var moreText = entry+"_more";
	var mtid = document.getElementById(moreText)
	if (mtid.style.display == "block") {
		mtid.style.display = "none";
	} else {
		mtid.style.display = "block";
	}
	if (this.innerHTML == "more") {
		this.innerHTML = "less";
		this.setAttribute("title", "shrink this entry");
	} else {
		this.innerHTML = "more";
		this.setAttribute("title", "read the rest of this entry");
	}
	return false
}
