/*

Author: Johnc
Proj No: 50181
Proj Name: Flash Construct 2.0
Usage:	Contains the methods required to start a topic, add its page array, parse the page array
				checking for navigation irregularities, load the required first page and navigate the section
				as required by its navigation needs as defined by the screen ID's contained within.

Date: 10/02/06

*/

// Starts the process's required to load a topic by loading the relivant topic_loader.htm into the loaderFrame of index.htm
// From there, the topic_loader.htm loads the topic_tracking.js file, which continues the load sequance when it is completly
// loaded.
function _startBookmarkTopic(moduleID,sectionID,topicID,type, pageNum)
{

	parent.goToBookmark = pageNum;
	if(type=="scenario")
	{
		var loaderFrameRef = contentFrame
		loaderFrameRef.location = this.rootPath + moduleID + "/" + sectionID +"/" + topicID + "/content/scenario.htm";
	}
	if(type=="topic")
	{
		var loaderFrameRef = loaderFrame
		loaderFrameRef.location = this.rootPath + moduleID + "/" + sectionID +"/" + topicID + "/topic_loader.htm";
	}
	this.trackingFileLoaded = false;
	this.topicLoadedChecker = setTimeout("checkTopicLoaded()", 2000)

}


// Starts the process's required to load a topic by loading the relivant topic_loader.htm into the loaderFrame of index.htm
// From there, the topic_loader.htm loads the topic_tracking.js file, which continues the load sequance when it is completly
// loaded.



function _startTopic()
{

	//var loaderFrameRef = document.frames["loaderFrame"];
	var loaderFrameRef = loaderFrame
	var strLoadLocation = "";
	//alert("topicTrackingScriptObj.src: " + topicTrackingScriptObj.src)
	//loaderFrame.location = "content/" + this.fullIDString.replace(/_/gi, "/") + "/" + this.fullIDString + "_tracking.js"

	strLoadLocation = this.rootPath + this.fullIDString.replace(/_/gi, "/") + "/topic_loader.htm";
	if(parent.isTextOnly)
	{
		strLoadLocation = strLoadLocation.replace(this.objID, "text" + this.objID);
	}
	loaderFrameRef.location = strLoadLocation;
	//alert("topicTrackingScriptObj.src: " + topicTrackingScriptObj.src)

	// Used to determine if the xXX_tracking.js file has loaded.
	this.trackingFileLoaded = false;
	this.topicLoadedChecker = setTimeout("checkTopicLoaded()", 2000)


	// At this point, the next action comes from the end of the tracking.js file
	// when it attempts to add itself to the currTopicObj. currTopicObj will then
	// continue the load sequence.
}

function _showSubMenu()
{
	var contentFrameRef = contentFrame;
	var linkLocation = this.rootPath + this.fullIDString.replace(/_/gi, "/") + "/menu/" + this.fullIDString + "_menu.htm";
	if(isTextOnly)
	{
		linkLocation = linkLocation.replace("/menu/", "/textmenu/");
	}
	contentFrameRef.location = linkLocation;

}

function _showSitemap()
{
	var contentFrameRef = contentFrame;
	contentFrameRef.location = this.rootPath + this.fullIDString.replace(/_/gi, "/") + "/" + this.fullIDString + ".htm";

}

function _startTextTopic(whatIDString)
{
	var loaderFrameRef = loaderFrame
	this.fullIDString = this.fullIDString.replace("text","")
	this.fullIDString = this.fullIDString.replace(whatIDString,"text"+whatIDString)
	loaderFrameRef.location = this.rootPath + this.fullIDString.replace(/_/gi, "/") + "/topic_loader.htm";
	this.trackingFileLoaded = false;
	this.topicLoadedChecker = setTimeout("checkTopicLoaded()", 2000)


	// At this point, the next action comes from the end of the tracking.js file
	// when it attempts to add itself to the currTopicObj. currTopicObj will then
	// continue the load sequence.
}


