// JavaScript Document

function toggleAll() {
	var theLink = document.getElementById("theToggle");
	var theDescriptions = document.getElementsByTagName("div");
	var theHeadings = document.getElementsByTagName("h3");
	if (theLink.firstChild.data == "Expand all") {
		theLink.firstChild.data = "Contract all";
		for (var i=0; i < theDescriptions.length; i++) {
			if (theDescriptions[i].getAttribute("id").indexOf("ch") != -1) {
				theDescriptions[i].style.display = "block";
			}
		}
		/*for (i = 0; i < theHeadings.length; i++) {
			if (theHeadings[i].getAttribute("class") == "chapter") {
				theHeadings[i].style.backgroundImage = "url(/images/downarrow.gif)";
			}
		}*/
	} else {
		theLink.firstChild.data = "Expand all";
		for (var i=0; i < theDescriptions.length; i++) {
			if (theDescriptions[i].getAttribute("id").indexOf("ch") != -1) {
				theDescriptions[i].style.display = "none";
			}
		}
		/*for (i = 0; i < theHeadings.length; i++) {
			if (theHeadings[i].getAttribute("class") == "chapter") {
				theHeadings[i].style.backgroundImage = "url(/images/rtarrow.gif)";
			}
		}*/
	}
}

function toggleSection(id, heading) {
	var theSection = document.getElementById(id);
	var theHeading = document.getElementById(heading);
	if (theSection.style.display == "block") {
		theSection.style.display = "none";
		//theHeading.style.backgroundImage = "url(/images/rtarrow.gif)";
	} else {
		theSection.style.display = "block";
		//theHeading.style.backgroundImage = "url(/images/downarrow.gif)";
	}
}

function setToggleFuncs() {
	var sectionId, titleID;
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("maincontent")) return false;
	var mainHead = document.getElementById("mainHead");
	var newPara = document.createElement("p");
	var theLink = document.createElement("a");
	theLink.setAttribute("href", null);
	theLink.setAttribute("id", "theToggle");
	var theLinkText = document.createTextNode("Expand all");
	theLink.appendChild(theLinkText);
	newPara.appendChild(theLink);
	var theText = document.createTextNode("Click the chapter headings to see what each chapter contains. ");
	newPara.insertBefore(theText, theLink);
	var theParent = mainHead.parentNode;
	var nextOne = mainHead.nextSibling;
	theParent.insertBefore(newPara, nextOne);
	var theToggleLink = document.getElementById("theToggle");
	theToggleLink.onclick = function() {
		toggleAll();
		return false;
	}
	var toc = document.getElementById("maincontent");
	var links = toc.getElementsByTagName("a");
	for (var i=0; i<links.length;i++) {
		sectionId = links[i].getAttribute("href").split("#")[1];
		titleId = sectionId+"Title";
		if (!document.getElementById(sectionId)) continue;
		if (sectionId == "ch01") {
			document.getElementById(sectionId).style.display = "block";
			//document.getElementById(titleId).style.backgroundImage = "url(/images/downarrow.gif)";
		} else {
			document.getElementById(sectionId).style.display = "none";
		}
		links[i].destination = sectionId;
		links[i].heading = titleId;
		links[i].onclick = function() {
			toggleSection(this.destination, this.heading);
			return false;	
		}
	}
}
setToggleFuncs();