//************************************************************************************************//
//************* UTILITIES ************************************************************************//
//************************************************************************************************//

//****************************************************************************************/
// THIS FUNCTION WILL SET SPECIFIC PROPERTIES OF A FILE, GIVEN THE FILE NAME.
//****************************************************************************************/
function SetFileProperties(name_string){
	this.prefix		=	name_string.substring(0,2);
	this.chapter		=	parseInt(name_string.substring(2,4),10);
	this.navchapter		=	CheckForChapterLetter(this.chapter);
	this.section		=	name_string.substring(4,5);
	this.topic		=	parseInt(name_string.substring(5,7),10);
	this.section_number	=	this.section=="z"?0:parseInt(this.section,36)-9;
	this.string_chapter	=	this.chapter<10?'0'+this.chapter:this.chapter;
	this.string_topic	=	this.topic<10?'0'+this.topic:this.topic;
	this.sharedchapter	=	SetSharedChapter(this.chapter);
	this.directory		=	SetDirectory(this.sharedchapter,this.string_chapter,this.section);
	this.file		=	name_string;
	
	// The path and filename of the chapter opener
	this.chapter_opener_file =	SetChapterOpenerFile(this.sharedchapter,this.string_chapter);
	
	// The path and filename of the section opener
	this.section_opener_file =	SetSectionOpenerFile(this.sharedchapter,this.string_chapter,this.section);
	
}

//****************************************************************************************/
//  THIS FUNCTION SETS THE THE LETTER FOR CHAPTER P AND THE APPENDICES.
//****************************************************************************************/
function CheckForChapterLetter(the_chapter){
	
	switch(the_chapter){
		case 0:
			return "P";
			break;	
		default:
			return the_chapter;
			break;	
	}
	
}

//****************************************************************************************/
//  THIS FUNCTION SETS SHAREDCHAPTER TO TRUE OR FALSE.
//****************************************************************************************/
function SetSharedChapter(the_chapter){
	var shared;

	if ((the_chapter > 5) && (the_chapter < 15)){
		shared = 1;
	}else{
		shared = 0;
	}

	return shared;
}
		
//****************************************************************************************/
//  THIS FUNCTION SETS THE DIRECTORY FOR THE CHAPTER.
//****************************************************************************************/
function SetDirectory(is_shared_chapter,the_string_chapter,the_section){
	var the_directory

	if (is_shared_chapter){
		the_directory = parent.root_path + common_folder_path + 'ch'+ the_string_chapter + '/' + 'ch' + the_string_chapter + the_section +'/';
	}else{
		the_directory = parent.root_path + 'ch'+ the_string_chapter + '/' + 'ch' + the_string_chapter + the_section +'/';
	}

	return the_directory;
}

//****************************************************************************************/
//  THIS FUNCTION SETS THE PATH AND FILE NAME OF THE CHAPTER OPENER.
//****************************************************************************************/
function SetChapterOpenerFile(is_shared_chapter,the_string_chapter){
	var chapter_opener_file
	
	if (is_shared_chapter){
		chapter_opener_file = parent.root_path + common_folder_path + 'ch' + the_string_chapter + '/' + 'ch' + the_string_chapter + 'z' + '/' + 'cc' + the_string_chapter + 'z' + '01';
	}else{
		chapter_opener_file = parent.root_path + 'ch' + the_string_chapter + '/' + 'ch' + the_string_chapter + 'z' + '/' + 'cc' + the_string_chapter + 'z' + '01';	
	}

	return chapter_opener_file;
}

//****************************************************************************************/
//  THIS FUNCTION SETS THE PATH AND FILE NAME OF THE SECTION OPENER.
//****************************************************************************************/
function SetSectionOpenerFile(is_shared_chapter,the_string_chapter,the_section){
	var section_opener_file
	
	if (is_shared_chapter){
		section_opener_file = parent.root_path + common_folder_path + 'ch' + the_string_chapter + '/' + 'ch' + the_string_chapter + the_section + '/' + 'sc' + the_string_chapter + the_section + '01';
	}else{
		section_opener_file = parent.root_path + 'ch' + the_string_chapter + '/' + 'ch' + the_string_chapter + the_section + '/' + 'sc' + the_string_chapter + the_section + '01';
	}
	return section_opener_file;
}
		
