/* DLTree - Creates a Treeveiew from DLs (Definition Lists).
The script will operate on any definition list on the page that has the class attribute "tree", and ignore all others.
Each term/definitions pair should only have one <dt> tag with its text enclosed in a hyperlink, and a single <dd> tag.
The <dd> tag can then include other html structures, such as paragraphs, tables, other lists etc.
The resulting tree structure can be used for multiple purposes, such as FAQs, sitemaps, menus, directories and other listings.

For example:
<dl class="tree">
  <dt><a href="#">Are you open on the weekends? </a></dt>
  <dd>The Brooklyn (Brighton) office is open all weekends, all holidays,
	and in all weather conditions.</dd>
  <dt><a href="#">Do you accept my insurance?</a></dt>
  <dd>If we do NOT accept your insurance, it is either (a) because it is
	very rarely seen in the areas we cover, or (b) because it endangers the
	health of the patients it covers by making essential services difficult
	to obtain. <a href="insurances.htm">Click here for a partial listing
	of insurances we accept</a>.</dd>
</dl>	

The script also assigns and toggles icons for each node. Two cusotmisable variables point to the image files for the icons:
var plus = "url(../images/plus.gif)"
var minus = "url(../images/minus.gif)"

For this feature to work, you need to place these files in a similar directory your server, or use our own naming and path, and modify the variables instead.

The third variable creates space for these icons, and can also be modified:
var padding = "14px"

You can also include Expand All and Collapse All buttons that call methods in this script:
<a href="#" onclick="expandDLTree('tree')">Expand All</a>
<a href="#" onclick="initDLTree('tree')">Collapse All</a>

Author: Karina Steffens, www.neo-archaic.net
Created: October 2006
*/

//Icon images
var plus = "url(../images/plus.gif)"
var minus = "url(../images/minus.gif)"
//Padding for the icons
var padding = "14px"



//Initiates all the Defenition Lists with the className "tree"
function initDLTree(className){
	if (document.getElementById) {
		var lastId;
		var dls = document.getElementsByTagName("DL")
		for (var i=0; i<dls.length; i++){
			var dl = dls[i];
			if (className != null && !hasClass (dl, className)){
				continue;
			}
			var id = dl.id
			if (id == ""){
				id = "dl" + i;
				dl.id = id
			}
			collapseDL(id)
		}		
	}	
}

//Collapses a Definition List
function collapseDL(id){
	if (document.getElementById) {
		  var lastId;
			var dl = document.getElementById(id);
				for (i=0; i<dl.childNodes.length; i++) {
				var node = dl.childNodes[i];
				if (node.nodeName=="DT") {
					for (j=0; j<node.childNodes.length; j++){
					   lnk = node.childNodes[j];
					   if (lnk.nodeName == "A"){
							   break;
					   }
					}
					lnk.id = id+i;
					//lnk.hash = "expand_node"
					lnk.style.backgroundImage = plus;
					lnk.style.backgroundRepeat = "no-repeat";
					lnk.style.backgroundPosition = "left center";
					lnk.style.paddingLeft = padding;					
					lnk.onclick = lnk.onkeyup = function(){
						toggleDisplay("dd" + this.id);
						toggleSign(this);
						return false;
					}
					lastId = lnk.id;
				}
				if (node.nodeName=="DD") {
					node.id = "dd" + lastId;
					node.style.display = "none"
				}
			}
		}	
}

//Toggles the display the passed <dd> element (could also be used for divs and other elements)
function toggleDisplay(id){
	  var obj = document.getElementById(id)
		  if (obj != undefined){
			  if (obj.style.display == "none"){
				obj.style.display = "block"
			}else{
				obj.style.display = "none"
			}			
		}		  
}


//Toggles the displayed sign icon (plus and minus)
function toggleSign(lnk){
	if (lnk.style.backgroundImage == plus){
		lnk.style.backgroundImage = minus;
	}else if (lnk.style.backgroundImage == minus){
		lnk.style.backgroundImage = plus;
	}
}


//Expands all the Definition lists on the page that correspond to the passed className ("tree")
function expandDLTree(className){
	if (!document.getElementById){
		return;
	}
	var dds = document.getElementsByTagName("DD")
	for (var i=0; i<dds.length; i++){
		var dd = dds[i];
		if (hasClass(dd.parentNode, className)){
			dd.style.display = "block"
		}
	}
	var lnks  = document.getElementsByTagName("A")
	for (var i=0; i<lnks.length; i++){
		var lnk = lnks[i];
		if (lnk.style.backgroundImage == plus && hasClass(lnk.parentNode.parentNode, className)){
			lnk.style.backgroundImage = minus;
		}
	}
}

//Checks if the passed element's className includes the passed className. 
function hasClass(obj, className){
	var classArray = obj.className.split(" ");
	for (var i in classArray){
		if (classArray[i] == className){
			return true;
		}
	}
	return false;
}

//Initiate the function without conflicting with the window.onload event of any preceding scripts
var tempFuncDL = window.onload;
window.onload = function(){
	if (typeof (tempFuncDL) == "function"){
		try{
			tempFuncDL();
		} catch(e){}
	}
	  initDLTree("tree");
}

