/*

This is the Modularity Engine A, by Charlie Loyd <char@rheme.net>
for Panharmonicon Design (http://panharmonicon.com). 

*/

function boot() {	eachtangent('mkhinges');	mkmenu();	eachtangent('mkmenuli');}function eachtangent(state) {	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++) {
		var div = divs[i];
		if (div.className == "tangent") {			switch (state) {
			case "hide": turn(div.id, "off"); break;			case "show": turn(div.id, "on"); break;
			case "mkhinges": mkhinges(div); break;
			case "mkmenuli": mkmenuli(div); break;			}
		}
	}}

function mkhinges(div) {
	div.style.display = "none";

	var dp = div.parentNode;
	var df = div.firstChild;
	var dl = div.lastChild;
	var shortdesc = df.title.toLowerCase();
	var longdesc = df.firstChild.nodeValue.toLowerCase();

	dp.insertBefore(mkhingeblurb(div, longdesc, "show"), div);
	div.appendChild(mkhingeblurb(div, longdesc, "hide"));
}

function mkhingeblurb(div, desc, action) {
	state = "off"; o = "›"; c = "‹";
	if (action == "show")
		{ state = "on"; o = "‹"; c = "›"; }
	a = document.createElement("a");
	a.href = "javascript:turn('" + div.id + "','" + state + "');";
	a.appendChild(document.createTextNode(o + action + " " + desc + c));
	a.style.border = "none";
	a.style.fontWeight = "bold";
//	a.style.color = "rgb(50, 60, 70)";	a.className = "hideshowtext";	p = document.createElement("p");
	p.className = "hinge";
	p.id = action + "-" + div.id;
	p.appendChild(a);	return(p);
}

function mkmenuli(div) {	var menu = document.getElementById("menu");
	var li = document.createElement("li");

	ahide = document.createElement("a");
	ahide.id = "menu-hide-" + div.id;
	ahide.href = "javascript:turn('" + div.id + "','off')";
	ahide.style.display = "none";
	hidetext = document.createTextNode("hide");
	ahide.appendChild(hidetext);
	li.appendChild(ahide);

	ashow = document.createElement("a");
	ashow.href = "javascript:turn('" + div.id + "','on')";
	ashow.id = "menu-show-" + div.id;
	showtext = document.createTextNode("show");
	ashow.appendChild(showtext);
	li.appendChild(ashow);

	li.appendChild(document.createTextNode(" ")); // ...
	agoto = document.createElement("a");
	gototext = document.createTextNode(div.firstChild.title);
	agoto.appendChild(gototext);
	agoto.onclick = function() { turn(div.id,"on"); }
	agoto.href = "#" + div.id;
	li.appendChild(agoto);
	menu.appendChild(li);}
function turn(id, action) {
	var thang = document.getElementById(id);

	if (action == "off") {		
		var kids = thang.childNodes;
		for (var i = 0; i < kids.length; i++) {
			var kid = kids[i];
			if (kid.className == "tangent"
			&& kid.tagName.toLowerCase() == "div")
				turn(kid.id, "off");
		}
		truth = "block";
		beauty = "none";
		love = "inline";
		cleanliness = "none";
	} else if (action == "on") {
		if (thang.parentNode.className == "tangent"
		&& thang.parentNode.tagName.toLowerCase() == "div")
			turn(thang.parentNode.id, "on");
		truth = "none";
		beauty = "block";
		love = "none"
		cleanliness = "inline";
	} else { return false; }

	thang.style.display = beauty;
	var show = document.getElementById("show-" + thang.id);
	show.style.display = truth;
	var hide = document.getElementById("hide-" + thang.id);
	hide.style.display = beauty; // never matters
	var menushow = document.getElementById("menu-show-" + thang.id);
	menushow.style.display = love;
	var menuhide = document.getElementById("menu-hide-" + thang.id);
	menuhide.style.display = cleanliness;}function mkmenu() {	menulist = document.createElement("ul");	menulist.id = "menu";	hideall = document.createElement("li");	hidealla = document.createElement("a");	hidealla.href = "javascript:eachtangent('hide');";	hidealltext = document.createTextNode("Just the facts, ma’am.");	hidealla.appendChild(hidealltext);	hideall.appendChild(hidealla);	hideall.className = "menu-head";	menulist.appendChild(hideall);	showall = document.createElement("li");	showalla = document.createElement("a");	showalla.href = "javascript:eachtangent('show');";	showalltext = document.createTextNode("Tell me everything!");	showalla.appendChild(showalltext);	showall.appendChild(showalla);	showall.className = "menu-head";	menulist.appendChild(showall);	document.getElementsByTagName("body").item(0).appendChild(menulist);}