function _startScenario(whatIDString)
{
	var loaderFrameRef = contentFrame

	loaderFrameRef.location = this.rootPath + this.fullIDString.replace(/_/gi, "/") + "/content/scenario.htm";

	this.trackingFileLoaded = false;
	this.topicLoadedChecker = setTimeout("checkTopicLoaded()", 2000)
}

function _startPowerPoint(whatIDString)
{

	var loaderFrameRef = contentFrame

	loaderFrameRef.location = this.rootPath + "powerrpoint/power_pointer_launch.htm";
}

function _startAssessment(whatID)
{
	var newID = whatID.replace(this.objID + "_","")
	alert("newID: " + newID)
	this.startTopic(newID)

}


// Called as part of the load topic sequence. Passed an array which it copies if the .pageArray property is false.
function _addPageArray(whatPageArray)
{
	//this.debug("_addPageArray(whatPageArray: " + whatPageArray + ")",1)
	this.trackingFileLoaded = true;
	clearTimeout(this.topicLoadedChecker)
	if ((!this.pageArray) || (this.isDiagnostic))
	{
		this.pageArray = this.parsePageArray(whatPageArray);
		//alert("this.pageArray:\n\n" + this.pageArray.join("\n"))
	}
	this.currentPageCursor = 0;

	// If its .pageArray is set to false at this point, then it should be a
	// filing cabinet topic, so reset the sub array page counters.
	if (!this.pageArray)
		this.resetFilingCabinetAuxPages();

	if (windowTracker.bookmarkData)
	{
		this.setToBookmark(windowTracker.bookmarkData)
	}

	this.branchsTakenScreenIDArray = false;
	this.loadCurrentPage();
}

// Scans through the page array looking for unusual page types and calling further
// array parsing functions as required.
function _parsePageArray(whatPageArray)
{
	/*
	if (this.objType == "quiz")
	{
		this.isDiagnostic = true;
		this.diagnosticScreenScoresArray = false;
		whatPageArray = this.parseQuizNavArray(whatPageArray)
	}
	*/
	if (this.randomiseQuestions)
	{
		this.isDiagnostic = true;
		this.diagnosticScreenScoresArray = false;
		// A setting used to globaly override the settings in the course structure.
		if (!this.forceOrderInAssessmentScreenQuestions)
		{
			whatPageArray = this.parseQuizNavArray(whatPageArray, true);
		}
		else
		{
			whatPageArray = this.parseQuizNavArray(whatPageArray, false);
		}
	}
	else
	{
		whatPageArray = this.parseQuizNavArray(whatPageArray, false);
	}
	return whatPageArray
}

// Parse's the page array for all questionScreen
function _parseQuizNavArray(whatPageArray, randomizeScreenQuestions)
{
	var tempArray  = new Array()

	//this.debug("parseQuizNavArray(whatPageArray: " + whatPageArray.join("\n") + ")", 1, false, true)

	var count = 0;
	while (count < whatPageArray.length)
	{
		if (whatPageArray[count][3] == "screenQuestion")
			tempArray[tempArray.length] = whatPageArray[count]
		count++
	}

	tempArray = tempArray.sort(this.randomSort)

	var count = 0;
	var tempCount = 0;
	while (count < whatPageArray.length)
	{
		if (whatPageArray[count][3] == "screenQuestion")
		{
			if (randomizeScreenQuestions)
			{
				whatPageArray[count] = tempArray[tempCount]
			}
			//whatPageArray[count][2] = "QUESTION " + (tempCount + 1) + " - " + whatPageArray[count][2]
			whatPageArray[count][4] = (tempCount + 1)
			tempCount++;
		}
		count++;
	}
	//this.debug("parseQuizNavArray return " + whatPageArray.join("\n"), 1, false, true)
	return whatPageArray;
}


