// JavaScript Document
//
// highlightSection(idToSearch, highlightClass, idToIgnore)
//
// Applies a style to the link that "looks like" the current section.  Used
// with CSS to simulate the "current section" feeling in navigation.
// 
// Assumes the URL with the fewest slashes is the current section (e.g.,
// index.html over ../people/index.html).  See _getSectionLink (below) for
// details.  Ignores the $idToIgnore when choosing a link, usually used to
// prevent the back to home link from getting highlighted.
//
function highlightSectionLi(idToSearch, highlightId, idToIgnore) {
	if(!document.getElementById) return ;							// browser detect DOM support
	
	// Get the section link candidate
	sectionLink = _getSectionLinkLi(idToSearch, idToIgnore);
	
	// If we found one...
	if(sectionLink != null) {
		// Highlight it
		sectionLink.id = highlightId ;
		
	// ...else we didn't find one, try to highlight the current page
	} else {
		var selfLinks = _getSectionLinkLi(idToSearch) ;
		
		// If there is only one candidate...
		if(selfLinks.length == 1) {
			// ...and it is not the $idToIgnore
			if(selfLinks[0].id != idToIgnore) {
				// Highlight it
				selfLinks[0].id = highlightId ;
			}
		}
	}
	
	var imgContainer = $("#highlight").parent();
	imgContainer.children('img').css('visibility','visible');
}

function getSectionTitle(idToSearch) {
	if(!document.getElementById) return null ;						// browser detect DOM support
	
	// get the container we should be searching
	var container = document.getElementById(idToSearch) ;			// get the container
	if(container==null) return null ;								// bail if we can't find it
	
	
	// init variables
	var fewestSlashes = -1 ;
	var fewestSlashesCount = -1 ;
	var sectionCandidate = null ;
	
	var container = document.getElementById(idToSearch);			// get the containers
	if(container==null) return new Array() ;						// bail if we can't find it
	
	var anchors = container.getElementsByTagName("a");
	for(var i=0; i<anchors.length; i++) {								// iterate anchors...
		var linkCandidate = anchors.item(i);
		var href = linkCandidate.getAttribute("href", 2) ;
		var slashCount = _substrCount(href, "/") ;
		
		// only consider cases where href start with ".." or there are no slashes ("index.html")
		// and the $idToIgnore is undefined or not the current link
		if(href.indexOf("..") == 0 || slashCount == 0) {
			if((slashCount < fewestSlashes || fewestSlashes == -1)) {
				fewestSlashes = slashCount ;
				fewestSlashesCount = 1 ;
				candidateTitle = linkCandidate.innerHTML;
			} else if(slashCount == fewestSlashes) {
				fewestSlashesCount++ ;
				/*candidateTitle = null;*/
			}
		}
	}																//...iterate anchors
	
	return candidateTitle.toLowerCase();
}

function addSubHighlight(idToSearch, highlightClass) {
	if(!document.getElementById) return ;							// browser detect DOM support
	
	var selfLink = _getSelfLinkObj(idToSearch) ;
	selfLink.className = highlightClass ;
}

//====== Private Functions ======//

//
// _getSectionLink(idToSearch, idToIgnore)
//
// Only works when using document-realtive links in $idToSearch.
//
// Returns the DOM anchor object in $idToSearch, as long as it is not
// $idToIgnore, most likely to be the current section for this page.  The
// algorithm assumes the link with the fewest slashes is the link to this
// section (e.g., index.html over ../people/index.html).  Ignores links
// that don't start with ../, unless there are no slashes in the URL.  
// Returns null if there are more than one possible matches.
//
function _getSectionLinkLi(idToSearch, idToIgnore) {
	if(!document.getElementById) return null ;						// browser detect DOM support
	
	// get the container we should be searching
	var container = document.getElementById(idToSearch) ;			// get the container
	if(container==null) return null ;								// bail if we can't find it
	
	// init variables
	var fewestSlashes = -1 ;
	var fewestSlashesCount = -1 ;
	var sectionCandidate = null ;
	
	var anchors = container.getElementsByTagName("a") ;				// get all anchors
	for(var i=0; i<anchors.length; i++) {								// iterate anchors...
		var linkCandidate = anchors.item(i) ;							// this candidate
		
		var href = linkCandidate.getAttribute("href", 2) ;
		var slashCount = _substrCount(href, "/") ;
		
		// only consider cases where href start with ".." or there are no slashes ("index.html")
		// and the $idToIgnore is undefined or not the current link
		if(href.indexOf("..") == 0 || slashCount == 0) {
			if((slashCount < fewestSlashes || fewestSlashes == -1) && (idToIgnore == null || linkCandidate.id != idToIgnore)) {
				fewestSlashes = slashCount ;
				fewestSlashesCount = 1 ;
				sectionCandidate = linkCandidate ;
			} else if(slashCount == fewestSlashes) {
				fewestSlashesCount++ ;
				/*sectionCandidate = null ;*/
			}
		}
	}																//...iterate anchors
	
	return sectionCandidate ;
}

function _getSelfLinkObj(idToSearch) {
	if(!document.getElementById) return new Array() ;				// browser detect DOM support
	
	var selfLink = new Array() ;									// the return array
	var url = newURLFromLocationAndDocument(location, document) ;	// the URL of this document
	
			// get the container we should be searching
	var container = document.getElementById(idToSearch);			// get the containers
	if(container==null) return new Array() ;						// bail if we can't find it
	
	var anchors = container.getElementsByTagName("a");
	for(var i=0; i<anchors.length; i++) {								// iterate anchors...
		var linkCandidate = anchors.item(i) ;							// this candidate
		var candidateHREF = linkCandidate.getAttribute("href") ;		// get the href
				
		if(candidateHREF != null && !candidateHREF.indexOf("#")==0) {	// Ignore cases where candidate HREF is null or a jump link to this page...
			var joinedHREF = url.joinHREF(candidateHREF) ;					// relative to this URL
			if(url.equalsHREF(joinedHREF)) {								// if equal...
				selfLink = linkCandidate.parentNode;					// add it to return array
			}																// ...if equal
		}																// ...end ignore cases
	}																//...iterate anchors
	
	return selfLink;
}

function highlightTables(color) {
	var table = document.getElementsByTagName("tr");
	
	for(var i = table.length - 1; i >= 0; i--) {
		if((i - 1) % 2 == 0) {
			table.item(i).style.backgroundColor = color;	
		}
	}
}

