/*
Chris Holland Outline Toolbox
This is a work in progress

For more info:
frenchy@gmail.com
http://chrisholland.blogspot.com/

Copyright Chris Holland 2004-2005
Some rights reserved under a Creative Commons License:
Share-alike, with Attribution, do whatever you want with it beyond that :)
http://www.creativecommons.org/

*/

/* start basic stuff */
function isWithinNode(e,i,c,t,obj) {
answer = false;
te = e;
while(te && !answer) {
	if	((te.id && (te.id == i)) || (te.className && (te.className == i+"Class"))
			|| (!t && c && te.className && (te.className == c))
			|| (!t && c && te.className && (te.className.indexOf(c) != -1))
			|| (t && te.tagName && (te.tagName.toLowerCase() == t))
			|| (obj && (te == obj))
		) {
		answer = te;
	} else {
		te = te.parentNode;
	}
}
return te;
}//isWithinNode

function getEvent(event) {
return (event ? event : window.event);
}//getEvent()

function getEventElement(e) {
return (e.srcElement ? e.srcElement: (e.target ? e.target : e.currentTarget));
}//getEventElement()

/* end basic stuff */

function handleClick(event) {
e = getEvent(event);
eL = getEventElement(e);
if (listItem = isWithinNode(eL,null,null,"li",null)) {
	processListItem(listItem);
}//if we clicked on a list item
}//handleClick

function processListItem(liObj,isInitMode) {
	childList = liObj.getElementsByTagName("ol");
	if (!childList || (childList.length == 0)) childList = liObj.getElementsByTagName("ul");
	if (childList) childList = childList[0];
	if (childList) {
		currentDisplay = childList.style.display;
		if (!currentDisplay || (currentDisplay == "block")) {
			if (isInitMode && (!childList.getAttribute("compact"))) showAsExpanded(liObj,childList);
			else showAsCompact(liObj,childList);
		} else {
			if (isInitMode) showAsCompact(liObj,childList);
			else showAsExpanded(liObj,childList);
		}
	}
}//processListItem

function showAsCompact(liObj, childList) {
	childList.style.display = "none"
	childList.setAttribute("compact","compact");
	liObj.firstChild.className = "compactHeader";
	liObj.className="compact";
}//showAsCompact

function showAsExpanded(liObj, childList) {
	childList.style.display = "block";
	childList.removeAttribute("compact");
	liObj.firstChild.className = "expandedHeader";
	liObj.className = "expanded";
}//showAsExpanded

function initOutlines() {
	listItems = document.getElementsByTagName("li");
	for(i=0;listItems[i];i++) {
		processListItem(listItems[i],true);
	}

}//initOutlines

if (document.addEventListener) {
	document.addEventListener("mouseup", handleClick, false);
	window.addEventListener("load", initOutlines, false);
} else {
	document.onmouseup = handleClick;
	window.onload = initOutlines;
}

