/* 

	List Expander 
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	
*/

this.listexpander = function(){
	
	// edit 
	
	var expandTo = 1; // level up to which you want your lists to be initially expanded. 1 is minimum
	var expandText = "Expand All"; // text for expand all button
	var collapseText = "Collapse All"; // text for collapse all button		
	var listClass = "listexpander" // class name that you want to assign to list(s). If you wish to change it make sure to update the css file as well  
	
	// end edit (do not edit below this line)
	
	this.start = function(){
		var ul = document.getElementsByTagName("ul");
		for (var i=0;i<ul.length;i++){
			if(ul[i].className == listClass){
				create(ul[i]);
				buttons(ul[i])
			};
		};
	};

	this.create = function(list) {	
		var items = list.getElementsByTagName("li");
		for(var i=0;i<items.length;i++){
			listItem(items[i]);
		};
	};	

	this.listItem = function(li){
		if(li.getElementsByTagName("ul").length > 0){
			var ul = li.getElementsByTagName("ul")[0];
			ul.style.display = (depth(ul) <= expandTo) ? "block" : "none";
			li.className = (depth(ul) <= expandTo) ? "expanded" : "collapsed";
			li.over = true;	
			ul.onmouseover = function(){li.over = false;} 
			ul.onmouseout = function(){li.over = true;} 
			li.onclick = function(){
				if(this.over){
					ul.style.display = (ul.style.display == "none") ? "block" : "none";
					this.className = (ul.style.display == "none") ? "collapsed" : "expanded";				
				};
			};
		};		
	};	
	
	this.buttons = function(list){
		var parent = list.parentNode;
		var p = document.createElement("p");
		p.className = listClass;
		var a = document.createElement("a");
		a.innerHTML = expandText;
		a.onclick = function(){expand(list)};
		p.appendChild(a);
		var a = document.createElement("a");
		a.innerHTML = collapseText;
		a.onclick = function(){collapse(list)};
		p.appendChild(a);
		parent.insertBefore(p,list);
	};
	
	this.expand = function(list){
		li = list.getElementsByTagName("li");
		for(var i=0;i<li.length;i++){
			if(li[i].getElementsByTagName("ul").length > 0){
				var ul = li[i].getElementsByTagName("ul")[0];
				ul.style.display = "block";
				li[i].className = "expanded";
			};
		};
	};
	
	this.collapse = function(list){
		li = list.getElementsByTagName("li");
		for(var i=0;i<li.length;i++){
			if(li[i].getElementsByTagName("ul").length > 0){
				var ul = li[i].getElementsByTagName("ul")[0];
				ul.style.display = "none";
				li[i].className = "collapsed";
			};
		};
	};
	
	this.depth = function(obj){
		var level = 1;
		while(obj.parentNode.className != listClass){
			if (obj.tagName == "UL") level++;
			obj = obj.parentNode;
		};
		return level;
	};	
	
	start();


// Navigation Tree  for the Menu,  (///       window.onload = function () {    
																		//////};     /// ) taken out from the below and inserted in above function to enable both function of the javascript on windowload.///

	var x = document.getElementById('navTree');
    var listItems = x.getElementsByTagName("LI");
    for (var i = 0; i < listItems.length; i++){
		var hasSubMenu = false; // assume LI has no sub menu
		var listItemsChildren = listItems[i].childNodes; // get all LI children
		// loop through children to see if the node is an element and the tag is a "UL"
		for (var count = 0; count < listItemsChildren.length; count++){ 
			if(listItemsChildren[count].nodeType == 1 && listItemsChildren[count].tagName == "UL"){
				// is a UL set hasSubMenu as true
				hasSubMenu = true;
			}
		}

		if (hasSubMenu == true){
			
			listItems[i].className+=' closed';
			
			var firstChild = listItems[i].firstChild; // text node
			
			while (firstChild.nodeType != 1) {
				firstChild = firstChild.nextSibling;
			}
			firstChild.className+=' closed';


			if(window.attachEvent){ // does the browser support this object method?
				listItems[i].attachEvent('onclick',clickNav) // ie
			} else if (window.addEventListener){ // does the browser support this object method?
				listItems[i].addEventListener('click',clickNav,false); // mozilla and most other browsers
			}
		}
	}
};
window.onload = listexpander;




function clickNav(e)
{
	// cancel event bubbling
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	if (!e) var e = window.event;
	if (e.target) var tg = e.target;
	else if (e.srcElement) var tg = e.srcElement;
	
	while (tg.nodeName != 'LI'){ // Safari GRRRRRRRRRR
		tg = tg.parentNode;
	}
	
	var firstChild = tg.firstChild; // text node
	
	while (firstChild.nodeType != 1) {
		firstChild = firstChild.nextSibling;
	}
	// if elements class value contains and instance of open
	if (firstChild.className.search(/open/) >= 0){
	 	tg.className = tg.className.replace('open', 'closed');
		firstChild.className = firstChild.className.replace('open', 'closed');
	} else {
	 	tg.className = tg.className.replace('closed', 'open');
		firstChild.className = firstChild.className.replace('closed', 'open');
	}
}