/**
 *	Table of Contents with navigation,
 *
 *	This set of functions assumes the existence of a frame with
 *	id="mainframe" and a UL element with an A element within each
 *	terminal LI. 
 *
 *  Some of these functions are derived from those in expandableUL.js
 *	and family.js
 *
 * 
 */

var showBulletImage = true ;
var bulletSrc = "images/boxBullet.gif" ;

/*	Adds expanding/collapsing functionality to a UL element with
	id = "expandableUL" */
function loadExpandableUL()
{
	if( document.getElementById ) {
		/*	Get all list items in expandableUL. */
		var zLI = document.getElementById('expandableUL').getElementsByTagName('li') ;

		/*	For LI elements with UL children, wrap LI text with anchors to 
			expand/collapse children UL elements. */
		for( var i=0; i<zLI.length; i++ ) {

			/*	Create IMG element for expander image. */
			var expanderImage = document.createElement("img") ;
			expanderImage.src = "images/eUL-collapsed.gif" ;
			expanderImage.alt = "" ;
			expanderImage.border = "0" ;

			var itemBulletImage = document.createElement("img") ;
			itemBulletImage.src = bulletSrc ;
			itemBulletImage.alt = "" ;
			itemBulletImage.border = "0" ;

			if( zLI[i].getElementsByTagName('ul').length > 0 ) {
				/*	LI element should expand */
				
				/*	Get node and nodeValue of current LI */
				var zContentNode = zLI[i].childNodes[0] ;
				var zContent = zContentNode.nodeValue ;
				
				/*	Create anchor to wrap LI element text */
				var anchorWrapper = document.createElement("a") ;
				anchorWrapper.href = "#" ;
				anchorWrapper.onclick = function() { toggle(this) } ;
				
				/*	Put LI text into A text. */
				var anchorTextNode = document.createTextNode(zContent) ;
				anchorWrapper.appendChild( anchorTextNode ) ;

				/*	Swap LI text with new A. */
				zLI[i].replaceChild( anchorWrapper, zContentNode ) ;
				
				/*	For an expandable LI, we want the image to respond to clicks. */
				expanderImage.onclick = function() { toggle(this) } ;

				/*	IMG element will be the leftmost element within the LI. */
				zLI[i].insertBefore( expanderImage, zLI[i].firstChild /*anchorWrapper*/ ) ;
			} else {
				/*	Else, we have a terminal LI. */
				
				var anchorItem = zLI[i].getElementsByTagName('a')[0] ;
				
				if( anchorItem != null ) {
					anchorItem.target = "mainframe" ;
				}

				/*	Use hidden expander image to maintain consistent left alignment.
				 	or use your own bullet image. */
				//expanderImage.style.visibility = "hidden" ;
				//zLI[i].insertBefore( expanderImage, zLI[i].firstChild ) ;
				itemBulletImage.style.visibility = (showBulletImage) ? "visible" : "hidden" ;
				zLI[i].insertBefore( itemBulletImage, zLI[i].firstChild ) ;
			}
		}
	}
}

/*	Toggle a child UL element's display property to show or hide it. */
function toggle(aElement)
{
	ulElement = aElement.parentNode.getElementsByTagName('ul')[0] ;
	imgElement = aElement.parentNode.getElementsByTagName('img')[0] ;
	
	if(ulElement) {
		if(ulElement.style.display == 'none' || ulElement.style.display == '') {
			ulElement.style.display = "block" ;
			imgElement.src = "images/eUL-expanded.gif";
		} else {
			ulElement.style.display = "none" ;
			imgElement.src = "images/eUL-collapsed.gif" ;
		}
	} else {
		/* We have a terminal LI */
	}
}

/**
 *	Update the previous and next navigation targets.
 */
function updateNavigation()
{
//	var currentURI = document.getElementById('mainframe').contentWindow.location.href ;
	var currentURI = parent.mainframe.location.href; 
	var zA = document.getElementById('expandableUL').getElementsByTagName('a') ;

	/*	Look for anchor element whose href matches the loaded document in mainframe. */
	for( i = 0; i < zA.length; i++ ) {
		if( zA[i].href == currentURI ) {
			var aElement = zA[i] ;
			aElement.className = "activelink" ; /* was aElement.style.color = "#fff"; */
		} else {
			zA[i].className = "inactivelink" ; /* was zA[i].style.color = "#ffbf52"; */
		}
	}

	/*	Adjust previous and next navigation links with respect to the current
	 	current document in mainframe. */
	if( aElement != null ) {
		var previous = getPreviousSibling( aElement.parentNode ) ;
		var next = getNextSibling( aElement.parentNode ) ;

		var previousLinks = getElementsByClass("navigationPrev", document, "a") ;
		var nextLinks = getElementsByClass("navigationNext", document, "a") ;
		
		if( previous /* != null */ ) {
			for( i = 0; i < previousLinks.length; i++ ) {
				previousLinks[i].style.visibility = "visible" ;
				previousLinks[i].href = previous.getElementsByTagName('a')[0].href ;
				previousLinks[i].firstChild.width=24;
				previousLinks[i].firstChild.height=24;				
			}

			//previousLink.style.visibility = "visible" ;
			//previousLink.href = previous.getElementsByTagName('a')[0].href ;
		} else {
			for( i = 0; i < previousLinks.length; i++ ) {
				previousLinks[i].style.visibility = "hidden" ;
			}
		}

		if( next != null ) {
			for( i = 0; i < nextLinks.length; i++ ) {
				nextLinks[i].style.visibility = "visible" ;
				nextLinks[i].href = next.getElementsByTagName('a')[0].href ;
				nextLinks[i].firstChild.width=24;
				nextLinks[i].firstChild.height=24;				
			}
		} else {
			for( i = 0; i < nextLinks.length; i++ ) {
				nextLinks[i].style.visibility = "hidden" ;
			}
		}
	} else {
		/*	There is no navigation link that corresponds with the page in mainframe. */
	}
}
