/* ---------------------------------------- */
//											//
//				COMPLEX TREE				//
//											//
/* ---------------------------------------- */

/*
Automatically identifies if an object is a parent, hasInfo or is current
*/
advTree = {
	init : function(target,treeClass, optionalArgs){
		
		
		// set target tree
		oTargetTree = document.getElementById(target);
		
		// if no target tree return
		if (!oTargetTree){return;}
	
		// add class to tree
		addClass(oTargetTree, treeClass);
		
		// set node styles
		advTree.setStyles(target);
		
		// add events
		//addEvent(oTargetTree, 'mouseup', advTree.clickNav);
		oTargetTree.onmouseup = advTree.clickNav;
	},
	
	
	// set node styles
	setStyles : function(target){
		
		var target = document.getElementById(target);
		var lis = target.getElementsByTagName("LI");
		
		var itemMatches = null;
		var itemMatches = new Array();
		
		var itemMatchCount = 0;
	
		// for all the child li nodes
		for (i=0;i<lis.length;i++)
		{
			// if the LI has a UL child it must be a parent
			if(lis[i].getElementsByTagName('UL')[0]){
				
				// add parent class
				addClass(lis[i],"parent");
				// and hide sub menu
				addClass(lis[i].getElementsByTagName('UL')[0],"hidden");
	
			}
			// if the LI has a DIV child it must have extra info
			if(advTree.hasDescription(lis[i])){
				// add hasInfo
				addClass(lis[i],"hasInfo");
				// hide Info
				addClass(lis[i].getElementsByTagName('DIV')[0],"hidden");
			}
			
			// reset item matches
			// create a new array of matches
			// see if the item is a potential current match
			if(advTree.isCurrent(lis[i])){
				// add the object to the array
				itemMatches[itemMatchCount] = new Array();
				itemMatches[itemMatchCount].theObject = lis[i];
				itemMatches[itemMatchCount].theType = advTree.isCurrent(lis[i]);
				itemMatchCount++;
			}
		} 
		
		// current match types array
		var matchTypes = new Array('isUser','isSearch','isHash','isURL');
		
		// the current object is not found
		foundCurrent = false;
		
		// for each of the match types
		for (i=0;i<matchTypes.length;i++){
			// loop through the matched items
			for (count=0;count<itemMatches.length;count++){
				// if the the items match type is equal to the tested match type
				if(itemMatches[count].theType == matchTypes[i]){
					// set the current items style
					advTree.setCurrentStyle(itemMatches[count].theObject);
					// we have found the current item
					foundCurrent = true;
				}
			}
			// if the current item is found break the for loop.
			if (foundCurrent){break;}
		}
	},
	
	setCurrentStyle : function(target){
		// if the matched object is a parent
		if(checkClass(target, 'parent')){
			swapClass(target, 'parent', 'currentParentActive');
			swapClass(target.getElementsByTagName('UL')[0],"hidden","open");
		} else if(checkClass(target, 'hasInfo')){
			swapClass(target, 'hasInfo', 'currentHasInfoActive');
			swapClass(target.getElementsByTagName('DIV')[0],"hidden","open");
		// else must be a node
		} else {
			addClass(target,'current');
		}
		
		// loop up the tree and assign the current sections
		var parentNode = target;
		// while the node is a UL or LI
		while(parentNode.tagName == "UL" || parentNode.tagName == "LI"){
			// if the node is a parent set it to active
			if(checkClass(parentNode,'parent')){	
				swapClass(parentNode,'parent','parentActive');
			}
			// open its nested element
			if(checkClass(parentNode,'hidden')){	
				swapClass(parentNode,'hidden','open');
			}
			// add current section to node for styling
			addClass(parentNode, 'currentSection');
			// jump to parent
			parentNode = parentNode.parentNode;
		}
	},
	
	// onclick function
	clickNav : function(e){
		tg = getEventTarget(e);
//dbg_put("clickNav on "+e+",target is "+tg.tagName);
		// if the user clicked on an A
		if(tg.tagName == "A"){
			// and the href attribute is '#'
			if(new RegExp('#$').test(tg.getAttribute("href"))){
				// make the target the parent li
				tg = tg.parentNode
//dbg_put("  set to parent " + tg.tagName);
			}
		} // else user will goto link
		// if the user clicks an LI
		if(tg.tagName == "LI"){
			// and the li has a child
			if(tg.getElementsByTagName("UL")[0] || tg.getElementsByTagName("DIV")[0]){
				// toggle the node
//dbg_put("  toggle node");
				advTree.toggleNode(tg);
			// else
			} else if (tg.getElementsByTagName("A")[0]) {
				// follow the lis anchor child link
				window.location.href = tg.getElementsByTagName("A")[0].href;
//dbg_put("  follow anchor");
			}
		}
	},
	
	toggleNode : function(tg){
		if(checkClass(tg,'parent') || checkClass(tg,'parentActive')){
			swapClass(tg,'parentActive','parent');
			swapClass(tg.getElementsByTagName("UL")[0],'open','hidden');
			return;
		}
		if(checkClass(tg,'currentParent') || checkClass(tg,'currentParentActive')){
			swapClass(tg,'currentParent','currentParentActive');
			swapClass(tg.getElementsByTagName("UL")[0],'open','hidden');
			return;
		}
		if(checkClass(tg,'hasInfo') || checkClass(tg,'hasInfoActive')){
			swapClass(tg,'hasInfo','hasInfoActive');
			swapClass(tg.getElementsByTagName("DIV")[0],'open','hidden');
			return;
		}
		if(checkClass(tg,'currentHasInfo') || checkClass(tg,'currentHasInfoActive')){
			swapClass(tg,'currentHasInfo','currentHasInfoActive');
			swapClass(tg.getElementsByTagName("DIV")[0],'open','hidden');
			return;
		}
	},
	
	expandAll : function(target){
		var target = document.getElementById(target);
		var uls = target.getElementsByTagName("UL");
		for (i=0;i<uls.length;i++)
		{		
			if(checkClass(uls[i].parentNode,"parent")){
				swapClass(uls[i].parentNode,"parent","parentActive");
			} else if(checkClass(uls[i].parentNode,"currentParent")){
				swapClass(uls[i].parentNode,"currentParentActive","currentParent");
			}
			if(checkClass(uls[i],"hidden")){swapClass(uls[i],"hidden","open");}
		}
	},
	
	closeAll : function(target){
		var target = document.getElementById(target);
		var uls = target.getElementsByTagName("UL");
		for (i=0;i<uls.length;i++)
		{		
			if(checkClass(uls[i].parentNode,"parentActive")){
				swapClass(uls[i].parentNode,"parent","parentActive");
			} else if(checkClass(uls[i].parentNode,"currentParentActive")){
				swapClass(uls[i].parentNode,"currentParentActive","currentParent");
			}
			if(checkClass(uls[i],"hidden")){swapClass(uls[i],"hidden","open");}
		}
	},
	
	cleanUp : function(target){
		advTree.closeAll(target);
		advTree.setStyles(target);
	},
	
	// hasDescription(o)
	// finds if an object has a user delcared child element with the the user declared class  
	hasDescription : function(o){
		// set the childNode as the objects firstChild
		var childNode = o.firstChild;
		// while a child node exists
		while(childNode)
		{	
			// is the child node a div?
			if(childNode.tagName == "DIV"){
				// does the child div have the user defined description class
				if(checkClass(childNode,'ext')){
					// yes
					return true;
				}
			}
			// element was not the user defined description element
			// child node = its next sibling
			childNode = childNode.nextSibling;
		}
		// no description elements
		return false;
	}, // end hasDescription(o)
	
	// find current A that matches page URL
	isCurrent : function(target){
		if(checkClass(target,'userSelected')){
			return 'isUser';
		}
		if(window.location.search){
			if(hasMenuVar('menuItem') && document.getElementById(hasMenuVar('menuItem'))){
				if(target.id == hasMenuVar('menuItem')){
					return 'isSearch';
				}
			}
		}
		var a = target.getElementsByTagName("A")[0];
		if(a){
			if(window.location.hash && window.location.hash!=""){
				
				if(a.href == window.location.href.split("?")[0] && (!new RegExp('#$').test(a.href))) {
						
						return 'isHash';
				}
			}
			if(a.href == window.location.href.split("#")[0] && (!new RegExp('#$').test(a.href))){
				return 'isURL';
			}
		}
		return false;
	}
}
// the following three functions ( PageQuery(q), queryString(key) and hasMenuVar(key) ) are used to determine if the windows URL has a user declare variable in the query string
/* 	
	These functions were not wrtten by me! 
	They were written by By Peter A. Bromberg, Ph.D.
	Found at: http://www.eggheadcafe.com/articles/20020107.asp
	Used with owners permission
*/

function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
	for(var j=0; j < this.keyValuePairs.length; j++) {
		if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key){
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

function hasMenuVar(key){
	if(queryString(key)=='false')
	{
		return false;
	}else{
		return(queryString(key));
	}
}
