var g_objAPI 	         = null;	// Reference to API
var g_initAPICount       = 0;		// Count API init attempts
var g_boolInitializeDone = false;	// Indicates if initialization has been done
var g_varInterval        = "";		// Reference for interval
var g_intAPIOrder        = 1;		// Inidcates search order for looking for API window. 
					// 0 - start from current window and work way up
					// 1 - start from top window and work way down
var g_strAPIVersion      = -1;		// Holds the version of the found API
var g_dtInitialized      = new Date();	// Holds time stamp for initialize
var g_boolAPIInitialized = false; 	// Use to determine if API initialized.
var g_boolFinishDone     = false;	// Indicates that SCORM close is complete
var g_boolIsIE           = false;	// Indicates if running in IE

	g_boolIsIE = navigator.appName.indexOf("Microsoft") != -1;

function getFlashMovieObject()
{
	if (window.document["player"])
		return(window.document["player"]);

	if (g_boolIsIE == false)	// (navigator.appName.indexOf("Microsoft Internet") == -1)
	{
		if ((document.embeds) && (document.embeds["player"]))
			return(document.embeds["player"]);
	}
	else 			// if (navigator.appName.indexOf("Microsoft Internet") != -1)
		return(document.getElementById("player"));
}

function SearchForSCORM()
{
	g_varInterval = setInterval("InitializeAPI()", 300);
}

function FindTheAPI(win)
{
	// Search the window hierarchy for an object named "API" for SCORM 1.2
	// Look in the current window (win) and recursively look in any child frames

	if (win.API != null)
		return(win.API);

	if (win.length > 0)  // check frames
	{
		for (var lLoop = 0; lLoop < win.length; lLoop++)
		{
		var objAPI = FindTheAPI(win.frames[lLoop]);
		
			if (objAPI != null)
				return(objAPI);
		}
	}

	return(null);
}

function getAPI(intAPISearchOrder)
{
var objAPI = null;

	intAPISearchOrder = ((typeof(intAPISearchOrder) == 'undefined') ? 0 : intAPISearchOrder);

	if (intAPISearchOrder == 0)
	{
		// start and the current window and recurse up through parent windows/frames

		objAPI = FindTheAPI(window);

		if ((objAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
			objAPI = FindTheAPI(window.opener);
		else if ((objAPI == null) && (window.parent != null) && (typeof(window.parent) != "undefined"))
			objAPI = FindTheAPI(window.parent);
	}
	else
	{
		// start at the top window and recurse down through child frames

		objAPI = FindTheAPI(this.top);

		if (objAPI == null)
		{
		var objTopWindow;

			// the API wasn't found in the current window's hierarchy.  If the
			// current window has an opener (was launched by another window),
			// check the opener's window hierarchy.

			objTopWindow = window.top;
			objTopWindow = objTopWindow.opener;

			while (objTopWindow && !objAPI)
			{
				objAPI = FindTheAPI(objTopWindow.top);	// checking window opener

				if (objAPI == null)
					objTopWindow = objTopWindow.opener;
			}
		}
	}

	if (objAPI == null)
	{
		// can't find API
	}
	else if ((objAPI != null) && (g_strAPIVersion == -1))
		g_strAPIVersion = objAPI.version;

	return(objAPI);
}

function InitializeAPI()
{
var playerObj = getFlashMovieObject();

	if ((g_initAPICount < 10) && !g_boolInitializeDone)
	{
		if (CheckAPI())
		{
		var err = InitializeSCO();
			clearInterval(g_varInterval);
		}
		else
			g_objAPI = getAPI(g_intAPIOrder);
	}
	else
	{
		playerObj.SendSCORMStatus("Exceeded Attempts");
		
		g_objAPI = null;

		clearInterval(g_varInterval);
	}

	g_initAPICount++;
}

function CheckAPI()
{
	return ((typeof(g_objAPI) != "undefined") && (g_objAPI != null))
}


function InitializeSCO()
{
var playerObj = getFlashMovieObject();
var err = true;

	if (!g_boolInitializeDone)
	{
		g_boolAPIInitialized = g_objAPI.LMSInitialize("");
		g_dtInitialized      = new Date();
	}

	if (g_boolAPIInitialized)
	{
		g_boolInitializeDone = true;
		
		playerObj.SendSCORMStatus("found");

		return(err + "");	// Force type to string
	}
	else
	{
		playerObj.SendSCORMStatus("Not Found");

		return("false");
	}
}

function SCORMSetValue(strName, strValue)
{
var strError = "";

	if (!CheckAPI())
		strError = "false"
	else
		strError = g_objAPI.LMSSetValue(strName, strValue.toString() + "");

	return(strError);
}

function SCORMGetValue(strName)
{
	return ((CheckAPI()) ? g_objAPI.LMSGetValue(strName.toString()) : "");
}


function SCORMCommand(command, args)
{
var strArgs    = new String(args);
var strCommand = new String(command);
var strReturn  = "";
var strArg1    = "";
var strArg2    = "";
var lPosition  = strArgs.indexOf(",");
var playerObj  = getFlashMovieObject();

	if (lPosition > -1)
	{
		strArg1 = strArgs.substr(0, lPosition);		// Name of data element to get from API
		strArg2 = strArgs.substr(lPosition + 1); 	// Name of Flash movie variable to set
	}
	else
		strArg1 = strArgs;

	if (!CheckAPI())
		strReturn = "";
	else if (strCommand.substring(0, 3) == "LMS")
	{
		if (strCommand == "LMSSetValue")
			strReturn = SCORMSetValue(strArg1, strArg2);
		else if ((strArg2) && (strArg2.length > 0))
		{
			if (strCommand == "LMSGetValue")
				strReturn = SCORMGetValue(strArg1);
		}
	}

	return(strReturn);
}

function MillisecondsToCMIDuration(lMilliseconds)
{
var strHMS       = "";
var strHours     = "";
var strMinutes   = "";
var strSeconds   = "";
var strThousands = "";
var dtDateTime   = new Date();

	dtDateTime.setTime(lMilliseconds);

	strHours     = "000" + Math.floor(lMilliseconds / 3600000);
	strMinutes   = "0" + dtDateTime.getMinutes();
	strSeconds   = "0" + dtDateTime.getSeconds();
	strThousands = "0" + Math.round(dtDateTime.getMilliseconds() / 10);

	// Convert duration from milliseconds to 0000:00:00.00 format

	strHMS  = strHours.substr(strHours.length - 4) + ":" + strMinutes.substr(strMinutes.length - 2) + ":";
	strHMS += strSeconds.substr(strSeconds.length - 2) + "." + strThousands.substr(strThousands.length - 2);

	return(strHMS);
}

function SCORMReportSessionTime()
{
var dtCurrent     = new Date();
var lMilliseconds = dtCurrent.getTime() - g_dtInitialized.getTime();

	return(SCORMSetValue("cmi.core.session_time", MillisecondsToCMIDuration(lMilliseconds)));
}


function SCORMFinish()
{
	if ((CheckAPI()) && (g_boolFinishDone == false))
	{
		SCORMReportSessionTime();
		g_boolFinishDone = (g_objAPI.LMSFinish("") == "true");
	}

	return (g_boolFinishDone + "");	// Force type to string
}

