/*

Contains the startup and shutdown functions

*/

// Called by onload in the main frameset.
function init()
{
	createCourse()	// Loads in and creates the course, found in coursestructures.js
	//currActiveTrackingObj.alertAll()

	// API_CORE.js, grabs all the needed data from the LMS then sends it to the course object
	API_CORE.initialize();


	createWindowHandler();	// Creates the window handler object, see window_opener.js / window_opener_methods.js for more

	// See if there is any bookmark data, if not, continue with the load sequence as normal.
	if (!checkBookmark())
		checkLoadSequence()

}

// Called from all pages with an exit button, confirms exit, closes all opened windows,
// then closes then learning. Add LMS_commit calls here.
function exit()
{
	result = window.confirm("Are you sure you want to exit?")
	if (result)
	{
		windowTracker.closeAllWindows();
		top.window.close();
	}
}

// This is designed to find a topic ID e.g. m05_t05
function checkBookmark()
{
	// If there was an opener window, check there first as we may be launched from a
	// main course to show ancellery information.
	if (windowTracker.bookmarkData != "")
	{
		currCourse.startTopic(getTopicFromBookmark(windowTracker.bookmarkData))
		// Write bookmark handler code here.
		return true;
	}

	return false;
}

// Split the ID to remove the module. Each module has its own index file and as such the startTopic method isn't expecting that in the ID chain.
function getTopicFromBookmark(whatData)
{
	var bookmarkSplit = whatData.split("_");
	if (bookmarkSplit.length > 2)
		return bookmarkSplit[1] + "_" + bookmarkSplit[2];
	else
		return bookmarkSplit[1]
}


// Instantiates the window handler object. All windows should be opened using this object as it will
// manage the windows and close them all as needed.
function createWindowHandler()
{
	windowTracker = new windowTrackerObject(currModule);
	windowTracker.connectToOpener();
}

// Checks to see what it should try and load currently, menu, section or page.
function checkLoadSequence()
{
/*
	var assessmentTopicFound = currCourse.checkForAssessmentTopic()
	//alert("assessmentTopicFound: " + assessmentTopicFound)
	//assessmentTopicFound.alertAll();
	if (assessmentTopicFound)
	{
		assessmentTopicFound.startTopic();
		return;
	}
*/


	// Check to see there is something at the current position and it has at least 2 entries in the
	// sub array.

	if ((firstLoadSequence[loadSequencePosition]) && (firstLoadSequence[loadSequencePosition][1]))
	{
		switch (firstLoadSequence[loadSequencePosition][0])
		{
			case "menu":
				loadMenu();
			break;
			case "topic":
				currCourse.startTopic()
			break;
			case "page":
				alert("page type not written yet, checkLoadSequence(), frameset_generic.js");
			break;
			default:
				alert("Unknown type of: " + firstLoadSequence[loadSequencePosition][0] + " found in checkLoadSequence(), frameset_generic.js");
			break;
		}
		// See if there is another object in the array, if not, set firstLoadSequenceDone to true
		if (!firstLoadSequence[loadSequencePosition + 1])
			firstLoadSequenceDone = true;
	}
	else	// Throw error
	{
		alert("Attempted to access firstLoadSequence[" + loadSequencePosition + "] failed, entry contained: " + firstLoadSequence[loadSequencePosition] + ", entry is either lacking correct ['type','id'] format or cursor is out of range.\n\nfirstLoadSequence.length: " + firstLoadSequence.length);
	}
}

// Called by the goNextLoadSequence() in generic_nav.js. The next link's href is changed in responce to detecting the
// end of a topic whilst within a load sequence.
function goNextLoadSequence()
{
	if (firstLoadSequence[(loadSequencePosition + 1)])
	{
		//alert("goNextLoadSequence()")
		loadSequencePosition++;
		checkLoadSequence()
	}
}

// Called by the goPrevLoadSequence() in generic_nav.js. The prevs link's href is changed in responce to detecting the
// begining of a topic whilst within a load sequence.
function goPrevLoadSequence()
{
	if (firstLoadSequence[(loadSequencePosition - 1)])
	{
		//alert("goPrevLoadSequence()")
		loadSequencePosition--;
		checkLoadSequence()
	}
}




// Called by the checkLoadSequence() function above, calls the gotoMenu()
// method on the currActiveTrackingObj
function loadMenu()
{
	DEBUG.lert("loadMenu()", 1, currActiveTrackingObj, true)
	currActiveTrackingObj.gotoMenu();
}




// As there is no other form of alert if an attempt to load the xXX_tracking.js,
// this function checks to see if the load was a success and if not, puts up an
// alert. It does this by checking the .trackingFileLoaded property of the currTopicObj
// block. This is set to false before the load and true by _addPageArray()
// in course_object_topic_methods.js which is called by the javascript in the xXX_tracking.js.
//
// This function is called by a setTimeout() from _startTopic() in course_object_topic_methods.js
function checkTopicLoaded()
{
	//alert("currTopicObj.trackingFileLoaded: " + currTopicObj.trackingFileLoaded)
	if (!currActiveTrackingObj.trackingFileLoaded)
	{
		//alert("The topic tracking file for:\n\n'" + currActiveTrackingObj.fullIDString + "'\n\nHas failed to load and as a result this section cannot be viewed.\n\nPlease make sure this section has been built, if the section\nhas already been built, please contact a developer.")

		// Reset the tracking object to what it was before the load so the rest of the content can still be accessed without error.
		if (rollBackTrackingObj)
			currActiveTrackingObj = rollBackTrackingObj;
	}
}
