//	autonav.js -- Auto-highlighting script
//	Version 1.4
//	Requires yahoo-dom-event.js
//
//	CHANGELOG
//	2008-09-16	Re-introduced navigationList variable, fixed invalid href bug
//				Replaced the replace with a regular expression


var Event = YAHOO.util.Event;
var Dom = YAHOO.util.Dom;
var templateParentPage;
var navigationList = 'menu';
	
Event.onAvailable(navigationList, function() {
		menuItems = Dom.getElementsBy(function() { return true; },'a',navigationList);
		if (!templateParentPage) {
			setActiveMenu(menuItems, extractPageName(document.URL));
		}
		else {
			var link = document.createElement('a');
			link.setAttribute('href', templateParentPage);
			if(link.href) setActiveMenu(menuItems, link.href);
		}
		sublists = Dom.getElementsBy(function() { return true; },'ul',navigationList);
		setActiveList(sublists);
		
});

	function extractPageName(uStr) {
		var currentPage = uStr;
		if (currentPage.indexOf('#') > -1) {
			currentPage = currentPage.substr(0,currentPage.indexOf('#'));
		}
		if (currentPage.indexOf('?') > -1) {
			currentPage = currentPage.substr(0,currentPage.indexOf('?'));
		}
		currentPage.replace(/[default|index].[htm|html|asp|aspx|php]/g, '');
		
		return currentPage;
	}
	

	function setActiveList(arr) {
		var topNode = Dom.get(navigationList);
		for(var i=0; i < arr.length; i++) {
			if (arr[i].id != navigationList) {
				if(!Dom.hasClass(arr[i],'active')) {
					Dom.addClass(arr[i],'inactive');	
				}
				Dom.addClass(Dom.getAncestorByTagName(arr[i],'LI'),'more');
			}
		}
	}
	// search through all the links in array, if one points to
	// the same file, apply the class .active to it and to all its parents
	// until it reaches the top node

	function setActiveMenu(arr, crtPage) {
		var topNode = Dom.get(navigationList);
		
	  for(var i=0; i < arr.length; i++) {
			
		  if((arr[i].href) && (extractPageName(arr[i].href) == crtPage)) {
			Dom.addClass(arr[i],'active');
			currNode = arr[i];
			while(currNode != topNode) {
				Dom.addClass(currNode,'active');
				if (currNode.tagName=='LI') {
					Dom.addClass(Dom.getFirstChild(currNode),'active');
				}
				currNode = currNode.parentNode;
			}
		  }
	  }
	}

	
