// JavaScript Document


// link variables
var objOld = null;
var linkContent = null;

// trim prototype
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
};

/**
 * Iterates through navigation object, locates active node, and
 * highlights node's tree and it's siblings.
 *
 */
function subnavConstr() {
	// iterate through line items
	// for each line item check if there is a match
		// if match is found, enable children nodes, disable all others
	

	// reference navigation list
	var objNavList = ge('navigation');
	var objHideList = new Array();

	if (objNavList !== null) {

		// reference each element
		var lineItems = objNavList.getElementsByTagName('LI');
		var foundNode = null;

		// locate page within tree
		for (var i=0; i<lineItems.length; i++) {
			if (lineItems[i].getElementsByTagName('A')[0].innerHTML.trim() == pageTitle) {
				foundNode = lineItems[i];
			}
			else if (lineItems[i].parentNode != objNavList) {
				objHideList.push(lineItems[i]);
			}
			lineItems[i].getElementsByTagName('A')[0].className = '';
		}
		// if the node has been found, update tree
		if (foundNode != null) {
			for (var i=0; i<objHideList.length; i++) {
				objHideList[i].style.display = 'none';
			}
			var objParent = foundNode.parentNode;
			if (objParent == objNavList) {
				setActiveClass('category-',foundNode);
			}
			while (objParent != objNavList) {
				// show parent
				objParent.style.display = '';
				setActiveClass('category-',objParent);

				// show children
				var objChildren = objParent.getElementsByTagName('LI');
				for (var j=0; j<objChildren.length; j++) {
					if (objChildren[j].parentNode == objParent) {
						objChildren[j].style.display = '';
					}
				}

				// show new menu
				objParent = objParent.parentNode;
			}

			// show child nodes if this is a category
			var objListChildren = foundNode.getElementsByTagName('UL');
			if (objListChildren.length > 0) {
				// update category class
				setActiveClass('category-',foundNode);
				
				if (objListChildren[0].parentNode == foundNode) {
					var objChildren = objListChildren[0].getElementsByTagName('LI');
					for (var j=0; j<objChildren.length; j++) {
						if (objChildren[j].parentNode == objListChildren[0]) {
							objChildren[j].style.display = '';
						}
					}
				}
			}
			else {
				setActiveClass('current-',foundNode);
			}
		}
	}
}
/**
 * Updates the class of the line item 
 *
 * @param	prefix		the prefix of the name of the new class
 * @param	objNode		the line item to update
 */
function setActiveClass(prefix, objNode) {
	var pClassName = objNode.className.split('-');
	if (pClassName.length > 0) {
		objNode.getElementsByTagName('A')[0].className = prefix+pClassName[1];
	}
}
