// Create page back next sequence object
var pages= new Sequence()

// This creates the mouseover functions and ghosting of the buttons.
// It requires the names of the back and next images.
// It requires the location of the source image for the off, over and ghost states for each image.
 
with (pages){
	setBackImageName 		("back")
	setBackImageSource 		("../../../../images/06_popup/popup-backnext/popup_bn_2.gif")
	setBackImageOverSource 	("../../../../images/06_popup/popup-backnext/popup_bn_2_f2.gif")
	setBackImageGhostSource ("../../../../images/06_popup/popup-backnext/popup_bn_2_ghost.gif")
	setNextImageName 		("next")
	setNextImageSource 		("../../../../images/06_popup/popup-backnext/popup_bn_3.gif")
	setNextImageOverSource 	("../../../../images/06_popup/popup-backnext/popup_bn_3_f2.gif")
	setNextImageGhostSource ("../../../../images/06_popup/popup-backnext/popup_bn_3_ghost.gif")
	setTargetFrameName		("midFrame")
}

// Reinitialises the back next navigation (ghosting) to take of navigation
// that occurs outside of the Sequence object
var initialised = false
function init(){
	if (!initialised){
		pages.initNavigation()
		initialised = true
	}
	id=window.setTimeout("init()",500);
	pages.updateNavigation()
}

//added functions
function goContents(){
	gotoPage(0);
}
function goTips(){
	i = pages.sequence.length - 2;
	gotoPage(i);
}
function goChecklist(){
	i = pages.sequence.length - 1;
	gotoPage(i);
}
function gotoPage(i){
	fileName = pages.sequence[i];
	parent.midFrame.location.href=fileName;
}
function goNext(){
	pages.goThisNext();
}
function goBack(){
	pages.goThisBack();
}
function addPage(seq){
	i = pages.sequence.length
	pages.sequence[i] = seq
}

// The sequence object
function Sequence(){
	this.sequence = new Array()
	this.backimage = ""
	this.backimagesource = ""
	this.backoversource = ""
	this.backghostsource = ""
	this.nextimage = ""
	this.nextimagesource = ""
	this.nextoversource = ""
	this.nextghostsource = ""
	this.addPage = addPage
	this.currentIndex = 0
	this.targetFrameName = ""
	this.goThisBack = goback
	this.goThisNext = gonext
	this.restoreBackImage = restorebackimage
	this.restoreNextImage = restorenextimage
	this.swapBackImage = swapbackimage
	this.swapNextImage = swapnextimage
	this.setCurrentIndex = setCurrentIndex
	this.initNavigation = initnav
	this.updateNavigation = updatenav
	this.setBackImageName = setBackImageName
	this.setNextImageName = setNextImageName
	this.setBackImageSource = setBackImageSource
	this.setNextImageSource = setNextImageSource
	this.setBackImageOverSource = setBackOverImageSource
	this.setNextImageOverSource = setNextOverImageSource
	this.setBackImageGhostSource = setBackGhostImageSource
	this.setNextImageGhostSource = setNextGhostImageSource
	this.setTargetFrameName = setTargetFrameName
	this.isCurrentIndex = isCurrentIndex
	this.error001 = error001
}

// Sequence object functions

// Loads previous page in sequence
function goback(){
	
	if (this.targetFrameName == ""){
		this.error001()
		return
	}
	var frameObj = eval ("parent."+this.targetFrameName)
	this.setCurrentIndex()
	if (this.currentIndex > 0){
		frameObj.document.location.href = this.sequence[this.currentIndex-1]
		document[this.nextimage].src = this.nextimagesource
		this.currentIndex = this.currentIndex-1
		if (this.currentIndex == 0){
			document[this.backimage].src = this.backghostsource
		}
	}
	else{
		document[this.backimage].src = this.backghostsource
	}
	return
}

// Loads next page in sequence
function gonext(){
	if (this.targetFrameName == ""){
		this.error001()
		return
	}
	var frameObj = eval ("parent."+this.targetFrameName)
	this.setCurrentIndex()
	if (this.currentIndex < (this.sequence.length-1)){
		frameObj.document.location.href = this.sequence[this.currentIndex+1]
		document[this.backimage].src = this.backimagesource
		this.currentIndex = this.currentIndex+1
		if (this.currentIndex >= (this.sequence.length-1)){
			document[this.nextimage].src = this.nextghostsource
		}
	}
	else{
		document[this.nextimage].src = this.nextghostsource
	}
	return
}

// Restores the default image for the 'back' button 
function restorebackimage(){
	if (this.currentIndex != 0){
		document[this.backimage].src = this.backimagesource	
	}
	return
}

// Restores the defaultimage for the 'next' button.
function restorenextimage(){
	if ( this.currentIndex < (this.sequence.length-1)){
		document[this.nextimage].src = this.nextimagesource	
	}
	return
}

// Performs image swap for the 'back' button.
function swapbackimage(){
	if (this.currentIndex != 0){
		document[this.backimage].src = this.backoversource
	}
	return
}

// Performs image swap for the 'next' button.
function swapnextimage(){
	if (this.currentIndex <(this.sequence.length-1)){
		document[this.nextimage].src = this.nextoversource
	}
	return
}

