var scormAPI = null; /* SCORM API */

/* look up through the frameset hierarchy for the SCORM API */
function findScormAPI(win)
{
	while ((win.API == null) && (win.parent != null) && (win.parent != win))
	{
		win = win.parent;
	}
	scormAPI = win.API;
}

/* init the SCORM API */
function initScormAPI(win)
{
	/* look for the SCORM API up in the frameset */
	findScormAPI(win);

	/* if we still have not found the API, look at the opener and it's frameset */
	if ((scormAPI == null) && (win.opener != null))
	{
		findScormAPI(win.opener);
	}
}

function startScormTimer()
	{
	startDate = new Date().getTime();
	}
function computeScormTime()
	{
	var formattedTime = "00:00:00.0";
	if ( startDate != 0 )
   		{
      		var currentDate = new Date().getTime();
      		var elapsedSeconds = ( (currentDate - startDate) / 1000 );
      		formattedTime = convertTotalSeconds( elapsedSeconds );
   		}
	return formattedTime;		
	} 

function convertTotalSeconds(ts)
	{	
	var Sec = (ts % 60);
	ts -= Sec;
	var tmp = (ts % 3600);  //# of seconds in the total # of minutes
	ts -= tmp;              //# of seconds in the total # of hours
	if ( (ts % 3600) != 0 ) var Hour = "00" ;
	else var Hour = ""+ (ts / 3600);
	if ( (tmp % 60) != 0 ) var Min = "00";
	else var Min = ""+(tmp / 60);
	Sec=""+Sec
	Sec=Sec.substring(0,Sec.indexOf("."))
	if (Hour.length < 2)Hour = "0"+Hour;
	if (Min.length < 2)Min = "0"+Min;
	if (Sec.length <2)Sec = "0"+Sec;
	var rtnVal = Hour+":"+Min+":"+Sec;
	return rtnVal;
 	}
	
	// make sure status conforms to scorm standards
function normalizeStatus(status)
	{
	switch (status.toUpperCase().charAt(0))
		{
		case 'C':	return "completed";
		case 'I':	return "incomplete";
		case 'N':	return "not attempted";
		case 'F':	return "failed";
		case 'P':	return "passed";
		}
	return status;
	}
// make sure the question type conforms to scorm standards
function normalizeType(theType)
	{
	switch (theType.toUpperCase().charAt(0))
		{
		case 'T':	return "true-false";
		case 'C':	return "choice";
		case 'F':	return "fill-in";
		case 'M':	return "matching";
		case 'P':	return "peformance";
		case 'S':	return "sequencing";
		case 'L':	return "likert";
		case 'N':	return "numeric";
		}
	return theType;
	}
// make sure the question result conforms to scorm standards
function normalizeResult(result)
	{
	switch (result.toUpperCase().charAt(0))
		{
		case 'C':	return "correct";
		case 'W':	return "wrong";
		case 'U':	return "unanticipated";
		case 'N':	return "neutral";
		}
	return result;
	}
	
// Handle all the the FSCommand messages in a Flash movie
function Synchro_DoFSCommand(command, args) 
	{
  var testObj = document.Synchro;
	
	args 				= String(args);
	command 			= String(command);
	var F_intData 	= args.split(";");
	
	// check for existence of scorm api
	if (scormAPI == null)
		return;
	
	switch (command)
		{
		case "MM_StartSession" :
			break;
		
		case "MM_cmiSendInteractionInfo" :
			//MM_cmiSendInteractionInfo(date, time, intid, objid, intrtype, correct, student, result, weight, latency)
			var	n	= scormAPI.LMSGetValue("cmi.interactions._count");
			scormAPI.LMSSetValue("cmi.interactions." + n + ".id", F_intData[2]);
			scormAPI.LMSSetValue("cmi.interactions." + n + ".objectives.0.id", F_intData[3]);
			scormAPI.LMSSetValue("cmi.interactions." + n + ".time", F_intData[1]);
			scormAPI.LMSSetValue("cmi.interactions." + n + ".type", normalizeType(F_intData[4]));
			scormAPI.LMSSetValue("cmi.interactions." + n + ".correct_responses.0.pattern", F_intData[5]);
			scormAPI.LMSSetValue("cmi.interactions." + n + ".weighting", F_intData[8]);
			scormAPI.LMSSetValue("cmi.interactions." + n + ".student_response", F_intData[6]);
			scormAPI.LMSSetValue("cmi.interactions." + n + ".result", normalizeResult(F_intData[7]));
			scormAPI.LMSSetValue("cmi.interactions." + n + ".latency", F_intData[9]);
			break;
			
		case "MM_cmiSendObjectiveInfo" :
			//MM_cmiSendObjectiveInfo(index, objid, score, status)
			var	n	= scormAPI.LMSGetValue("cmi.objectives._count");
			scormAPI.LMSSetValue("cmi.objectives." + n + ".id", F_intData[1]);
			scormAPI.LMSSetValue("cmi.objectives." + n + ".score.raw", F_intData[2]);
			scormAPI.LMSSetValue("cmi.objectives." + n + ".status", normalizeStatus(F_intData[3]));
			break;
			
		case "CMISetScore":
		case "MM_cmiSendScore" :
			//MM_cmiSendScore(theScore)
			scormAPI.LMSSetValue("cmi.core.score.raw", F_intData[0]);
			break;
			
		case "CMISetStatus":
		case "MM_cmiSetLessonStatus" :
			//MM_cmiSetLessonStatus(theStatus)
			scormAPI.LMSSetValue("cmi.core.lesson_status", normalizeStatus(F_intData[0]));
			break;
			
		case "CMISetTime" :
			//CMISetTime(t)
			scormAPI.LMSSetValue("cmi.core.session_time", F_intData[0]);
			break;
		
		case "CMISetCompleted" :
			//CMISetCompleted()
			scormAPI.LMSSetValue("cmi.core.lesson_status", "completed");
			break;
		
		case "CMISetStarted" :
			//CMISetCompleted()
			scormAPI.LMSSetValue("cmi.core.lesson_status", "incomplete");
			break;
			
		case "CMISetPassed":
			//CMISetCompleted()
			scormAPI.LMSSetValue("cmi.core.lesson_status", "passed");
			break;
			
		case "CMISetFailed":
			//CMISetCompleted()
			scormAPI.LMSSetValue("cmi.core.lesson_status", "failed");
			break;
			
		case "CMISetData":
			//CMISetData(data)
			scormAPI.LMSSetValue("cmi.suspend_data", F_intData[0]);
			break;
			
		case "CMISetLocation":
			//CMISetLocation(loc)
			scormAPI.LMSSetValue("cmi.core.lesson_location", F_intData[0]);
			break;
			
		case "CMISetTimedOut":
			//CMISetTimedOut()
			scormAPI.LMSSetValue("cmi.core.exit", "time-out");
			break;
		
		case "CMIInitialize":
			//CMIInitialize()
			scormAPI.LMSInitialize(args);
			break;
		
		case "CMIFinish":
			break;
			
		case "CMIExitAU":
			scormAPI.LMSFinish(args);
			break;
		}
	// END OF CMI FUNCTION MAPPING
	}