/*************************************************************/
// Script to highlight the current page in a navigation menu 
// - also supports a secondary level (but not a third yet)
// 
// Author: masni bennett
// Version: ICS 2.0
// Last updated: 21/11/06
/*************************************************************/

var url = location.href;
var page = "";

var newsItemPage = false;
var newsBackLink = "newsBackLink";

// Nav menu(s) to check
var leftNavId = "leftmenu";	
var topNavId = "topmenu";

function init() {
	selectNavItem(leftNavId);
	selectNavItem(topNavId);

	// If it's a news item page (one of the news stories),
	// fix up the "back" link according to the previous page
	if (newsItemPage) {
		previousPage = getFileNameFromURL(document.referrer);

		// If the previous page was the healthcare news,
		// change the "return to news" link to go to that page
		if (previousPage == "news_healthcare") {
			document.getElementById(newsBackLink).href = "../news_healthcare.html";
		}
		
		// Otherwise it's just left untouched (ie. the link goes back 
		// to the main news page).
	}
	if (document.images) {
		// Might just preload some hover images, while we're at it...
		var preloader1 = new Image();		preloader1.src = "../images/leftmenu_arrow.gif";
		var preloader2 = new Image();		preloader2.src = "../images/rightmenu_arrow.gif";
		var preloader3 = new Image();		preloader3.src = "../images/toggle_arrow_highlight.gif";
		var preloader4 = new Image();		preloader4.src = "../images/extlink_arrow.gif";
	}
}

function selectNavItem(menuId) {
	page = getFileNameFromURL(url);

	// If we've found the page...
	if (page != "") {
		navMenu = document.getElementById(menuId);
		if (navMenu) 
		{
			listItems = navMenu.getElementsByTagName("A");
	
			// For each link in the menu...
			for (var i=0; i<listItems.length; i++) {
				var temp = "";
	
				// Get the filename
				temp = getFileNameFromURL(listItems[i].href);
				
				// If it matches the current page, set the parent LI class to "highlighted"
				if (page == temp) {
					listItems[i].parentNode.className = "highlighted";
					
					// If the list item's parent's parent is also a list item 
					// (LI > UL > LI), then highlight that too!
					if (listItems[i].parentNode.parentNode.parentNode.tagName == "LI")
						listItems[i].parentNode.parentNode.parentNode.className = "highlighted";
				}
			}
		}
	}
}

// Function to return just the filename from the URL
// (not including the file extension or subfolders)
function getFileNameFromURL(url) {
	
	// News pages hack: If we are looking for the page name, 
	// and the file is within the news directory, then return "news" as the filename
	// so that the news menu item will be highlighted
	if ( url.lastIndexOf("/news/") > 0 && page == "" ) {
		newsItemPage = true;
		return "news";
	}
	if ( url.lastIndexOf("news_eclipse_archive") > 0 ) {
		return "news_eclipse";
	}
	
	// Otherwise, return the proper name!
	else {
		var filename = "";
		var fileExtensionPos = -1;
		
		// Try and find where the file extension starts
		if (url.lastIndexOf(".htm") > 0)
			fileExtensionPos = url.lastIndexOf(".htm");
		else if (url.lastIndexOf(".HTM") > 0)
			fileExtensionPos = url.lastIndexOf(".HTM");
		else if (url.lastIndexOf(".ph") > 0) 
			fileExtensionPos = url.lastIndexOf(".ph");	
		else if (url.lastIndexOf(".") > 0) 
			fileExtensionPos = url.lastIndexOf(".");	
			
		// Find the topmost (root) directory, and get the text between that and the file extension
		if (url.lastIndexOf("/") && fileExtensionPos > 0)
			  filename = url.substring( url.lastIndexOf( "/" ) + 1, fileExtensionPos )
	}
	return filename;
}