/*
 * Products Menu Sidnav
 * 
 * This files compares the current page being viewed with a list of links contained within a div (#productsMenu)
 * If it finds a match it highlights that link to signify to the user that its the current page.
 * 
 * When a match is found it adds the class "selected" to the link.  Style accordingly.
 * 
 * Requirements: mootools.v1.11.js
 * Menu structure: <div id="prodMenu"><ul>...list of links wrapped in li's...</ul></div>
 */

window.addEvent('domready', function(){
	//Process navigation links
	
	//set the default filenames
	var defaultFileNames = ['index.cfm', 'index.html', 'index.htm', 'default.htm', 'default.html', 'default.asp', 'default.aspx'];
	//get the pathname of the current page
	var pathname = window.location.pathname;
	//determine the filename of the current page
	var urlFilename = pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);
	//get the URL minus the protocol
	var urlLink = window.location.hostname + pathname;
	//get the path of the URL minus the filename
	var urlPath = urlLink.substr(0, urlLink.length-urlFilename.length);
	//Path with search characters
	var urlPathSearch = urlLink + window.location.search;
	//URL search characters
	var urlSeach = window.location.search;
	
	
	//Parse the list of sideNav links
	$ES('#menu ul li a').each(function(el){
		
		//reset the highlight URL
		var highlightURL = false;
		
		//get the link minus the protocol
		var aLinkTemp = el.href.substr(el.href.lastIndexOf("//")+2, el.href.length);
		
		//Strip out any hashes off the URL and the ALINK	
			
			//if there was an anchor tag on the alink, strip it out
			if(aLinkTemp.indexOf("#")!='-1'){
			
				//strip out any hashes
				var aLinkHash = aLinkTemp.substr(aLinkTemp.indexOf("#"), aLinkTemp.length);
				//get the link without the protocol and hashes
				var aLinkTemp = aLinkTemp.substr(0, aLinkTemp.length-aLinkHash.length);

			}
			//if there are any search tags, strip them off
			if(aLinkTemp.indexOf("?")!='-1'){
			
				//strip out any hashes
				var aLinkSearch = aLinkTemp.substr(aLinkTemp.indexOf("?"), aLinkTemp.length);
				//get the link without the protocol and search variables
				var aLinkTemp = aLinkTemp.substr(0, aLinkTemp.length-aLinkSearch.length);

			}
		
		//If the URL minus the protocol and anchors equals the aLink minus the protocol and anchors, set it to highlight
		if( aLinkTemp.toUpperCase() == urlLink.toUpperCase() ){
			highlightURL = true;
		}
		//links did not match
		else{
		
			//determine the filename of the alink we are checking
			var linkFilename = aLinkTemp.substr(aLinkTemp.lastIndexOf("/")+1, aLinkTemp.length);
			
			//if the URL with search options matches
			if(aLinkTemp.toUpperCase() == urlPathSearch.toUpperCase()){
				highlightURL = true;
			}
			
			//if the URL has a file, but the link does not
			else if(defaultFileNames.indexOf(urlFilename)>=0 && !linkFilename){
				
				//if the alink equals the URL path minus the filename
				if(aLinkTemp.toUpperCase() == urlPath.toUpperCase()){
					highlightURL = true;
				}
			}
		
			//if the url does not have a filename and the link does
			else if(!urlFilename && linkFilename){

				if(defaultFileNames.indexOf(linkFilename)>=0 && (aLinkTemp.toUpperCase() == urlPath.toUpperCase())){
					highlightURL = true;
				}
			}
			
		}
		
		//If the link should be highlighted, highlight it now 
		if(highlightURL){
			
			//Make the link bold
			el.className = "selected";
			
		}
	});
	
});
