/*******************************************************************************
	BI Learning Group
	Contact:	Kevin Heald		kevin.heald@biworldwide.com
	Date:		11.09.2004
	Refactored:	02.10.2006, 10.12.06 (for use with swfobject, reveal alternate lms communication exit strategies)		Glen Giefer
	Requires:	APIWrapper.js
	Standard:	SCORM 1.2
	Data Model Support:	core mandatory elements & student_data.mastery_score
*******************************************************************************/

/* Set Globals ----------------------------------------------------------------------------------*/

var sco_debug = false; // debugger, toggle (true/false)
var lms_initialized = false; // flag to capture whether LMS is successfully initialized

//object that maintains session data
var objSCO = {
	student_name: "",
	lesson_status: "",
	session_time: "",
	student_id: "",
	suspend_data: "",
	raw_score: "",
	lesson_location: "",
	launch_data: "",
	mastery_score: "",	// optional - may not be supported by LMS (not supported by Topclass)
	start_date: "",
		
	toString: function() {
		var str = "";
		for (var i in this) {
			if (i != "toString") str += i + ": " + this[i] + "\n";
		}
		return str;
	}
}

//object used for LMS communication
var objLMS = {
	lesson_status: "cmi.core.lesson_status",
	student_name: "cmi.core.student_name",
	student_id: "cmi.core.student_id",
	lesson_location: "cmi.core.lesson_location",
	raw_score: "cmi.core.score.raw",
	mastery_score: "cmi.student_data.mastery_score",	// optional - may not be supported by LMS (not supported by Topclass)
	launch_data: "cmi.launch_data",
	suspend_data: "cmi.suspend_data"
}

function lmsInitOk() {
	return ((lms_initialized == false) || (lms_initialized.toLowerCase().indexOf("1") == 0 && lms_initialized.length < 3) || lms_initialized.toLowerCase().indexOf("t") == 0);
}

/* Init Communication ---------------------------------------------------------------------------*/
function loadSCO() {
	if (sco_debug) alert("**** in loadSCO() ****\n\n");
	var debugMessage = ""; 
	
	// APIWrapper's LMSInitialize will return false if api not found or 1 if initialized or some other number if an error encountered
	lms_initialized = LMSInitialize();

	//lms_initialized may be either boolean false, or a string indicating success. 
	//	ambiguous whether that will be "1" or "true". other strings would be err codes.
	if (lms_initialized === false) lms_initialized = "api not found";
	if (lms_initialized === "undefined") lms_initialized = "undefined LMSIntitialize failure";

	if (lmsInitOk()) {
		for (var i in objLMS) objSCO[i] = LMSGetValue(objLMS[i]);
		
		// APIWrapper's LMSGetValue will return null if api not found or if an error encountered
		if (objSCO.lesson_status == "not attempted" || objSCO.lesson_status == "" || objSCO.lesson_status == null) {
			objSCO.lesson_status = "incomplete";									// now attempting the SCO
			LMSSetValue("cmi.core.lesson_status", objSCO.lesson_status);
		}
		
		startSCOTimer();
		
		if (sco_debug) alert("**** Debug loadSCO Data ****\n\n" + objSCO)
		if (sco_debug) alert("**** typeof(objSCO.mastery_score) is " + typeof(objSCO.mastery_score) + ", value: |" + objSCO.mastery_score + "| -- ||\n")
	} else {
		if (sco_debug) alert("**** Debug loadSCO Data ****\n\n" + "Unable to initialize LMS:\n" + lms_initialized + "\n\n" + objSCO);
	}
	
//for testing only!!
	//objSCO.lesson_status = "passed";
	//objSCO.raw_score = "80";
//end testing only		
	return lmsInitOk();
}

/* End Communication (set other data model items prior to calling this function) choose the appropriate function!! -----------------*/

//Use this function if lms communication is desired only on exit routine and not handled in course navigation
function exitSCO() {
	if (lmsInitOk()) {
		LMSSetValue("cmi.core.exit", ""); // normal exit
		computeSCOTime(); // format and set session_time
		
		if (sco_debug) alert("**** Debug exitSCO Data ****\n\n" + objSCO);
		
		LMSCommit();
		LMSFinish();
	}
}

/*
//Use this function if all sets and commits are handled in course navigation
// An assumption is made that as setValue with cmi.core.exit is SCORM optional, it is not set. 
// If it is desired to restore this, uncomment appropriately.	
function exitSCO() {
	if (lmsInitOk()) {
		if (sco_debug) alert("**** Debug exitSCO Data ****\n\n" + objSCO);
		//LMSSetValue("cmi.core.exit", ""); // normal exit
		//LMSCommit(); //uncomment if sets are made in course navigation but not commits
		LMSFinish();
	}
}
*/

/* Time Functions -------------------------------------------------------------------------------*/
function startSCOTimer() {
	objSCO.start_date = new Date().getTime();
}

function computeSCOTime() {
	if (objSCO.start_date != 0) {
		var currentDate = new Date().getTime();
		var elapsedSeconds = ((currentDate - objSCO.start_date) / 1000); // convert from milliseconds
		convertTotalSeconds(elapsedSeconds);
	} else {
		objSCO.session_time = "00:00:00";
	}
	LMSSetValue("cmi.core.session_time", objSCO.session_time);
}
	
// this function will convert seconds into hours, minutes, and seconds (00:00:00)
function convertTotalSeconds(ts) {
	var Sec = (ts % 60); var Hour; var Min;

	ts -= Sec;
	var tmp = (ts % 3600);	// # of seconds in the total # of minutes
	ts -= tmp;				// # of seconds in the total # of hours

	Hour = ((ts % 3600) != 0) ? 0 : (ts / 3600); // 3600 sec = 1 hr
	Min = ((tmp % 60) != 0) ? 0 : (tmp / 60); // 60 sec = 1 min	

	Sec = Math.round(Sec); // don't need decimal
	if (("" + Hour).length < 2) { Hour = "0" + Hour; }
	if (("" + Min).length < 2) { Min = "0" + Min; }
	if (("" + Sec).length < 2) { Sec = "0" + Sec; }

	objSCO.session_time = Hour + ":" + Min + ":" + Sec;
}
