function PadNum(i) {

	if (i < 10){
		j = '0' + i;
		return j
	}
	else {
		return i;
	}
}


function topNavigator()
{
	// get the current sco 
	var SCO=location.href.match(/\S+\/(\S+)\/(\S+)$/)[1];
	if(SCO.match(/tio\d{1}$/)){
		SCO='tio';
		document.getElementById('subnavigation').style.display='block';
	}
	var AnchorListInit = document.getElementById('navigation')
	var AnchorList = AnchorListInit.getElementsByTagName("a"); 
	for (x in AnchorList)  { 
		if(AnchorList[x].href && 
					  AnchorList[x].href.match(/\S+\/(\S+)\/(\S+)$/) && 
					  AnchorList[x].href.match(/\S+\/(\S+)\/(\S+)$/)[1]==SCO){
			AnchorList[x].className=AnchorList[x].className+'Active';
			AnchorList[x].onblur=null;
		} else if(SCO=='tio' && AnchorList[x].href && AnchorList[x].href.match(/\S+\/(\S+)\d{1}\/\S+$/) && AnchorList[x].href.match(/\S+\/(\S+)\d{1}\/\S+$/)[1]=='tio'){
			AnchorList[x].className=AnchorList[x].className+'Active';
			AnchorList[x].onblur=null;
			subNavigator();
		}
	}
}

function subNavigator()
{
	// get the current sco 
	var SCO=location.href.match(/\S+\/(\S+)\/(\S+)$/)[1];
	
	// get the current sco 
	if (SCO.match(/tio\d{1}$/)) {
		document.getElementById('subnavigation').style.display='block';
		var SubAnchorListInit = document.getElementById('subnavigation')
		var SubAnchorList = SubAnchorListInit.getElementsByTagName("a"); 
		for (y in SubAnchorList)  { 
			if(SubAnchorList[y].href && SubAnchorList[y].href.match(/\S+\/(\S+)\/(\S+)$/)[1]==SCO){
				SubAnchorList[y].className=SubAnchorList[y].className+'Active';
				SubAnchorList[y].onblur=null;
			}
		}
	}
}



//***************************************************************************************
// build page navigation
// pageNum is the current page, totalPages is the total number of pages in the activity
//***************************************************************************************
function LM_TSnav(pageNum, totalPages, mypath) {
	
	pageNum = eval(pageNum); 
	
	var nav_helper = new Navigation(); 
	var navStr = "";

	//Not on the first page, so add previous button 
	if (pageNum > 1) { 
		navStr = '<a href="body' + PadNum(pageNum-1) + '.htm" title="Previous page" id="previous"><em>Back</em><\/a>';
	} 
	else if (nav_helper.is_prev_session() ) { 
			// if there is another session 
		var session = nav_helper.get_prev_session(); 
		navStr += '<a href="' + session[2] + '" title="Previous section" id="previous"><em>Back</em></a>';
		
	}
	// check if to show the next button 
	if (pageNum != eval(totalPages)) { 
		navStr += '<a href="body' + PadNum(pageNum+1) + '.htm" title="Next page" id="next"><em>Next</em></a> ';
	} else if ( nav_helper.is_next_session() ) { 
		var session = nav_helper.get_next_session(); 
		navStr += '<a href="' + session[1] + '" title="Move On" id="moveon"><em>Move On</em><\/a>';		
	} 
	
	return navStr;
}


/** 
 * Class for helping to deal with 
 * navigation between sessions
 */ 
function Navigation() {
	this.Modules = new Object(); 
	
	this.init(); 
}

Navigation.Description = function(title, page, total) { 
	
	return "<span class='page'><b>" + title + "</b> Page " + ((page.substring(0,1) == "0") ? page.substring(1) : page) + " of " + total + "</span>"; 
	
}


/**  
 * Find module and session from the location href
 */ 
Navigation.prototype.init = function() 
{ 
	//alert(location.href);
	var ar_matches = location.href.match(/^.+\/(.*)\/(.*)\/(.*)\/body.+$/); 
	
	if (!ar_matches[1] || !ar_matches[2]) {
		ctad_error("Failed to initialise navigation instance\nUnable to match module and session from location URL"); 
		
		return false; 
	}
	this.$module_name = ar_matches[2]; 
	this.$session_name =  ar_matches[3]; 

	// generate modules 
	var m01 = this.init_module("mowe_ht_e3fract");

	m01.add_session("intro", "../intro/body01.htm", "../intro/body01.htm"); 
	m01.add_session("basics", "../basics/body01.htm", "../basics/body04.htm"); 
	m01.add_session("tio1", "../tio1/body01.htm", "../tio1/body04.htm"); 
	m01.add_session("tio2", "../tio2/body01.htm", "../tio2/body05.htm"); 
	m01.add_session("tio3", "../tio3/body01.htm", "../tio3/body05.htm"); 
	m01.add_session("tips", "../tips/body01.htm", "../tips/body01.htm"); 
	m01.add_session("res", "../res/body01.htm", "../res/body01.htm"); 
	m01.add_session("test", "../test/body01.htm", "../test/body09.htm");



	return true; 
}

