/*******************************************************************************
	BI Learning Group
	Contact:	Kevin Heald		kevin.heald@biworldwide.com
	Date:		12.08.2004
	Modified:	02.10.2006		Glen Giefer
	Requires:	APIWrapper.js, SCOFunctionsNew.js
	Standard:	SCORM 1.2
	Data Model Support:	core mandatory elements & student_data.mastery_score
*******************************************************************************/

/* Debugger -------------------------------------------------------------------------------------*/
var flash_debug = false; // toggle (true/false)

/* Define Flash Object --------------------------------------------------------------------------*/
// Flash Object Name/ID is hard coded to: swf_sco
function createSWFObject() {
	var swfObj = '';
		swfObj += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" id="swf_sco" width="' + flashAttributes.width + '" height="' + flashAttributes.height + '" align="middle">'
		swfObj += '<param name="allowScriptAccess" value="always" />';
		swfObj += '<param name="movie" value="' + flashAttributes.fileName + '" />';
		swfObj += '<param name="quality" value="high" />';
		swfObj += '<param name="bgcolor" value="' + flashAttributes.backColor + '" />';
		swfObj += '<param name="wmode" value="transparent" />';
		swfObj += '<embed src="' + flashAttributes.fileName + '" quality="high" bgcolor="' + flashAttributes.backColor + '" wmode="transparent" width="' + flashAttributes.width + '" height="' + flashAttributes.height + '" swLiveConnect=true id="swf_sco" name="swf_sco" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		swfObj += '</object>';
		
	return document.write(swfObj);
}

/* FS Command Handler ---------------------------------------------------------------------------*/
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

// Handle all the FSCommand messages in a Flash movie.
function swf_sco_DoFSCommand(command, args) {
	var swf_scoObj = isInternetExplorer ? document.all.swf_sco : document.swf_sco;
	
	// Place your code here.
	var flashArgs = new String(args).split(",");
	var flashCmd = new String(command);
	if (flash_debug) {
		alert("FS Command From Flash: " + flashCmd + "\nFS Command Args From Flash: " + flashArgs);
	}
	if (flashCmd == "loadSCO") {
		swf_scoObj.setVariable("testFS", "fscommand loadSCO"); //TESTING
			
		loadSCO(); // Initialize the SCORM API and set inital tracking values
		// Set variables in Flash file (note, this loop also sets session_time and start_date to flash but these aren't used by flash)
		for (var i in objSCO) {
			if (objSCO[i] != null) swf_scoObj.SetVariable(i, objSCO[i]);
		}
	} else if (flashCmd == "setValue") { // Send tracking data to LMS
		objSCO[flashArgs[0]] = flashArgs[1];
		LMSSetValue(objLMS[flashArgs[0]], objSCO[flashArgs[0]]);
	} else if (flashCmd == "getValue") { // Get tracking data from LMS
		objSCO[flashArgs[0]] = LMSGetValue(objLMS[flashArgs[0]]);
		if (objSCO[flashArgs[0]] != null) {
			swf_scoObj.SetVariable(flashArgs[0], objSCO[flashArgs[0]]);
			//suspend_data is last val got from lms, tell flash that vals are obtained
			if (flashArgs[0] == "suspend_data") swf_scoObj.SetVariable("lmsValsObtained", "true");
		}
		//note that we are not parsing student_name into its component parts, student_first_name, student_last_name, student_mi
	} else if (flashCmd == "commit") {
		LMSCommit(); // Commit data to LMS
	} else if (flashCmd == "exitSCO") {
		if (confirm("Are you sure you wish to exit?")) {
			if (flash_debug) {
				alert("lesson_location is " + swf_scoObj.GetVariable("_root.control.courseObj.curLoc") + 
						", lesson_status is " + swf_scoObj.GetVariable("lesson_status"));
			}
			top.close();
		}
	} else if (flashCmd == "getDebug") { //determine whether to use debugger
		if (flash_debug) alert("in swf_sco_DoFSCommand, content_debug is " + content_debug);
		//be sure to return a string
		var tmpDebug = (content_debug) ? "true" : "false";
		swf_scoObj.SetVariable("content_debug", tmpDebug);
	} else if (flashCmd == "setJS") { //set javascript variables from flash
		if (flash_debug) alert("setJS, flashArgs.length is " + flashArgs.length + ", flashArgs[0] is " + flashArgs[0] + ", flashArgs[1] is " + flashArgs[1]);
		//get variables from flash and populate objSCO
		if (flashArgs[0] == "setSCO") {
			objSCO[flashArgs[1]] = swf_scoObj.GetVariable(flashArgs[1]);
		}

		//set values to LMS,
		//note, this is optional, and when used must be called only after "setJS", "setSCO,...._..."
		if (flashArgs[0] == "setLMS") {
			LMSSetValue(objLMS[flashArgs[1]], objSCO[flashArgs[1]]);
		}
		
		//optional commit
		if (flashArgs[0] == "commitLMS") {
			LMSCommit();
		}
		
		if (flashArgs[0] == "flashAttributes") {
			flashAttributes.courseTitle = flashArgs[1];
			flashAttributes.moduleTitle = flashArgs[2];
		} 
		if (flashArgs[0] == "curLoc") {
			flashAttributes.curLoc = flashArgs[1];
			flashAttributes.curSwf = flashArgs[2];
			flashAttributes.curTopic = flashArgs[3];
		} 
	}
}

//function executed whether from browser exit or flash exit
// use this function when minimal server communication is desired
// NOTE: This function need not be used if lesson sets these values to lms on page navigation
function exitX() {
	if (flash_debug) alert("exitX, objSCO is\n\n" + objSCO);
	
	var api = getAPIHandle();
	if (api != null) {
		//NOTE: scorm properties will be updated in javascript continuously as user navigates through module
		// Send tracking data to LMS
		LMSSetValue("cmi.core.lesson_status", objSCO.lesson_status);
		LMSSetValue("cmi.core.lesson_location", objSCO.lesson_location);
		LMSSetValue("cmi.core.score.raw", objSCO.raw_score);
		LMSSetValue("cmi.suspend_data", objSCO.suspend_data);
	}
}

//----------------------------------
// function called via getURL

// update javascript variable, optionally set value to lms and commit. 
// NOTE: Use appropriate exitSCO based on how values are set and committed. see SCOFunctions.js
// NOTE: isLast is parameter used for custom request to commit on last page of navigation but not on every page of nav
function setSCO(strProp, strOpts, strLoc, strSuspend, strScore, strStatus, isLast) {
	var arrProp = strProp.split(",");
	var arrOpts = strOpts.split(",");
	var set = (arrOpts[0].indexOf("1") != -1);
	var commit = (arrOpts[1].indexOf("1") != -1);
	isLast = (isLast == "1") ? true : false;

	if (flash_debug) {
		var str = "";
		for (var i = 0; i < arguments.length; i++) str += arguments[i] + "\n"; 
		alert("setSCO, arguments are:\n\n" + str + "\nset is " + set + ", commit is " + commit + ", isLast is " + isLast);
	}
	
	//Warning, be careful with arguments length and index into arguments!
	//prevent mismatch of arguments and properties
	if (arrProp.length != arguments.length-3) return;

	for (var i = 0; i < arrProp.length; i++) {
		objSCO[arrProp[i]] = arguments[i+2];
		if (set && lmsInitOk()) LMSSetValue(objLMS[arrProp[i]], objSCO[arrProp[i]]);
	}

	if (set && lmsInitOk()) {
		computeSCOTime();

		if (commit || isLast) LMSCommit();
	}
}