DEFAULT_LESSON_FINISH_STATUS = "completed"

/*
var SCOresult=new Array();
SCOresult["W"]="wrong";
SCOresult["C"]="correct";
SCOresult["N"]="neutral";
SCOresult["U"]="unanticipated";

var SCOstatus=new Array();
SCOstatus["F"]="failed";
SCOstatus["P"]="passed";
SCOstatus["I"]="incomplete";
SCOstatus["C"]="completed";
SCOstatus["N"]="not attempted";

var SCOtype=new Array();
SCOtype["T"]="true-false";
SCOtype["C"]="choice";
SCOtype["M"]="matching";
SCOtype["S"]="sequencing";
SCOtype["N"]="numeric";
SCOtype["L"]="likert";
SCOtype["F"]="fill-in";
SCOtype["P"]="performance";
*/

if(window.scormobj!=true){
	var g_StartUpTime=0;
	var findAPITries = 0;

	function findAPI(win)
	{
		 // Check to see if the window (win) contains the API
		 // if the window (win) does not contain the API and
		 // the window (win) has a parent window and the parent window
		 // is not the same as the window (win)
		 while ( (win.API == null) && (win.parent != null) && (win.parent != win) )
		 {
				// increment the number of findAPITries
				findAPITries++;
				// Note: 7 is an arbitrary number, but should be more than sufficient
				if (findAPITries > 7)
				{
					 alert("Error finding API -- too deeply nested.");
					 return null;
				}

				// set the variable that represents the window being
				// being searched to be the parent of the current window
				// then search for the API again
				win = win.parent;
		 }
		 return win.API;
	}

	function getAPI()
	{
		 // start by looking for the API in the current window
		 var theAPI = findAPI(window);
		 // if the API is null (could not be found in the current window)
		 // and the current window has an opener window

		 if ( (theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined") )
		 {
				// try to find the API in the current window's opener
				theAPI = findAPI(window.opener);
		 }
		 // if the API has not been found
		 if (theAPI == null)
		 {
			//create empty API if we fail to get a real one
			theAPI = {
				LMSGetValue: new Function("return '';"),
				LMSGetLastError: new Function("return '';"),
				LMSFinish: new Function("return '';"),
				LMSInitialize: new Function("return '';"),
				LMSCommit: new Function("return '';"),
				LMSSetValue: new Function("return '';"),
				LMSGetErrorString: new Function("return '';"),
				LMSGetDiagnostic: new Function("return '';")
			};

				// Alert the user that the API Adapter could not be found
				top.status = ("Playing in standalone mode");
//				alert("NO API FOUND");
		 }
		 if(g_StartUpTime == 0){
			var D		= new Date();
			g_StartUpTime	= Math.round(D.getTime() / 1000);
		}

		 return theAPI;
	}

	window.sAPI = getAPI();

	function SCOInit() {
		if(window._onload) window._onload();
		if(!sAPI || window._lmsInitialized) return;
		var err = sAPI.LMSInitialize("");
		if(err) window._lmsInitialized = true;
	};

	function SCOFinish () {
		if(!sAPI || window._lmsFinished) return;
		var err;
		//put this in an if statement because if a user closes the browser this can produce an error if the browser destroys the parent frames (API) before this frame
		sAPI.LMSSetValue("cmi.core.lesson_status",DEFAULT_LESSON_FINISH_STATUS);
		err = sAPI.LMSFinish("");
		if(err) window._lmsFinished = true;
	}

	window.eventBroadcaster.addListener("onload","SCORM_INIT",SCOInit);
	window.eventBroadcaster.addListener("onbeforeunload","SCORM_PREFINISH",SCOFinish);
	window.eventBroadcaster.addListener("onunload","SCORM_FINISH",SCOFinish);

	window.scormobj=true;
}