/*
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];
			// Added to hide sub-uls
			//if (ul.nodeName == "UL" && ul.parentNode.parentNode.className == "aqtree3") 
			//{
			//	ul.style.display = "none";
			//}
			// End 
			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) 
{
	a.onclick = function () 
	{
		var display = ul.style.display;
		this.style.backgroundImage = (display == "block") ? "url(images/arrow_white_left_6.gif)" : "url(images/arrow_white_down_6.gif)";
		this.parentNode.className = (display == "block") ? "" : "expanded";
		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;
	}
}

/*              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);