// Parses the passed array, copying into temp array's before randomizing and finally
// returning the pruned array.
function _parseDiagnosticNavArray(whatPageArray)
{
	// These are the repositories for all pages that AREN'T diagnostic pages,
	// it is assumed that there are no content pages within the quiz, only start
	// and end.
	var introPagesArray = new Array();
	var outroPagesArray = new Array();
	var copyToIntro = true;

	var diagnosticPagesArray = new Array()	// Used to store the diagnostic pages.

	var finalPagesArray = new Array()				// Used to assemble the final collection of pages.

	var tempArray = new Array()	// Used to store each "area"'s sub array.
	var currArray;							// A reference to the sub array currently being dealt with.
	var count = 0;
	// First copy the pages into the sub array based on a,b,c & d sets.
	while (count < whatPageArray.length)
	{
		// Check its a diagnostic page.
		if (this.checkIsDiagnosticPage(whatPageArray[count][0]))
		{
			// Now we've found a diagnostic, set copyToIntro to false so any further pages
			// that aren't the diagnostic pages are copied into outroPagesArray.
			copyToIntro = false;

			// Each time you find an "a", its time to start a new sub array and fill that one up.
			if (whatPageArray[count][0].indexOf("_a.htm") != -1)
			{
				//alert("Found an A: " + whatPageArray[count][0])
				tempArray[tempArray.length] = new Array()
				currArray = tempArray[tempArray.length - 1]
			}
			currArray[currArray.length] = whatPageArray[count];
		}
		else	// If its not a diagnostic array, then copy it into either the intro or outro pages array.
		{
			if (copyToIntro)
			{
				introPagesArray[introPagesArray.length] = whatPageArray[count]
			}
			else
			{
				outroPagesArray[introPagesArray.length] = whatPageArray[count]
			}
		}
		count++;
	}

	// Now we need to start creating the randomised diagnostic pages array,
	// picking two random items from each sub array. To do this, we simply
	// randomise each sub array and copy the first two. At the end, the new
	// diagnostic pages array is also randomised.
	var count = 0;
	while (count < tempArray.length)
	{
		tempArray[count].sort(this.randomSort);
		diagnosticPagesArray[diagnosticPagesArray.length] = tempArray[count][0];
		diagnosticPagesArray[diagnosticPagesArray.length] = tempArray[count][1];
		count++;
	}
	//alert("diagnosticPagesArray.join() (pre random sort):\n\n" + diagnosticPagesArray.join("\n"))
	diagnosticPagesArray.sort(this.randomSort)

	// Lastly, we concatenate all 3 arrays (diagnosticPagesArray, introPagesArray and outroPagesArray)
	// into 1 array and return that.
	for (var currPage in introPagesArray)
		finalPagesArray[finalPagesArray.length] = introPagesArray[currPage]
	for (var currPage in diagnosticPagesArray)
		finalPagesArray[finalPagesArray.length] = diagnosticPagesArray[currPage]
	for (var currPage in outroPagesArray)
		finalPagesArray[finalPagesArray.length] = outroPagesArray[currPage]

	if (debug)
	{
		//alert("Starting nav array:\n\n" + whatPageArray.join("\n"))
		//alert("Final nav array:\n\n" + finalPagesArray.join("\n"))
	}

	return finalPagesArray;
}

// Checks to see if the _a, _b, _c or _d is present that denotes a page is assigned as a assessment page.
function _checkIsDiagnosticPage(whatScreenID)
{
	if ((whatScreenID.indexOf("_a.htm") != -1) || (whatScreenID.indexOf("_b.htm") != -1) || (whatScreenID.indexOf("_c.htm") != -1) || (whatScreenID.indexOf("_d.htm") != -1))
		return true;
	return false;
}



// Called as part of the load topic sequence. Loads the first page of the section.
function _loadCurrentPage()
{
	// If there is a page array, treat it as a normal eLearning nav array. Any randomization etc will have already been done so the
	// nav array can be navigated in a normal manner.
	if (this.pageArray)
	{
		this.debug("_loadCurrentPage()",1,this,true)

		strLoadLocation = this.rootPath + this.fullIDString.replace(/_/gi, "/") + "/" + this.pageArray[this.currentPageCursor][0];
		if(parent.isTextOnly)
		{
			strLoadLocation = strLoadLocation.replace(this.objID, "text" + this.objID);
		}
		contentFrame.location = strLoadLocation
	}
	else if (this.hasMenu)	// Its a menu
	{
		this.gotoMenu()
	}
	else
	{
		var result = confirm("Unsure how to procede with " + this.fullIDString + " in _loadCurrentPage(), course_object_topic_methods.js.\n\nObject has neither page array to load from nor a menu set to return too.\n\nPress Ok to view the objects properties or Cancel to attempt to continue.")
		if (result)
			this.alertAll();
	}
}


