var nFindAPITries = 0;
var API = null;
var terminated=false;
var maxTries = 500;
var APIVersion = "";
var iniciado=false;
var today=new Date();
var scorm_sessionstart=today.getTime();
var scorm_estado=new Array(5);

// The ScanForAPI() function searches for an object named API_1484_11
// in the window that is passed into the function. If the object is
// found a reference to the object is returned to the calling function.
// If the instance is found the SCO now has a handle to the LMS
// provided API Instance. The function searches a maximum number
// of parents of the current window. If no object is found the
// function returns a null reference. This function also reassigns a
// value to the win parameter passed in, based on the number of
// parents. At the end of the function call, the win variable will be
// set to the upper most parent in the chain of parents.
function ScanForAPI(win)
{
	while ((win.API_1484_11== null) && (win.parent != null)
	&& (win.parent != win))
	{
		nFindAPITries++;
		if (nFindAPITries > maxTries)
		{
			return null;
		}
		win = win.parent;
	}
	return win.API_1484_11;
}
// The GetAPI() function begins the process of searching for the LMS
// provided API Instance. The function takes in a parameter that
// represents the current window. The function is built to search in a
// specific order and stop when the LMS provided API Instance is found.
// The function begins by searching the current window’s parent, if the
// current window has a parent. If the API Instance is not found, the
// function then checks to see if there are any opener windows. If
// the window has an opener, the function begins to look for the
// API Instance in the opener window.
function GetAPI(win)
{
		if ((win.parent != null) && (win.parent != win))
		{
			API = ScanForAPI(win.parent);
		}
		if ((API == null) && (win.opener != null))
		{
			API = ScanForAPI(win.opener);
		}
		if (API != null)
		{
			APIVersion = API.version;
		}
}





function scorm_inicio()
{
	if(API!=null)
	{
		API.Initialize("");
		if(API.GetValue("cmi.completion_status")!="completed")
		{
			API.SetValue("cmi.completion_status","incomplete");
			API.SetValue("cmi.exit","suspend");
		}
		var data=API.GetValue("cmi.suspend_data");
		interfaz.scorm_send(data);
	}
}

function scorm_parada()
{
	if(API!=null && !terminated)
	{
		terminated=true;
		API.Terminate("");
		API=null;
		APIVersion="";
	}
}

function scorm_fin()
{
	if(API!=null)
	{
		API.SetValue("cmi.exit","normal");
		API.SetValue("cmi.completion_status","completed");
		API.Commit(""); // No llamo a Terminate porque algunos LMS cierran el SCO al hacerlo, impidiendo al alumno ver la pantalla de resultados. Terminate será invocado cuando el usuario cierre el SCO.
	}
}

function scorm_save(data)
{
	if(API!=null)
	{
		API.SetValue("cmi.suspend_data",data);
	}
}

function scorm_load()
{
	if(API!=null)
	{
		var data=API.GetValue("cmi.suspend_data");
//		var data="3;undefined;2;1;0;3;undefined;1";
		interfaz.scorm_send(data);
	}
}

function thisMovie(movieName)
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

GetAPI(window);
