window.addEvent('domready', function(){
	
	//Extend the element class to include ancestors of an element
	Element.extend({
	    ancestors: function () {
	        var result = [], el = this;
	        while (el = el.getTag() !== 'html' ? el.getParent() : false) result.push(el);
	        return $$(result);
	    }
	});
	//Extend the element class to include offspring (children)  of an element
	Element.extend({
	    offspring: function () {
	        var result = [], el = this;
	        if(el.getChildren()) result= el.getChildren();
	        return $$(result);
	    }
	});
	
	//Collapsable Sections	
	
		//list of target elements
		var sections = $$('#dynamicNav div.collapseSection');
		//list elements to be clicked on
		var headings = $$('#dynamicNav h4.collapseHeading');
		//array to store all of the collapsibles
		var collapsibles = new Array();

		//create collapse effects and hide collapseSections
		headings.each( function(heading, i) {
		
			//for each element create a slide effect
			var collapsible = new Fx.Slide(sections[i], {
					duration: 250,
					transition: Fx.Transitions.linear
			});
	
			//and store it in the array
			collapsibles[i] = collapsible;
	
			//add event listener
			heading.onclick = function(){
					collapsible.toggle();
					
					//hide the rest
	                for(var j = 0; j < collapsibles.length; j++){
	                        if(j!=i) collapsibles[j].slideOut();
	                }
					return false;
			}
			
			//Set the heading title to inform the user of click functionality
			heading.title = "Click to expand and collapse";
			
			//collapse all of the list items
			if($('dynamicNav').className == "collapsed"){
				(function(){
					collapsible.hide();
				}).delay(10);
			}
			else if($('dynamicNav').className =="expanded"){
				(function(){
					collapsible.show();
				}).delay(10);
			}	
			
		});
		
		//Used for a Collapse-All link to collapse the entire nav
		if($('collapse-all')){
			$('collapse-all').onclick = function(){
				headings.each( function(heading, i) {
					collapsibles[i].hide();
				});
				return false;
			}
		}
		//Used for an expand all link to expand all sections	
		if($('expand-all')){
			$('expand-all').onclick = function(){
				headings.each( function(heading, i) {
					collapsibles[i].show();
				});
				return false;
			}
		}		
	
	//Process navigation links
	
		//Hide subBulletLists
		var subLists = $$('.subBulletList').setStyle('display', 'none');
		//used to hold the section id to auto expand nav sections
		var sectionNum;
		//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;
		
		//create the array that will hold the section ids for expanding when finished
		var expandSections = new Array(0);
		
		//Parse the list of sideNav links
		$ES('#dynamicNav 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 there were not search parameters in the url
			if (!aLinkSearch) {
				//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;
				}
			}
			//If there were search parameters in the url
			else {
				//If the URL with the search parameters, but minus the protocol and anchors equals the aLink with the search parameters minus the protocol and anchors, set it to highlight
				if ((aLinkTemp.toUpperCase() + aLinkSearch.toUpperCase()) == urlPathSearch.toUpperCase()) {
					highlightURL = true;
				}
			}
			
			//links did not match
			if(!highlightURL){
			
				//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){
				
				var tabContent ="";
				
				//check if the link is in a collapsed list
				var myAncestors = $(el).ancestors();

				//loop through the ancestors
				myAncestors.each( function(ancestor, i) {
					
					//if the ancestory if the nav section
					if(ancestor.className == "collapseSection"){

						//get the section number from the id
						sectionNum = ancestor.id.substring(7, ancestor.id.length).toInt();
						
						//store the section number for expansion when parsing is complete
						if(sectionNum>=0){
							expandSections.push(ancestor.id.substring(7, ancestor.id.length).toInt());
						}
					}
					
				
					//check to see if the link is a tabContent page
					if(ancestor.className == "tabContent"){
						tabContent = true;
					}
					
					//Show the subBulletList containing the link
					if(ancestor.className == "subBulletList"){
						ancestor.setStyle('display', '');
					}
					//convert the arrow list item to a selected arrow
					if(ancestor.className == "arrow"){
						
						
						if(tabContent){
							ancestor.className = "tabContentList Selected";
						}
						else{
							ancestor.className ="selectedarrow";
							var myOffspring = $(ancestor).offspring();
							if(myOffspring){
								myOffspring.each( function(kid) {
									if(kid.className == "subBulletList"){
										kid.setStyle('display', '');
										
										var kidOffspring = $(kid).offspring();
										
										if(kidOffspring){
											kidOffspring.each( function(grandkid){
												if(grandkid.className == "tabContent"){
													ancestor.className = "tabContentList Selected";
												}
											});
										}
									}
								});
							}
						}
					}
					
					
				});
				
				//Make the link bold
				el.className = "Selected";
				
			}
		});
		
		//now we expand any section that contained a link.
		expandSections.each(function(section){
			if(!isNaN(section)){
				(function(){
					collapsibles[section].show();
				}).delay(1);
			}
		});
	
});