// Checks the page exists, increaments this.currentPageCursor then calls this.loadCurrentPage()
function _nextPage()
{
	//alert("Next")
	if (this.pageArray)
	{
		if (this.pageArray[(this.currentPageCursor + 1)])
		{
			this.currentPageCursor++
			this.loadCurrentPage()
		}
		else
		{
			alert("There is no entry at this.pageArray[" + (this.currentPageCursor + 1) + "], attempt to load page aborted.\n\n_nextPage(), course_object_topic_methods.js")
		}
	}
	else
	{
		alert("Missing page array\nthis.objID: " + this.objID + "\n\n_nextPage(), course_object_topic_methods.js")
	}
}


function _bookmarkPage(pageNum)
{

	if (this.pageArray)
	{
			this.currentPageCursor = (pageNum)
			this.loadCurrentPage()
	}
	else
	{
		alert("Missing page array\nthis.objID: " + this.objID + "\n\n_bookmarkPage(), course_object_topic_methods.js")
	}
}


// Checks the page exists, decreaments this.currentPageCursor then calls this.loadCurrentPage()
function _prevPage()
{
	if (this.pageArray)
	{
		if (this.pageArray[(this.currentPageCursor - 1)])
		{
			this.currentPageCursor--;
			this.loadCurrentPage();
		}
		else
		{
			alert("There is no entry at this.pageArray[" + this.currentPageCursor + "], attempt to load page aborted.\n\n_nextPage(), course_object_topic_methods.js")
		}
	}
	else
	{
		alert("Missing page array\nthis.objID: " + this.objID + "\n\n_nextPage(), course_object_topic_methods.js")
	}

}


// Searchs this.pageArray looking for an entry that contains whatPageID.
// If found it sets this.currentPageCursor to point at the entry then calls
// this.loadCurrentPage();
function _gotoPage(whatPageID)
{
	var pageToSkipTo = false;
	var count = 0;
	while (count < this.pageArray.length)
	{
		if (this.pageArray[count][0].indexOf(whatPageID) != -1)
		{
			pageToSkipTo = true;
			break;
		}
		count++
	}
	if (pageToSkipTo)
	{
		this.currentPageCursor = count;
		this.loadCurrentPage();
	}
	else
	{
		//this.debug("Could not find the ID: " + whatPageID + ", _gotoPage(whatPageID), course_object_topic_methods.js",3,this,true)
	}
}

// Generates a link list of all the pages in the current course object.
function _generatePageLinkList()
{
	if (this.pageArray)
	{
		var count = 0;
		var linksText = "<h3>Link list for " + this.objTitle + "</h3>\n<table cellpadding='5px' cellspacing='0px'>\n";
		var oddRow = true;
		while (count < this.pageArray.length)
		{
			var currPage = this.pageArray[count];
			if (oddRow)
			{
				var currRowClass = "oddLinksRow"
				oddRow = false;
			}
			else
			{
				var currRowClass = "evenLinksRow"
				oddRow = true;
			}
			linksText += "\t<tr valign='top' class='" + currRowClass + "'>\n";
			linksText += "\t\t<td valign='middle'><img src='../../../images/answer_info.gif' alt='" + this.unCamelCase(currPage[3]) + "'/></td>\n";
			linksText += "\t\t<td><a href='javascript:currentTopicObject.gotoPage(\"" + currPage[0] + "\");'>" + currPage[2] + "</a></td>\n";
			linksText += "\t\t<td><span style='float:right;'>(" + currPage[0].replace(".htm","") + ")</span></td>\n";
			linksText += "\t</tr>\n";
			count++;
		}
		linksText += "</table>"
		return linksText
	}
	else
	{
		//this.debug("ERROR:_generatePageLinkList() was called for " + this.fullIDString + ", however there doesn't appear to be a page array to generate links from.",3, this, true)
		return "The course object: " + this.fullIDString + " does not appear to have any pages attached to it.";
	}
}

