//	autonav.js -- GSSI Auto-highlighting script
//	http://www.graphicallyspeaking.ca/
//	Version 1.7.0
//	Requires yahoo-dom-event.js
//
//	CHANGELOG
//	2009-01-12	Fixed still-broken regex
//				Added auto-breadcrumbs
//	2008-12-09	Fixed templateParentPage index page bug
//	2008-11-04	Removed short variables pointing to YAHOO namespaces 
//				(causing variable naming conflict with Prototype)
//  2008-10-30  Fixed violently broken regex in extractPageName() -- Damien
//  2008-10-29	Added IE6 / IE7 bug fix for templateParentPage
//  2008-10-23	Added style block that hides navigation until classes are added.
//	2008-09-16	Re-introduced navigationList variable, fixed invalid href bug
//				Replaced the replace with a regular expression


var templateParentPage;
var navigationList = 'subnav';
	
document.write('<style type="text/css"> #' + navigationList + ' { display: none; } </style> ');

YAHOO.util.Event.onAvailable(navigationList, function() {
		menuItems = YAHOO.util.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) {
				var newLink = '';
				if(link.href.indexOf(document.domain) == -1) {
					if(link.href.indexOf('https://') != -1)
						newLink = 'https://' + document.domain + link.href;
					else
						newLink = 'http://' + document.domain + link.href;
				}
				else
					newLink = link.href;
				setActiveMenu(menuItems, extractPageName(newLink));
			}

		}
		sublists = YAHOO.util.Dom.getElementsBy(function() { return true; },'ul',navigationList);
		setActiveList(sublists);
		
		YAHOO.util.Dom.get(navigationList).style.display = 'block';
		var breadcrumbs = YAHOO.util.Dom.get('breadcrumbs');
		if(breadcrumbs) buildBreadcrumbs(breadcrumbs);
});

	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 = currentPage.replace(/(default|index)\.(html|htm|aspx|asp|php)/, '');
		
		return currentPage;
	}
	

	function setActiveList(arr) {
		var topNode = YAHOO.util.Dom.get(navigationList);
		for(var i=0; i < arr.length; i++) {
			if (arr[i].id != navigationList) {
				if(!YAHOO.util.Dom.hasClass(arr[i],'active')) {
					YAHOO.util.Dom.addClass(arr[i],'inactive');	
				}
				YAHOO.util.Dom.addClass(YAHOO.util.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 = YAHOO.util.Dom.get(navigationList);
		for(var i=0; i < arr.length; i++) {
			  if((arr[i].href) && (extractPageName(arr[i].href) == crtPage)) {
				YAHOO.util.Dom.addClass(arr[i],'active');
				var currNode = arr[i];
				while(currNode != topNode) {
					YAHOO.util.Dom.addClass(currNode,'active');
					if (currNode.tagName=='LI') {
						YAHOO.util.Dom.addClass(YAHOO.util.Dom.getFirstChild(currNode),'active');
					}
					currNode = currNode.parentNode;
				}
			  }
	  }
	}
	function buildBreadcrumbs(target) {
		var els = YAHOO.util.Dom.getElementsByClassName('active','a',navigationList);
		var bStr = '';
		
		/*if (els.length == 0 && !templateParentPage) {
			bStr = '<span>Home</span>';
		}
		else {
			bStr = '<a href="/">Home</a>';
		}*/
		for (var i=0; i<els.length; i++) {
			if (i == els.length-1 && !templateParentPage) {
				bStr = bStr + ' <span>' + els[i].innerHTML + '</span> &#62;';
			}
			else {
				bStr = bStr + ' <a href="' + els[i].href + '">' + els[i].innerHTML + '</a> &#62;';
			}
		}
		
		if (templateParentPage) {
			bStr = bStr + ' <span>' + thisPageName + '</span>';
		}
		
		bStr = '<a href="/">Home</a> &#62; ' + bStr;
		
		var idx = bStr.lastIndexOf('&#62;');
		//alert(idx + ' ' + bStr.length + ' ' + (idx+5 == bStr.length));
		if(idx+5 == bStr.length)
			target.innerHTML = bStr.substr(0, idx);
		else
			target.innerHTML = bStr;
	}

	
