/*
aqtree3.js

Converts an unordered list to an explorer-style tree

To make this work, simply add one line to your HTML:
<script type="text/javascript" src="aqtree3.js"></script>

and then make the top UL of your nested unordered list of class "aqtree3".

That's it. No registration function, nothing.

http://www.kryogenix.org/code/browser/aqlists/

Stewart 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)

*/

function makeTrees() {
    // 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 == "aqtree3") {
            processULEL(ul);
        }
    }
}

function processULEL(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; 
                               processULEL(subul);
                               break;
                }
            }
            if (subul) {
                associateEL(a,subul);
            } else {
                //a.parentNode.style.listStyleImage = "url(bullet.gif)";
            }
        }
    }
}

function associateEL(a,ul) 
{
	//alert(a);
	a.onclick = function () 
	{
		var display = ul.style.display;
		//this.parentNode.style.listStyleImage = (display == "block") ? "url(plus.gif)" : "url(minus.gif)";
		ul.style.display = (display == "block") ? "none" : "block";
		return false;
	}
	a.onmouseover = function() 
	{
		var display = ul.style.display;
		window.status = (display == "block") ? "Collapse" : "Expand";
		return true;
	}
	a.onmouseout = function() 
	{
		window.status = "";
		return true;
	}
}

/*		Added to persist location in menu		*/

function showCurrentPage()
{
	var objLocation = this.location
	var arrLi = document.getElementsByTagName("LI");
	
	for (var i = 0; i < arrLi.length; i++)
	{
		// Loop through LIs
		if (!arrLi[i].childNodes || arrLi[i].childNodes.length == 0) break;
		for (var j = 0; j < arrLi[i].childNodes.length; j++)
		{
			// Loop through childNodes of each LI
			if (arrLi[i].childNodes[j].nodeName == "A") 
			{
				var objA = arrLi[i].childNodes[j]
				if (objA.href == objLocation.href)
				{
					if (objA.parentNode.parentNode.className != "aqtree3")
					{
						objA.parentNode.parentNode.style.display = "block";
						// Quick hack to allow 2nd level opening
						if (objA.parentNode.parentNode.parentNode.parentNode.style.display != "block")
						{
							objA.parentNode.parentNode.parentNode.parentNode.style.display = "block";
						}
					}
				}
			}
		}
	}
}

/*              Utility functions                    */

/*
AddEvent Manager (c) 2005-2006 Angus Turnbull http://www.twinhelix.com
Free usage permitted as long as this credit notice remains intact.
*/

if (typeof addEvent != 'function')
{
 var addEvent = function(o, t, f, l)
 {
  var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  if (o[d] && !l) return o[d](t, f, false);
  if (!o._evts) o._evts = {};
  if (!o._evts[t])
  {
   o._evts[t] = o[n] ? { b: o[n] } : {};
   o[n] = new Function('e',
    'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
     'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
     '} return r');
   if (t != 'unload') addEvent(window, 'unload', function() {
    removeEvent(rO, rT, rF, rL);
   });
  }
  if (!f._i) f._i = addEvent._i++;
  o._evts[t][f._i] = f;
 };
 addEvent._i = 1;
 var removeEvent = function(o, t, f, l)
 {
  var d = 'removeEventListener';
  if (o[d] && !l) return o[d](t, f, false);
  if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
 };
}

// Optional cancelEvent() function you can call within your event handlers to
// stop them performing the normal browser action or kill the event entirely.
// Pass an event object, and the second "c" parameter cancels event bubbling.
function cancelEvent(e, c)
{
 e.returnValue = false;
 if (e.preventDefault) e.preventDefault();
 if (c)
 {
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
 }
};

addEvent(window, "load", makeTrees);

