/*

Author: Johnc
Proj No: 60045
Proj Name: BroMo
Usage: Contains functions called by the internal tracking and the external API_CORE files.

Date: 24/10/06

*/

function _addLMSData(whatDataArray)
{
	//this.debug("_addLMSData():whatDataArray",1,whatDataArray,true);
	if (this.parentObj)
	{
		//this.debug("ERROR:_addLMSData() should only be called from the top level course object.",3,false,true);
		return false;
	}
	this.runningFromLMS = true;
	for (var currDataKey in whatDataArray)
	{
		switch (unescape(currDataKey))
		{
			case "_variables[suspend_data]":
				this.addTrackingDataToCourse(unescape(whatDataArray[currDataKey]))
			break;
			default:
				//this.debug("I've found: '" + currDataKey + "', it has a value of :'" + whatDataArray[currDataKey] + "', but I've not been told what to do with it, some I'm ignoring it for now.",2,false,true)
			break;
		}
	}
	this.LMSdataAdded = true;


	/*
	if (API_CORE.get_status() < 1)
		API_CORE.set_status(1)
	*/
}


/*
Takes a string along the lines of:

s30_t05=2&s25_t05=2&s25_t10=2&s20_t05=2&s20_t10=2&s15_t05=2&s10_t05=2&s10_t10=2&s05_t05=2;

And splits it, marking the child objects tracking states as denoted by the string.

NOTE: This should only be called by the course object.
*/
function _addTrackingDataToCourse(whatTrackingDataString)
{
	//this.debug("_addTrackingData():whatTrackingDataString",1,whatTrackingDataString,true);
	if (this.parentObj)
	{
		//this.debug("Warning: _addTrackingDataToCourse() should only be called from the top level course object, I'm finding that and calling this method there.",2,false,true);
		var courseObj = this.getCourseObj();
		courseObj.addTrackingDataToCourse();
		return false;
	}
	var trackingStatesArray = whatTrackingDataString.split("&");
	var count = 0;
	while (count < trackingStatesArray.length)
	{
		var trackingSplit = trackingStatesArray[count].split("=");
		var currObjID = trackingSplit[0];
		//this.debug("_addTrackingDataToCourse()::Looking at: " + currObjID + ", original ref: " + trackingStatesArray[count],2);
		var currObjState = parseInt(trackingSplit[1]);
		var currObj = this.findObject(currObjID);

		if (currObj&&currObj.fullIDString.indexOf("exp")==-1)
		{
			//this.debug("_addTrackingDataToCourse()::Set '" + currObj.fullIDString + "' to a tracking state of: " + currObjState,3);
			currObj.upDateTracking(currObjState);
		}
		else
		{
			//this.debug("ERROR:_addTrackingDataToCourse(). Data for an object with the ID of '" + currObjID + "' was found in the tracking string, however no such object was found in the course structure.",3,false,true);
		}
		count++;
	}
}

/*
Creates a string along the lines of:

s30_t05=2&s25_t05=2&s25_t10=2&s20_t05=2&s20_t10=2&s15_t05=2&s10_t05=2&s10_t10=2&s05_t05=2;

Based on the current topic states.

NOTE: This should only be called by the course object.
*/
function _sendTrackingDataToLMS()
{
	if (this.parentObj)
	{
		//this.debug("Warning: _sendTrackingDataToLMS() should only be called from the top level course object, I'm finding that and calling this method there.",2,false,true);
		var courseObj = this.getCourseObj()
		courseObj.sendTrackingDataToLMS()
		return false;
	}
	if ((this.runningFromLMS) && (this.LMSdataAdded))
	{
		//this.debug("_addTrackingData()",1,false,true);

		var trackingDataArray = new Array();
		var topicObjectsArray = this.getTopicObjects();
		var count = 0;
		while (count < topicObjectsArray.length)
		{
			var currTopic = topicObjectsArray[count]
			trackingDataArray[trackingDataArray.length] = currTopic.parentObj.objID + "_" + currTopic.objID + "=" + currTopic.trackingState
			count++;
		}
		var trackingString = trackingDataArray.join("&");
		//alert('escape("suspend_data"):' + escape("suspend_data"))
		API_CORE.set_variable("suspend_data", trackingString);
	}
	else
	{
		//this.debug("_addTrackingData(), not bothering as I'm not running from an LMS.",1,false,true);
	}
}

function _getTopicObjects()
{
	var topicObjectsArray = new Array()
	if (this.childObjsIndexed)
	{
		var count = 0;
		while (count < this.childObjsIndexed.length)
		{
			// If it has children, then its not a topic so recurse another level.
			//alert(this.childObjsIndexed[count].objID)
			if(this.childObjsIndexed[count].objID.indexOf("exp")==-1)
			{
				if (this.childObjsIndexed[count].childObjsIndexed)
				{
					var returnArray = this.childObjsIndexed[count].getTopicObjects()
					if (returnArray.length > 0)
					{
						topicObjectsArray = this.joinArrays(returnArray, topicObjectsArray)
					}
				}
				else
				{
					//this.debug("Added topic with the ID of '" + this.childObjsIndexed[count].objID + ", found in '" + this.objID + "' in getTopicObjects()",1)
					topicObjectsArray[topicObjectsArray.length] = this.childObjsIndexed[count];
				}
			}
			count++;
		}
	}
	return topicObjectsArray
}

function _exitCourse()
{
	if (this.parentObj)
	{
		//this.debug("Warning: _exitCourse() should only be called from the top level course object, I'm finding that and calling this method there.",2,false,true);
		var courseObj = this.getCourseObj()
		courseObj.exitCourse();
		return false;
	}
	if (!this.runningFromLMS)
	{
		top.window.close();
	}
	else
	{
		this.sendTrackingDataToLMS();
		this.commitLMS();
		this.finalizeLMS();
	}
}


function _commitLMS()
{
	if (this.parentObj)
	{
		//this.debug("Warning: _commitLMS() should only be called from the top level course object, I'm finding that and calling this method there.",2,false,true);
		var courseObj = this.getCourseObj()
		courseObj.commitLMS();
		return false;
	}
	if (this.runningFromLMS)
	{
		API_CORE.commit();
	}
	else
	{
		//this.debug("_commitLMS(), not bothering as I'm not running from an LMS.",1,false,true);
	}
}


function _finalizeLMS()
{
	if (this.parentObj)
	{
		//this.debug("Warning: _finalizeLMS() should only be called from the top level course object, I'm finding that and calling this method there.",2,false,true);
		var courseObj = this.getCourseObj()
		courseObj.finalizeLMS();
		return false;
	}
	API_CORE.finish();
}

function _getCourseObj()
{
	if (this.parentObj)
	{
		return this.parentObj.getCourseObj()
	}
	else
	{
		return this;
	}
}
