
function createTree(treeid, sectionId){
	
	var ultags = document.getElementById(treeid).getElementsByTagName("ul")
	var imtags = document.getElementById(treeid).getElementsByTagName("img");

	for (var i=0; i<ultags.length; i++) {
		buildSubTree(treeid, ultags[i], i, imtags[i], sectionId)
	}
}

function buildSubTree(treeid, ulelement, index, imageElement, sectionId){

	ulelement.parentNode.className="submenu"

	if ( sectionId == index ){

		ulelement.setAttribute("rel", "open")
		ulelement.style.display="block"
		imageElement.src = "img/nav/" + imageElement.name + "_mouseover.gif"
	}
	else {
		ulelement.setAttribute("rel", "closed")
	}

	//element being opened.
	ulelement.parentNode.onclick=function(e){

		var submenu=this.getElementsByTagName("ul")[0]
		if (submenu.getAttribute("rel")=="closed"){
	
		//	submenu.style.display="block"
		//	submenu.setAttribute("rel", "open")
		}
		else if (submenu.getAttribute("rel")=="open"){

		//	submenu.style.display="none"
		//	submenu.setAttribute("rel", "closed")
		}
		
		preventpropagate(e)
	}

	ulelement.onclick=function(e){
		preventpropagate(e)
	}
}

function expandSubTree(treeid, ulelement){ //expand a UL element and any of its parent ULs

	var rootnode=document.getElementById(treeid)
	var currentnode=ulelement
	currentnode.style.display="block"
	while (currentnode!=rootnode){
		if (currentnode.tagName=="UL"){ //if parent node is a UL, expand it too
			currentnode.style.display="block"
			currentnode.setAttribute("rel", "open") //indicate it's open
		}
		currentnode=currentnode.parentNode
	}
}

////A few utility functions below//////////////////////

function preventpropagate(e){ //prevent action from bubbling upwards
	
	if (typeof e!="undefined")
		e.stopPropagation()
	else
		event.cancelBubble=true
}

function dotask(target, functionref, _tasktype){ //assign a function to execute to an event handler (ie: onunload)
	
	var tasktype = ""

	if(window.addEventListener)
		tasktype = _tasktype
	else
		tasktype = "on" + _tasktype

	if (target.addEventListener)
		target.addEventListener(tasktype, functionref, false)
	else if (target.attachEvent)
		target.attachEvent(tasktype, functionref)
}