/*

DB learnt code from Stuart Langridge:

filename: toggleMenuXP.js

Converts an unordered list to an explorer-style tree, with clickable
icons

To make this work, simply add one line to your HTML:
<script type="text/javascript" src="toggleMenuXP.js"></script>

and then make the top UL of your nested unordered list of class
"toggleMenuXP".

That's it. No registration function, nothing.

http://www.kryogenix.org/code/browser/aqlists/

Stuart Langridge, November 2002
sil@kryogenix.org

Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/) and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109)

*/

addEvent(window, "load", makeTreesC);

function makeTreesC() {
    // We don't actually need createElement, but we do
    // need good DOM support, so this is a good check.
    if (!document.createElement) return;
    
    uls = document.getElementsByTagName("ul");
    for (uli=0;uli<uls.length;uli++) {
        ul = uls[uli];
        if (ul.nodeName == "UL" && ul.className.indexOf("toggleMenuXP")>-1 ) {
            processULELC(ul);
        }
    }
}

function processULELC(ul) {
    if (!ul.childNodes || ul.childNodes.length == 0) return;
    // Iterate LIs
    for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
        var item = ul.childNodes[itemi];
        if (item.nodeName == "LI") {
            // Iterate things in this LI
            var a;
            var subul;
	    subul = "";
            for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
                var sitem = item.childNodes[sitemi];
                switch (sitem.nodeName) {
                    case "A": a = sitem; break;
                    case "UL": subul = sitem; 
                               processULELC(subul);
                               break;
                }
            }
            if (subul) {
                associateELC(a,subul);
            } else {
                a.parentNode.className = (a.parentNode.className=='') ? "bullet" : a.parentNode.className+ ' bullet';
            }
        }
    }
}

//link on LI
function LINK_ON_LI_associateELC(a,ul) {
    if (a.parentNode.className.indexOf('bullet_open') == -1 && a.parentNode.className.indexOf('bullet_closed') == -1)
      a.parentNode.className = a.parentNode.className + ' bullet_closed';
    a.parentNode.onclick = function () {
		if (this.className.indexOf('bullet_open')>-1)
		{
			this.className = this.className.replace('bullet_open', 'bullet_closed');
		}
		else
		{
			this.className = this.className.replace('bullet_closed', 'bullet_open');
		}
		//alert(this.parentNode.className);
		//this.parentNode.className = (this.parentNode.className.indexOf('bullet_open')>-1) ? "bullet_closed" : "bullet_open";
        return true;
    }
}

//link on anchor
function associateELC(a,ul) {
    if (a.parentNode.className.indexOf('bullet_open') == -1 && a.parentNode.className.indexOf('bullet_closed') == -1)
      a.parentNode.className = a.parentNode.className + ' bullet_closed';
	var objImageForLink = a.parentNode.childNodes[0];
	var functTemp;
    functTemp = function () {
		if (this.parentNode.className.indexOf('bullet_open')>-1)
		{
			this.parentNode.className = this.parentNode.className.replace('bullet_open', 'bullet_closed');
			if (this.nodeName=="IMG") {this.src=this.src.replace('minus', 'plus');}
		}
		else
		{
			this.parentNode.className = this.parentNode.className.replace('bullet_closed', 'bullet_open');
			if (this.nodeName=="IMG") {this.src=this.src.replace('plus', 'minus');}
		}
		//alert(this.parentNode.className);
		//this.parentNode.className = (this.parentNode.className.indexOf('bullet_open')>-1) ? "bullet_closed" : "bullet_open";
        return true;
    }
	//a.onclick = functTemp;
	objImageForLink.onclick = functTemp;
	
	//the following loop adds the same onclick event to all child images of the LI, to allow content on submenus
	for (var i = 0; i < a.parentNode.childNodes.length; i++)
		if (a.parentNode.childNodes[i].nodeName=='IMG')
			a.parentNode.childNodes[i].onclick = functTemp;

}

/*              Utility functions                    */

function addEvent(obj, evType, fn){
  /* adds an eventListener for browsers which support it
     Written by Scott Andrew: nice one, Scott */
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}