/** 
 * Checks if there is a previous session 
 * @return true if sessions exists else false
 */ 
Navigation.prototype.is_prev_session = function() { 
	var module = this.get_current_module();
	if (!module) { 
		ctad_error("Failed to find current module sessions. Have you added them in menu.js? Current session name is = " + this.get_module_name() ); 
		return false; 
	}
		
	if (module.get_prev_session( this.get_session_name() )) 
		return true; 
		
	return false; 
}

/** 
 * Checks if there is a next  session 
 * @return true if sessions exists else false
 */ 
Navigation.prototype.is_next_session = function() { 
	var module = this.get_current_module();
	if (!module) {
		ctad_error("Failed to find current module sessions. Have you added them in menu.js? Current session name is = " + this.get_module_name() ); 
		return false; 
	}
		
	if (module.get_next_session( this.get_session_name() )) 
		return true; 
		
	return false; 
}


/** 
  * Gets the previous session from current session 
  * @return previos session 
  */ 
Navigation.prototype.get_prev_session = function() { 
	if (!this.is_prev_session()) 
		return null; 
	
	return this.get_current_module().get_prev_session( this.get_session_name() );
}

/** 
  * Gets the previous session from current session 
  * @return previos session 
  */ 
Navigation.prototype.get_next_session = function() { 
	if (!this.is_next_session()) 
		return null; 
	
	return this.get_current_module().get_next_session( this.get_session_name() );
}


/** 
  * Initialise a new module instance in the Modules object
  * @return created module instance 
  */ 
Navigation.prototype.init_module = function(name) { 
		
	this.Modules[name] = new NavigationModule(); 
	return this.Modules[name]; 
}


/** 
  * Returns the current modules instance 
  * @return module instance 
  */ 
Navigation.prototype.get_current_module = function() { 
	return this.get_module( this.get_module_name() ); 
}

/** 
 * Returns the module by name 
 * @param name of the module 
 * @return object holding all sessions for module [name]
 */ 
Navigation.prototype.get_module = function(name) { 
	if (!this.Modules[name])
		return null; 
		
	return this.Modules[name];
}

/** 
  * Returns the name of current session 
  * @return current session name 
  */ 
Navigation.prototype.get_session_name  = function() { 
	return this.$session_name; 
}

/** 
  * Returns the name of current module
  * @return current module  name 
  */ 
Navigation.prototype.get_module_name = function() { 
	return this.$module_name; 
}

/** 
  * Returns the next module 
  * @param module name 
  * @return name of next module
  * currently assumes last character is the module number
  */ 
Navigation.prototype.get_next_module = function(module_name) { 
var arr_match=module_name.match(/^(.*)_(.+)$/);
var next_module=arr_match[1]+'_'+(Number(arr_match[2])+1)
if(this.Modules[next_module])
			return next_module; 
			
	return null; 
}

/** 
  * Returns the previous module 
  * @param module name 
  * @return name of next module
  * currently assumes last character is the module number
  */ 
Navigation.prototype.get_previous_module = function(module_name) { 
var arr_match=module_name.match(/^(.*)_(.+)$/);
var prev_module=arr_match[1]+'_'+(Number(arr_match[2])-1)
if(this.Modules[prev_module])
			return prev_module; 
			
	return null; 
}


/** 
  * Module object which holds 
  * all the sessions. 
  * 
  * has some helper functions for easy functionality 
  */ 
function NavigationModule() { 
	this.sessions = Array(); 
}


/** 
 * Adds a new session to the object list 
 * @param name name of new session 
 * @param path to session first page 
 */ 
NavigationModule.prototype.add_session = function(name, path_next, path_prev) { 
	this.sessions.push( [name, path_next, path_prev] )
}

/** 
 * Returns session with its name and path 
 * @param session name 
 * @return session 
 */ 
NavigationModule.prototype.get_session = function(session_name) { 
	
	for (var i = 0; i < this.sessions.length; i++) 
		if ( this.sessions[i][0] == session_name)
			return this.sessions[i]; 
			
	return null; 
}

/** 
  * Returns the next session 
  * @param session name 
  * @return next session from index [session_name]
  */ 
NavigationModule.prototype.get_next_session = function(session_name) { 
	for (var i = 0; i < this.sessions.length; i++) 
		if ( this.sessions[i][0] == session_name && this.sessions[i++])
			return this.sessions[i++]; 
			
	return null; 
}
/** 
  * Returns the previous session 
  * @param session name 
  * @return next session from index [session_name]
  */ 
NavigationModule.prototype.get_prev_session = function(session_name) { 
	for (var i = 0; i < this.sessions.length; i++) 
		if ( this.sessions[i][0] == session_name && this.sessions[i--])
			return this.sessions[i--]; 
			
	return null; 
}

