/* www.BadgerTrek.com javascript library */

CLOSED_IMAGE='/script/p.png'; // Collapsed element img
OPEN_IMAGE='/script/m.png';   // Expanded element img

/* makeCollapsible - makes an element collapsible
 * 
 * colElement - the element representing the list to make collapsible
 * inserts a '+' type image as the clickable expander as parent's first child on non list items
 * replaces bullet with image for list items
 * adds an onclick to the image to toggle visiblity and the image to a '-'
 */
function makeCollapsible(colElement){
  	colElement.style.display='none';
	var parent=colElement.parentNode;
	var node;
	
	if(parent.nodeName == 'LI') {
	  // removed list item bullets and the sapce they occupy
		parent.style.listStyleImage = "url(/script/p.png)";
		colElement.style.listStyleImage = "none";
		node=parent;
	} else {
		node=document.createElement('img');
		node.setAttribute('src',CLOSED_IMAGE);
		parent.insertBefore(node,parent.firstChild);
	}
	node.setAttribute('class','collapsibleClosed');
  	node.onclick=createToggleFunction(node, colElement);
}

/* createToggleFunction - returns a function that toggles the sublist display
 * 
 * toggleElement  - the element representing the toggle gadget
 * sublistElement - an array of elements representing the sublists that should
 *                  be opened or closed when the toggle gadget is clicked
 */
function createToggleFunction(toggleElement, colElement){
	return function(){
		var parent=colElement.parentNode;
		// toggle status of toggle element, default to collapsing it if class not defined
		if (toggleElement.getAttribute('class')=='collapsibleClosed'){
			toggleElement.setAttribute('class','collapsibleOpen');
			colElement.style.display = '';
			if(parent.nodeName == 'LI') {
				// removed list item bullets and the sapce they occupy
				parent.style.listStyleImage = "url(" + OPEN_IMAGE + ")";
				colElement.style.listStyleImage = "none";
			} else {
				toggleElement.setAttribute('src',OPEN_IMAGE);
			}
		} else {
			toggleElement.setAttribute('class','collapsibleClosed');
			colElement.style.display = 'none';
			if(parent.nodeName == 'LI') {
				// removed list item bullets and the sapce they occupy
				parent.style.listStyleImage = "url(" + CLOSED_IMAGE + ")";
				colElement.style.listStyleImage = "none";
			} else {
				toggleElement.setAttribute('src',CLOSED_IMAGE);
			}
		}
  }
}

/*
 * Add new operation to page onload, preserving prior onload functions defined already 
 * Ex:  addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
 * EX:  addLoadEvent(function() { ...  } );
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


