/** 
 * Modified from http://www.richnetapps.com/blog/index.php?p=23&more=1&c=1&tb=1&pb=1
 * Uses javascript to assign the selected class to a link with the href set to the same as the current page.
 *
 * Currently the extractPageName function does not consider subfolders.
 * e.g. news/index.html & blog/index.html will incorrectly highlight both links
 *
 * Usage:
 * Use window.onload or similar or add a script block after the menu with the following
 * 		highlightCurrentPage(menuID, highlightClass);  -- OR --
 *		highlightCurrentPageWithImages(menuID, overFilename, imgDir);
 * Where
 *  - menuID is the id of the menu container.  This can be a table cell, ul, div or any tag that can be given an id
 *  - highlightClass is the css style that is used for menu highlights. 
 * e.g.
 *		highlightCurrentPage('navigationBar', 'selected');
 *		highlightCurrentPageWithImages('navigationBar', '_over', 'files/frontend/images/menu');
 */
// extract the file name from a URL 
//(if input is '/files/index.html', output is 'index')
function extractPageName(hrefString)
{
  var arr = hrefString.split('.');
  if(arr.length >= 2) {
  	arr = arr[arr.length-2].split('/');
  	return arr[arr.length-1].toLowerCase();	
  } else {
	  return "";
  }
}

// search through all the links in array, if one points to
// the same file, apply the class .current to it and to its 
// parent
function setActiveMenu(arr, crtPage, selClass)
{
  for(var i=0; i < arr.length; i++)
  {
    if(extractPageName(arr[i].href) == crtPage)
    {
      arr[i].className = selClass;
      arr[i].parentNode.className = selClass;
    }
  }
}

// call this method from your page
function highlightCurrentPage(menuContainer, selClass)
{
  if(document.location.href) 
    hrefString = document.location.href;
      else
    hrefString = document.location;

    if (document.getElementById(menuContainer)!=null)
    setActiveMenu(document.getElementById(menuContainer).getElementsByTagName('a'), extractPageName(hrefString), selClass);
}

function highlightCurrentPageWithImages(menuContainer, over, imgDir){
	if (document.location.href){ 
    	hrefString = document.location.href;
    } else {
    	hrefString = document.location;
    }
	if (document.getElementById(menuContainer)!=null){
    	arr 		= document.getElementById(menuContainer).getElementsByTagName('a');
		crtPage 	= extractPageName(hrefString);
		for(var i=0; i < arr.length; i++){
			if(extractPageName(arr[i].href) == crtPage){
		  		src = arr[i].firstChild.src;
				src = src.split('.');
				ext = src[src.length-1];
				src = src[(src.length-2)].split('/');
				src = imgDir + src[(src.length-1)] + over + '.' + ext;
		  		arr[i].firstChild.src = src;
			}
	  	}	
	} 
}