// Adds a page to the sequence objects sequence array.
function addThisPage(seq){
	i = this.sequence.length
	this.sequence[i] = seq
}

// Stores the name of the 'back' image. 
function setBackImageName(string){
	this.backimage = string
}

// Stores the name of the 'next' image. 
function setNextImageName(string){
	this.nextimage = string
}

// Stores the name of the 'back' image's default source relative filepath.  
function setBackImageSource(string){
	this.backimagesource = string
}

// Stores the name of the 'next' image's default source relative filepath.  
function setNextImageSource(string){
	this.nextimagesource = string
}

// Preloads the 'over' source of the 'back' image and stores the source relative filepath. 
function setBackOverImageSource(string){
	image1 = new Image();
	image1.src = string;
	this.backoversource = string
}

// Preloads the 'over' source of the 'next' image and stores the source relative filepath. 
function setNextOverImageSource(string){
	image1 = new Image();
	image1.src = string;
	this.nextoversource = string
}

// Preloads the 'ghost' source of the 'back' image and stores the source relative filepath. 
function setBackGhostImageSource(string){
	image1 = new Image();
	image1.src = string;
	this.backghostsource = string
}

// Preloads the 'ghost' source of the 'back' image and stores the source relative filepath. 
function setNextGhostImageSource(string){
	image1 = new Image();
	image1.src = string;
	this.nextghostsource = string
}

// Sets the name of the frame in the frameset in which pages in the sequence will be loaded.
function setTargetFrameName(framename){
	this.targetFrameName = framename
}

// Displays the sequence on the page for debugging purposes.
function displaySequence(sequenceObj){
	document.writeln('<p style="font-family:verdana,tahoma; font-size:10pt; font-weight:600">The page sequence is:</p>')
	for (var i=0; i<sequenceObj.sequence.length; i++){
		document.writeln('<b><span style="font-family:verdana,tahoma; font-size:10pt">' + (i+1) + '.</b> ' + sequenceObj.sequence[i] +'</span><br>')
	}
	document.writeln('</p>')
}

// Used to set current index. 
// This corrects a problem where refresh of the frameset resets the current index to zero.
function setCurrentIndex(){
	var temp = this.sequence[this.currentIndex]
	var actualpage = eval("parent."+this.targetFrameName+".document.URL")
	var indexpage = ""
	
	// This loop extracts the filename from the relative filepath
	for (var z=temp.length; z>=0; z--){
		if(temp.charAt(z)!="/"){
			indexpage += temp.charAt(z)
		}
		else{
			break
		}
	}
	
	temp = indexpage
	indexpage = ""
	for (var y=temp.length; y>=0; y--){
		indexpage += temp.charAt(y)
	}
	
	if (actualpage.indexOf(indexpage)>0){
		return
	}
	else{
		for (var i=0; i<this.sequence.length; i++){
			indexpage = ""
			temp=this.sequence[i]
			
			// This loop extracts the filename from the relative filepath
			for (var j=temp.length; j>=0; j--){
				if(temp.charAt(j)!="/"){
					indexpage += temp.charAt(j)
				}
				else{
					break
				}
			}
			
			temp = indexpage
			indexpage = ""
			for (var k=temp.length; k>=0; k--){
				indexpage += temp.charAt(k)
			}
					
			if (actualpage.indexOf(indexpage)>0){
				this.currentIndex = i
				break;
			}
			
		}
	}
	
	return
}

function isCurrentIndex(){
	var temp = this.sequence[this.currentIndex]
	var actualpage = eval("parent."+this.targetFrameName+".document.URL")
	var indexpage = ""
	
	// This loop extracts the filename from the relative filepath
	for (var z=temp.length; z>=0; z--){
		if(temp.charAt(z)!="/"){
			indexpage += temp.charAt(z)
		}
		else{
			break
		}
	}
	
	temp = indexpage
	indexpage = ""
	for (var y=temp.length; y>=0; y--){
		indexpage += temp.charAt(y)
	}
	
	if (actualpage.indexOf(indexpage)>0){
		return true
	}
	else{
		return false
	}
}

// Enables/disables the navigation buttons depending on
// which page in the sequence is the currently displayed.
// Corrects the problem when someone refreshes the page.
function initnav(){
	this.setCurrentIndex()
	document[this.backimage].src = this.currentIndex == 0 ? this.backghostsource : this.backimagesource
	document[this.nextimage].src = this.currentIndex >= (this.sequence.length-1) ? this.nextghostsource : this.nextimagesource
	return
}

// Enables/disables the navigation buttons depending on
// which page in the sequence is the currently displayed.
// Corrects the problem when someone refreshes the page.
function updatenav(){
	if (!this.isCurrentIndex()){
		this.setCurrentIndex()
		document[this.backimage].src = this.currentIndex == 0 ? this.backghostsource : this.backimagesource
		document[this.nextimage].src = this.currentIndex >= (this.sequence.length-1) ? this.nextghostsource : this.nextimagesource
		return
	}
}

// Displays and error message.
function error001(){
	alert("You have not defined the target frame.\n\nPlease use the '.setTargetFrame()' of your sequence \nobject to define the target frame.\n\neg. mysequence.setTargetFrame(\"MyTargetFrame\")")
}
