var ie
if (BrowserDetect.browser.indexOf("Explorer") != -1) {
	ie = true
} else {
	ie = false
}

/*******************************************************************************
 * Generic onLoad stuff:
 * http://www.sitepoint.com/article/structural-markup-javascript/
 ******************************************************************************/
   
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
   obj.addEventListener(evType, fn, false)

   return true
 } else if (obj.attachEvent){
   var r = obj.attachEvent("on"+evType, fn)

   return r
 } else {
   return false;
 }
}

/*******************************************************************************
 * A heavy hack of this page:
 * http://www.dynamicdrive.com/dynamicindex1/simpletreemenu.js
 ******************************************************************************/

var treemenu = new Object()

treemenu.imgClick = function(ev) {
    var imgObject
    if (ev.srcElement) { //only IE will return true
		ie = true
        imgObject = ev.srcElement
    } else {
		ie = false
        imgObject = this
    }
    
    if (imgObject.src.indexOf("_col.bmp") == -1) {
        imgObject.src = imgObject.src.replace("_exp.bmp", "_col.bmp")
    } else if (imgObject.src.indexOf("_exp.jpg") == -1) {
        imgObject.src = imgObject.src.replace("_col.bmp", "_exp.bmp")
    } else {
        alert("Malformed image src: " + imgObject.src)
    }
    
}

/***************************************
* BEGIN functions for getting and
* setting the state of a parent node
***************************************/
treemenu.isClassOpen = function(obj) {
	if (ie) {
		if (obj.getAttribute("className") &&
        	obj.getAttribute("className").indexOf("open") != -1) {
		
			return true;
		} else {
			return false;
		}
	} else {
		if (obj.getAttribute("class") &&
			obj.getAttribute("class").indexOf("open") != -1) {
			
			return true;
		} else {
			return false;
		}
	}
}

treemenu.isClassClosed = function(obj) {
	if (ie) {
		if (obj.getAttribute("className") &&
			obj.getAttribute("className").indexOf("closed") != -1) {
			
			return true;
		} else {
			return false;
		}
	} else {
	
		if (obj.getAttribute("class") &&
			obj.getAttribute("class").indexOf("closed") != -1) {
			
			return true;
		} else {
			return false;
		}
	}
}

treemenu.setClassOpen = function(obj) {
	if (ie) {
		
		if (obj.getAttribute("className")) {
			
			if (obj.getAttribute("className").indexOf("open") != -1) {
				/* Do nothing. It's already marked as "open" */
				
			} else if (obj.getAttribute("className").indexOf("closed") != -1) {
				obj.setAttribute("className",
								 obj.getAttribute("className").replace("closed", "open"))
			} else {
				/* It's currently marked as neither "open" or "closed" */
				obj.setAttribute("className",
								 obj.getAttribute("className")+ " open")
			}
		} else {
			obj.setAttribute("className", "open")
		}
	} else {
	
		if (obj.getAttribute("class")) {
			
			if (obj.getAttribute("class").indexOf("open") != -1) {
				/* Do nothing. It's already marked as "open" */
				
			} else if (obj.getAttribute("class").indexOf("closed") != -1) {
				obj.setAttribute("class",
								 obj.getAttribute("class").replace("closed", "open"))
			} else {
				/* It's currently marked as neither "open" or "closed" */
				obj.setAttribute("class",
								 obj.getAttribute("class")+ " open")
			}
		} else {
			obj.setAttribute("class", "open")
		}
	}
}

treemenu.setClassClosed = function(obj) {
	if (ie) {
		if (obj.getAttribute("className")) {
			
			if (obj.getAttribute("className").indexOf("closed") != -1) {
				/* Do nothing. It's already marked as "closed" */
				
			} else if (obj.getAttribute("className").indexOf("open") != -1) {
				obj.setAttribute("className",
								 obj.getAttribute("className").replace("open", "closed"))
			} else {
				/* It's currently marked as neither "open" or "closed" */
				obj.setAttribute("className",
								 obj.getAttribute("className")+ " closed")
			}
		} else {
			obj.setAttribute("className", "closed")
		}
	} else {
	
		if (obj.getAttribute("class")) {
			
			if (obj.getAttribute("class").indexOf("closed") != -1) {
				/* Do nothing. It's already marked as "closed" */
				
			} else if (obj.getAttribute("class").indexOf("open") != -1) {
				obj.setAttribute("class",
								 obj.getAttribute("class").replace("open", "closed"))
			} else {
				/* It's currently marked as neither "open" or "closed" */
				obj.setAttribute("class",
								 obj.getAttribute("class")+ " closed")
			}
		} else {
			obj.setAttribute("class", "closed")
		}
	}
}
	
