/* standard functions library */

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	// alert ("the number of elements in the class is " + classElements.length) ; 
	return classElements;
}

/* this needs to be refactored and renamed */

function toggleDislosureItem(thisElement, searchClass, state1, state2) {
	//alert ("called toggleDislosureItem") ; 	
	//alert(" this element is " + thisElement) ;
	containingElement = thisElement.parentNode; 
	//alert(" this element parent node is " + containingElement) ;
	
	parentElement = containingElement.parentNode;
	classOfParentElement = containingElement.parentNode.className;
	//alert ("class of parent element is " + classOfParentElement) ; 
	
	
	// now find the img tag within the a link 
	// the first child is the image 
	
	firstChild = thisElement.firstChild; 
	//alert("first child is " + firstChild) ;
	//alert("first child tag is " + firstChild.tagName) ;
	
	if (thisElement.firstChild.tagName == "IMG") {
		//alert("first child was an image") ;
		disclosureImage = thisElement.firstChild; 
	}

	


	if (classOfParentElement == searchClass + state1) {
		//alert ("found state 1") ;
		parentElement.className = searchClass + state2;
		if (disclosureImage != null) {
			disclosureImage.setAttribute("src", "images/disclosureTriangleExpanded.png"); 
		}
	}
	else if (classOfParentElement == searchClass + state2) {
		//alert ("found state 2") ;
		parentElement.className = searchClass + state1;
		if (disclosureImage != null) {
			disclosureImage.setAttribute("src", "images/disclosureTriangleCollapsed.png"); 
		}
	}	
	else {
		//alert ("element not found") ;
	
	}
}

function swapElement(elementID, newElementID) {
	//alert ("called swapElement with new element id " + newElementID) ; 	

	var existingElement = document.getElementById(elementID) ;
	
	var existingElementParent = existingElement.parentNode;
	var newElement = document.getElementById(newElementID) ;	
	// alert ("newElement is " + newElement) ; 
		
	// must clone the node, else we are in essence moving the node from the hidden area to the currentBio
	// if we do not clone, the function will fail on its second click of a selected node
	var newElementClone = newElement.cloneNode(true); 
	// set the new elements id to the old element's placeholder id 
	newElementClone.setAttribute("id", elementID) ;
	// DOM approved method
	existingElementParent.replaceChild(newElementClone, existingElement) ;
}


/* NOT USED - kept as sample only of navigating the tree 

// assumes unselected class name is null
// resets the selected menu item's class name to null
// then sets the newly selected class name
function updateMenuWithIDUsingSelectionClassNameAndLink(menuID, selectionClassName, selectedLink) {
	var menu = document.getElementById(menuID) ;// this is a ul
	var menuItem = menu.firstChild;
	while (menuItem != menu.lastChild ) {
		// only get element nodes (same as returned as getElementById etc) 
		if (menuItem.nodeType == 1 ) {
			if (menuItem.getAttribute("class") == selectionClassName) {
				// alert("found menu item of selection class " + selectionClassName) ;
				menuItem.setAttribute("class", null) ;
			};
		}
		// get next item
		menuItem = menuItem.nextSibling;
	}
	// update the menu item class; note the menu item is the parent of the link
	selectedLink.parentNode.setAttribute("class", selectionClassName);
}
*/

/* good idea for a general funciton 
function setAttributeOfAllOtherSiblingsOfElementToValue(attributeName, anElement, aValue) {
	
	// set class of all previous siblings to null
	
	var currentElement = anElement.previousSibling;
	//alert("current element is " + currentElement) ;
	
	while (currentElement != null ) {
		// first line filters out text elements such as line breaks, which are also nodes in the tree
		if (currentElement.nodeType == 1 ) {
			
				currentElement.setAttribute(attributeName, aValue) ;
			
		}
		currentElement = currentElement.previousSibling;
	}	
	// alert("done with previous siblings") ;
	// set class of all next siblings to null
	
	currentElement = anElement.nextSibling;
	
	//alert("next menuItem is " + menuItem + " " + menuItem.nodeType + " " + menuItem.parentNode) ;
	
	while (currentElement != null ) {
		// first line filters out text elements such as line breaks, which are also nodes in the tree
		if (currentElement.nodeType == 1 ) {

				currentElement.setAttribute(attributeName, aValue) ;
		}
		currentElement = currentElement.nextSibling;
	}	
	//alert("done with next siblings") ;
}
*/

/* Uses class name not id, which makes it much more complicated
function handleMenuSelection(link, selectionClassName) {
	// this is the link selected
	var menu  = link.parentNode.parentNode; // the parent of the link is the list item and its parent is the ul 
	
	
	var selectedMenuItem = link.parentNode ;
	// set class of selected item to selectionClassName
	selectedMenuItem.setAttribute("class", selectionClassName);
	
	// set class of all other menu items to null
	setAttributeOfAllOtherSiblingsOfElementToValue("class", selectedMenuItem, null) ;

	// finally swap elements
	// by convention the id of the new content is given by the title of the link
	
	swapElement("currentBio", link.getAttribute("title") ) ; 
}
*/

function handleMenuSelection(link, selectionID) {
	//alert("called this") ;
	// this is the link selected
	// var menu  = link.parentNode.parentNode; // the parent of the link is the list item and its parent is the ul 
	
	// reset the id of the currently selected item to null
	var currentSelection = document.getElementById(selectionID).setAttribute("id", null);
	// set the id of the new link to the selectionID
	var selectedMenuItem = link.parentNode ;
	// the parent node of the link is the li element 
	link.parentNode.setAttribute("id", selectionID);
	
	// finally swap elements
	// by convention the id of the new content is given by the title of the link
	
	swapElement("currentBio", link.getAttribute("title") ) ; 
}

function handleMenuSelectionWithSwapOf(link, selectionID, swapElementID) {
	//alert("got here") ;
	// this is the link selected
	// var menu  = link.parentNode.parentNode; // the parent of the link is the list item and its parent is the ul 
	
	// reset the id of the currently selected item to null
	var currentSelection = document.getElementById(selectionID).setAttribute("id", null);
	// set the id of the new link to the selectionID
	var selectedMenuItem = link.parentNode ;
	// the parent node of the link is the li element 
	link.parentNode.setAttribute("id", selectionID);
	
	// finally swap elements
	// by convention the id of the new content is given by the title of the link
	
	swapElement(swapElementID, link.getAttribute("title") ) ; 
}

