/**
 * @author Dave March
 * 
 * This file:
 * 1.	Adorns the current left sidebar link with an arrow image,
 * 2.	Boldfaces the currently selected button and turns off hover.
 * 
 * Assumptions:
 * 1.	This file needs to be called out from inside <head> section.
 * 		I am therefore including it in "includes/head_stuff_inc.php".
 * 2.	Name of arrow image is hardcoded.
 * 3.	"id" of sidebar div is: "sidebar".
 * 4.	"id" of menu container <ul> is: "menu".
 * 
 * Notes: 
 * 1.	Since we use 'non-obtrusive' techniques to install the functions,
 * 		we keep HTML/CSS changes to an absolute minimum. 
 * 		See bottom section of this file.
 *
 * 		First released: 2009/08/29 - dmm
 */

/*** globals ***/
var links;

function baseName( path )
{
	if( path.lastIndexOf( '/' ) == -1 ) // no slash so no path.
		return path; // we already have basename so return it.
	return path.substr( path.lastIndexOf('/') + 1 );
}
	
function endsWith( haystack, needle )
{
	// we need the first part otherwise FF will crash on a null string.
	if( haystack == null || haystack.lastIndexOf( needle ) == -1 ) return false;
	
	if( haystack.substr( haystack.lastIndexOf( needle ) ) == needle ) 
	{
		return true;
	}
	return fals
}

function startup( )
{
	var thisFile = baseName( document.location.toString() );
	if( thisFile == '' ) thisFile = "index.php";
	links = document.getElementById( "sidebar" ).getElementsByTagName( "a" );

	// create an IMG element ...
	var img = document.createElement( "img" );
	img.setAttribute( "src", "images/arrow-12x14.png" ); /* +++ */
	img.setAttribute( "align", "absmiddle" );
	img.style.position = "absolute";
	img.style.paddingTop = "2px"; // helps center arrow vertically

	for( var i = 0; i < links.length; i++ )
	{
		// Add an arrow to the link corresponding to the page we're on
		if ( baseName( links[i].getAttribute("href") ) == thisFile ) 
		{
			// add a non-breaking space to separate the left arrow.
			// 'cause regular spaces don't work in FF.
			var inner = links[i].innerHTML;
			links[i].innerHTML = inner + "&nbsp;";
			// Add image after the anchor tag.
			links[i].parentNode.appendChild( img );
		}
	}

	// Boldface menu text corresponding to the page we're on,
	// and suspend color change on hover for that button.
	var buttons = document.getElementById( "menu" ).getElementsByTagName( "a" );
	var topLink = baseName( links[0].getAttribute( 'href' ) );
	for( var i = 0; i < buttons.length; i++ )
	{
		if( baseName( buttons[i].getAttribute( "href" ) ) == topLink )
		{
			var inner = buttons[i].innerHTML;
			buttons[i].innerHTML = "<strong>" + inner + "</strong";
			buttons[i].parentNode.className = "color" + ( i + 1 ).toString();
		}
	}
}

/*********************************************************
 *            Install function(s) on page load 
 *********************************************************/

if (window.addEventListener) // DOM Compliant
{
	window.addEventListener( "load", startup, false )
}
else if (window.attachEvent) // IE
{
		window.attachEvent( "onload", startup );
}
else // Old browser?
{
	window.onload = startup;
}