/***************************************
* END functions for getting and setting
* the state of a parent node
***************************************/

treemenu.createTree = function(treeid, parentImgClass) {
	
    var ultags = document.getElementById(treeid).getElementsByTagName("ul")
    for (var i = 0; i < ultags.length; i++) {
        treemenu.buildSubTree(treeid, ultags[i], i)
    }
    
    var treeImgTags = document.getElementById(treeid).getElementsByTagName("img")
    for (var i = 0; i < treeImgTags.length; i++) {
		if (ie) {
			
			if (treeImgTags[i].getAttribute("className") &&
				treeImgTags[i].getAttribute("className").indexOf(parentImgClass) != -1) {
				/* The first part of this conditional tests to see if it has a class
				 * attribute and the second part checks the value of the class
				 * attribute. */
				
				addEvent(treeImgTags[i], "click", treemenu.imgClick)
			}
		} else {
			if (treeImgTags[i].getAttribute("class") &&
				treeImgTags[i].getAttribute("class").indexOf(parentImgClass) != -1) {
				/* The first part of this conditional tests to see if it has a class
				 * attribute and the second part checks the value of the class
				 * attribute. */
				
				addEvent(treeImgTags[i], "click", treemenu.imgClick)
			}
		}
    }                                                 
}



treemenu.buildSubTree = function(treeid, ulelement, index) {

    if (!treemenu.isClassOpen(ulelement) || treemenu.isClassClosed(ulelement)) {
        //if UL has NO open/closed class explicted added by user
        
		treemenu.setClassClosed(ulelement)
        ulelement.style.display = "none"
        
    } else if (treemenu.isClassOpen(ulelement)) {
        // else if this UL has an explicit class value of "open"
        
        treemenu.expandSubTree(treeid, ulelement) //expand this UL plus all parent ULs (so the most inner UL is revealed!)
    }
    ulelement.parentNode.onclick = function(e) {
        var submenu = this.getElementsByTagName("ul")[0]
		
		if (treemenu.isClassClosed(submenu)) {
			// If it's closed then switch it to open.
			
            submenu.style.display = "block"
			treemenu.setClassOpen(submenu)
			
        } else if (treemenu.isClassOpen(submenu)) {
			// If it's open then switch it to closed.
			
            submenu.style.display = "none"
			treemenu.setClassClosed(submenu)
        }
		
        treemenu.preventpropagate(e)
    }
    ulelement.onclick = function(e){
        treemenu.preventpropagate(e)
    }
}

treemenu.expandSubTree = function(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"
			treemenu.setClassOpen(currentnode)
        }
        currentnode = currentnode.parentNode
    }
}

treemenu.flatten = function(treeid, action) { //expand or contract all UL elements
    var ultags = document.getElementById(treeid).getElementsByTagName("ul")
    for (var i = 0; i < ultags.length; i++) {
        ultags[i].style.display = (action=="expand")? "block" : "none"
		if (action == "expand") {
			treemenu.setClassOpen(ultags[i])
		} else {
			treemenu.setClassClosed(ultags[i])
		}
    }
}

////A few utility functions below//////////////////////

treemenu.searcharray = function(thearray, value) {
	//searches an array for the entered value. If found, delete value from array
	
    var isfound = false
    for (var i = 0; i < thearray.length; i++) {
        if (thearray[i] == value) {
            isfound = true
            thearray.shift() //delete this element from array for efficiency sake
            break
        }
    }
    return isfound
}

treemenu.preventpropagate = function(e) { //prevent action from bubbling upwards
    if (typeof e!= "undefined") {
        e.stopPropagation()
    } else {
        event.cancelBubble = true
    }
}

treemenu.dotask = function(target, functionref, tasktype) { //assign a function to execute to an event handler (ie: onunload)
    var tasktype = (window.addEventListener)? tasktype : "on"+tasktype
    if (target.addEventListener) {
        target.addEventListener(tasktype, functionref, false)
    } else if (target.attachEvent) {
        target.attachEvent(tasktype, functionref)
    }
}