//****************************************************************************************/
//  THIS FUNCTION SETS THE NAMES OF THE ON AND OFF STATES OF BUTTONS.
//****************************************************************************************/
function GifButton(gif_name,image_path){
	this.On	 = image_path + gif_name + "_ovr.gif";
	this.Off = image_path + gif_name + "_up.gif";
}
		

		
//****************************************************************************************/
// THIS FUNCTION RETURNS THE FILE NAME GIVEN THE WHOLE PATH.
//****************************************************************************************/
function GetFileName(file_name_path){
	var path_array = file_name_path.split("/");
	path_array.reverse();
	var file_name = path_array[0];

	return file_name;

}


//****************************************************************************************/
// THIS FUNCTION RETURNS AN ITEM FROM A STRING GIVEN THE STRING, THE DELIMITER, AND 
// WHAT ITEM NUMBER OF THE STRING.  
//****************************************************************************************/
function GetItem(the_string, the_delimiter, which_item){
	var item_array = the_string.split(the_delimiter);
	
	if(which_item == "last"){
		item_array.reverse();
		var the_item = item_array[0];
	} else {
		var the_item = item_array[which_item];
	}

	return the_item;

}

//****************************************************************************************/
// THIS FUNCTION RELOADS THE CORRECT HTML INTO THE NAV BAR. THIS FUNCTION NEEDS TO 
// BE ON EVERY HTML PAGE.
//****************************************************************************************/
function DisplayNav(){
	parent.nav.location.replace(parent.navbar_file_path);
}

//****************************************************************************************/
// THIS FUNCTION CREATES THE HTML DYNAMICALLY TO DISPLAY THE NAVIGATION BAR.
//****************************************************************************************/
function DisplayNavLocation(){
	var temp_string = " ";
	var current_file = main.document.location.href;
	var current_file_name = GetItem((GetFileName(current_file)), ".", 0);
	
	//*** SPLASH SCREEN TABLE OF CONTENTS ***//
	if (current_file_name == "calc7esplash"){
		return "Calculus Table of Contents";
	//*** EVERYTHING ELSE ***//	 
	} else {
		var current_file_properties = new SetFileProperties(current_file_name);
		
		//*** CHAPTER CONTENTS PAGE ***//
		if (current_file_properties.prefix == 'cc'){
			temp_string += '<a href = "javascript:parent.LoadTableOfContentsPage()">Calculus Table of Contents</a>';
			temp_string += '&nbsp;&nbsp;&nbsp;&nbsp;'
			temp_string += "Chapter " + current_file_properties.navchapter;
			temp_string += '&nbsp;&nbsp;&nbsp;&nbsp;';
		//*** EVERYTHING ELSE ***//	  
		}else{
			temp_string += '<a href = "javascript:parent.LoadTableOfContentsPage()">Calculus Table of Contents</a>';
			temp_string += '&nbsp;&nbsp;&nbsp;&nbsp;'
			temp_string += '<a href = "javascript:parent.LoadCOPage(';
			temp_string += "'" + current_file_name + "'" + ')">' + "Chapter " + current_file_properties.navchapter + '</a>   ';
			temp_string += '&nbsp;&nbsp;&nbsp;&nbsp;'; 
			
			//*** CHAPTER OPENER(MOTIVATOR) ***//
			if(current_file_properties.prefix=='co'){
				temp_string += 'Chapter Opener';
				temp_string += '&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			
			if(current_file_properties.section_number != 0){
				//*** CONCEPT PAGE ***//
				if((current_file_properties.topic != 0) && (current_file_properties.prefix != 'sc')){
					temp_string += '<a href = "javascript:parent.LoadSOPage(';
					temp_string += "'" + current_file_name + "'" + ')">' + "Section " + current_file_properties.section_number + '</a>   ';
					temp_string += '&nbsp;&nbsp;&nbsp;&nbsp;' 
					if (current_file_properties.prefix == 'cn') temp_string += " Concept " + current_file_properties.topic;
				//*** SECTION CONTENTS PAGE ***//	
        	    }else{
					temp_string += "Section " + current_file_properties.section_number;
					temp_string += '&nbsp;&nbsp;&nbsp;&nbsp;'; 
				}
			}
		
			
			switch(current_file_properties.prefix){
				case "se":
					temp_string += "Section Exercises";
					break;
				case "re":
					temp_string += "Chapter Review Exercises";
					break;
				case "ef":
					temp_string += "Explore It First";
					break;	
				case "lk":
					temp_string += "Look Ahead";
					break;	
				case "ct":
					temp_string += "Connection";
					break;
				case "mt":
					temp_string += "Math Trend";
					break;	
				case "sp":
					temp_string += "Section Project";
					break;			
				case "ps":
					temp_string += "Problem Solving";
					break;
				case "lb":
					temp_string += "Calculus Lab";
					break;		
			}
			
			
		}
		
	}	
	
	return temp_string;

}


