// JavaScript Document

//NOTE: This script was edited to handle only the sub nav and ignore the top level nav.

/*################### Current support #####################  
	
1) Activate current navigation elements for up to 2 levels 
   of navigation based on Navigation div ids

2) If current activated sub navigation item has no top level 
   index page in its folder it will search for one up to but 
   not including the site root folder

############## Future support / Improvements ##############

1) Add support for collapsing subnav items

2) Better documentation and variable names

3) Find a better way to seporate path folders, currently
   the find_root variable has to also include the // in 
   http://domain.com/ counting as 3.

3) More then 2 sub navigation divs

   NOTES: More then 2 level support may work if navigation
   		  Divs are put into an array.  The myNav function 
		  will first determine what level it is and call 
		  the appropriate functions to highlight its 
		  parent navigation items if they exist
		  
4) If there is no subnav in the site top nav does not light up for sub pages

5) Account for Home page nav not activation if index.html is not in the URL

6) Add ability to switch images in navigation.
	
	 NOTES: Set up an array to hold the images

##########################################################*/
	
	
	var find_root = 4; 
	/*This variable is the number of / in the URL up to the root folder. To find 
	this number uncomment the next line and open the home page in the root folder
	This must be set and may change if the site moves locations.  If a page other
	then one stored in the root folder is opened with the following line uncommented 
	an invalid find_root number will be displayed*/
	
	//find_root_setting ();
	
	var topLvlNav = 'navigationList'; // The div id of the main nav
	var subLvlNav = 'navigationLeft'; // The div id of the sub nav
	
	url=location.toString();
	pathis=location.toString(); // convert the URL to a string	
	new_array=pathis.split("/"); // move the string into an array seporated by /	
	path_parts=new_array[new_array.length - 2];	//find the current folder of the active page
	
	myNav(); // Call the main function		

function myNav(){
	
	
	
	//Make an array of all Top level navigation items	
	top_nav_links = document.getElementById(topLvlNav).getElementsByTagName('a');
	
	
	
	if(new_array.length <= find_root){	// If in root folder
		//If it is in the root folder of the site it must be the homepage
		in_root_folder();
		//alert ("Page is in root folder");
	}
	else{// If in sub folder and it has a top nav item
		//if(pathis.search('1')==-1){ //If it is not a top level item
			in_sub_folder(); // find it's sub level item and activate it
			//find_index(); // find it's main nav item and activate it		
			//alert ("page is in sub folder and has a parrent");			
		//}
		//else{ // it's in a subfolder but is a top nav item
			//find_index(); // find it's main nav item and activate it
			//alert ("page is in sub folder");
		//}
	}
};

function in_root_folder(){ // It's in the root folder		
	for (var i = 0; i < top_nav_links.length; i++){		
		if(url.search(top_nav_links[i].pathname) != -1){
			
			//document.getElementById(top_nav_links[i].id).className = "on";
		}		
	}
};
function in_sub_folder() {
	//Make an array of all Top level navigation items
	var sub_nav_links = document.getElementById(subLvlNav).getElementsByTagName('a');
	
	for (var i = 0; i < sub_nav_links.length; i++) {
		check_link = new_array[new_array.length - 1];
		check_id = sub_nav_links[i].pathname;
		if(check_id.search(check_link) != -1){
			document.getElementById(sub_nav_links[i].id).className = "on";
			
			path_parts = null;
		}
	}
};

function find_index(){
	var find_index_check = false;
	var current_folder_check = 2;
	while(find_index_check == false){
		//document.write(new_array.length - (find_root + current_folder_check));
		if(new_array.length - (find_root + current_folder_check) < -1){					
				find_index_check = true;
		}
		else{
			for(var i = 0; i < top_nav_links.length; i++) {	
				
				check_link = new_array[new_array.length - current_folder_check]+'/'+'index';
				check_id = top_nav_links[i].pathname;		
				if(check_id.search(check_link) !=-1 ){						
					//document.getElementById(top_nav_links[i].id).className = "on";
					find_index_check = true;						
				}
			}			
		}
		current_folder_check++;
	}
};

function find_root_setting (){
	pathis=location.toString(); // convert the URL to a string	
	new_array=pathis.split("/"); // move the string into an array seporated by /	
	path_parts=new_array[new_array.length - 2];	//find the current folder of the active page
	alert('The correct find_root setting is: '+(new_array.length))
}

// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
} // JavaScript Document