// Used by the presentation pages to synch
function _synchToCurrentPage(whatPageID)
{
	var pageToSkipTo = false;
	var count = 0;
	while (count < this.pageArray.length)
	{
		if (this.pageArray[count][0].indexOf(whatPageID) != -1)
		{
			pageToSkipTo = true;
			break;
		}
		count++;
	}
	if (pageToSkipTo)
	{
		this.currentPageCursor = count;
		//this.loadCurrentPage();
		//this.debug("Synched to the ID: " + whatPageID + ", _gotoPage(whatPageID), course_object_topic_methods.js",1,this,true)
	}
	else
	{
		//this.debug("Could not find the ID: " + whatPageID + ", _gotoPage(whatPageID), course_object_topic_methods.js",3,this,true)
	}
}

// Checks the current object to see if it has a specific menu, if not
// calls the parent objects .gotoMenu() method.
function _gotoMenu()
{
	//alert(this.hasMenu)
	if (this.hasMenu)
	{
		// Currently the code is set to globaly return to the module menu. As the menu html files are designed
		// to find the current tracking object and returns its menu, the correct content will still be shown.
		// This is to cut down on the amount of manual work required to create a new section, if future projects
		// require individual art directions for different menu screens, use the commented line below instead.
		var currModule = this.fullIDString.split("_")[0]

		try
		{
			if(!parent.isTextOnly)
			{
				contentFrame.location = this.rootPath + currModule + "/menu/" + currModule + "_menu.htm";
			}
			else
			{

				contentFrame.location = this.rootPath + currModule + "/menu/" + currModule + "_textonly_menu.htm";
			}
		}
		catch(err)
		{
		}



		// Individual menu screens for section/topic level menu's, leave commented for now.
		//contentFrame.location = this.rootPath + "/content/" + this.fullIDString.replace(/_/gi, "/") + "/menu/" + this.fullIDString + "_menu.htm";
	}
	else
	{
		if (this.parentObj)
		{
			// Reset the currActiveTrackingObj so that when the menu.htm loads it populates from the correct
			// object.
			currActiveTrackingObj = this.parentObj
			this.parentObj.gotoMenu();
		}
		else
		{
			alert("Unable to return to menu, reached the top of the parent/child chain and no course objects have had .hasMenu set. _gotoMenu(), course_object_topic_methods.js")
		}
	}
}


function _checkForAssessmentTopic()
{
	var assessmentTopicFound = false;
	if (this.childObjsIndexed)
	{
		var count = 0;
		//this.debug("Child objects found. ", 3, this)
		while (count < this.childObjsIndexed.length)
		{
			//this.debug("currChild: ", 3, currChild);
			var currChild = this.childObjsIndexed[count]
			if (currChild.objType == "assessment")
			{
				//this.debug("Assessment Object found: ", 3, currChild)
				assessmentTopicFound = currChild
			}
			if (currChild.childObjsIndexed)
			{
				//this.debug("Recursing.... ", 3, currChild)
				assessmentTopicFound = currChild.checkForAssessmentTopic();
			}
			count++;
		}
	}
	else
	{
		return false;
	}
	return assessmentTopicFound;
}


// Checks the current page array for the ID in the bookmark string and adjusts the page count.
function _setToBookmark(whatBoomark)
{
	// If its a normal eLearning then just set the page number
	// and allow the load sequence to continue as normal.
	if (this.pageArray)
	{
		var count = 0;
		while (count < this.pageArray.length)
		{
			if (this.pageArray[count][0].indexOf(whatBoomark) != -1)
			{
				this.currentPageCursor = count;
				return;
			}
			count++;
		}
	}
	else
		alert("Failed to find a vaild page array for " + this.objID + " in _setToBookmark(), course_object_topic_methods.js")
}
