/*
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/

This script depends on some specific classes in your stylesheet for best looks.  They should have this form:
li.cmpro-compacted { list-style-image: url(/images/global/downb.gif); }
	Your theme's plus sign
li.cmpro-expanded { list-style-image: url(/images/global/upb.gif); }
	Your theme's minus sign
li.cmpro-empty  { list-style-image: url(/images/global/arrow.gif); }
	The default icon for your CMPro page lists

*/

/* 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)) {
		if (((c=listItem.parentNode.className) && (c.indexOf("xoxo") != -1))) {
			processListItem(listItem);
		}//	
	}//if we clicked on a list item
}//handleClick

function processListItem(liObj,isInitMode) {
	noChildren = false;
	childList = liObj.getElementsByTagName("ol");
	if (!childList || (childList.length == 0)) childList = liObj.getElementsByTagName("ul");
	if (!childList || (childList.length == 0)) noChildren = true;	
	if (childList) childList = childList[0];
	if (noChildren == true) {
		showAsEmpty(liObj);
	} else {
		if (childList) {
			currentDisplay = childList.style.display;
			if (!currentDisplay || (currentDisplay == "block")) {
				if (isInitMode && (childList.getAttribute("style") != 'display: none;')) 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"
	liObj.firstChild.className = "compactHeader";
	liObj.className="cmpro-compacted cmpro-pageslistitem";
}//showAsCompact

function showAsExpanded(liObj, childList) {
	childList.style.display = "block";
	liObj.firstChild.className = "expandedHeader";
	liObj.className = "cmpro-expanded cmpro-pageslistitem";
}//showAsExpanded

function showAsEmpty(liObj) {
	liObj.firstChild.className = "normalHeader";
	liObj.className = "cmpro-empty cmpro-pageslistitem";
}//showAsEmpty

function initOutlines() {
	listItems = document.getElementsByTagName("li");
	for(i=0;listItems[i];i++) {
		if (((c=listItems[i].parentNode.className) && (c.indexOf("xoxo") != -1))) {
			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;
}
