//=================================================================================
// Developed	: Fuel Technical 
// Date			: 16th Jan 2007
// Comment		: 
//=================================================================================
var fuel_API = null;
var init = "false";
var sessionTime = new Date();
var LMSReadyState;
var startTime = new Date().getTime();
var strFunctionId
//=================================================================================
// This function searches api and assigns it to fuel_API for LMS function
//=================================================================================
function fuel_getAPI()
{
	var myAPI = null;
	var tries = 0, triesMax = 500;
	while (tries < triesMax && myAPI == null)
	{
		window.status = 'Looking for API object ' + tries + '/' + triesMax;
		myAPI = findAPI(window);
		if (myAPI == null && typeof(window.parent) != 'undefined') myAPI = findAPI(window.parent);
		if (myAPI == null && typeof(window.top) != 'undefined') myAPI = findAPI(window.top);
		if (myAPI == null && typeof(window.opener) != 'undefined') if (window.opener != null && !window.opener.closed) myAPI = findAPI(window.opener);
		tries++;
	}
	
	if (myAPI == null)
	{
		alert('JavaScript Warning: API object not found in window or opener. (' + tries + ')');
	}
	else
	{
		fuel_API = myAPI;
		showAlert(fuel_API)
		window.status = '***API found***';
		fuel_adlOnload();
		detectFlash();
	}
}
//=================================================================================
// Search the window hierarchy for an object named "API"; Look in the current window (win) and recursively look in any child frames
//=================================================================================
function findAPI(win) 
{
	if (win.API != null) 
	{
		return win.API;
	}
	
	if (win.length>0) 
	{
		showAlert("looking for api in "+win.length+" windows frames")

		for (var i = 0; i<win.length; i++) 
		{
			showAlert("Recursively looking for api in frames["+i+"] of "+win.length)
			var theAPI = findAPI(win.frames[i]);
			if (theAPI != null) 
			{
				return theAPI;
			}
		}
	}
	return null;
}

//=================================================================================
//  This function takes into consideration all the time since course launched till the course exit.
//=================================================================================
function stopWatch() 
{
	var endTime = new Date().getTime();
	var elapsedTime = Math.round((endTime-startTime)/1000);
	var hours = elapsedTime/3600;
	var minutes;
	var seconds;

	if (hours<1) 
	{
		hours = "00";
	} 
	else 
	{
		hours = Math.floor(hours);
		elapsedTime = elapsedTime%3600;
	}
	
	minutes = elapsedTime/60;
	if (minutes<1) 
	{
		minutes = "00";
	} 
	else 
	{
		minutes = Math.floor(minutes);
		if (minutes<10) 
		{
			minutes = "0"+minutes;
		}
	}

	seconds = elapsedTime%60;
	if (seconds<10) 
	{
		seconds = "0"+seconds;
	}
	
	var sessionTime = hours+":"+minutes+":"+seconds;
	return sessionTime;
}

//=================================================================================
//  This function is triggered 
//=================================================================================
function fuel_adlOnload() 
{
	showAlert("fuel_adlOnload() called..");
	strFunctionId = "Load";
	if(fuel_API != null)
	{
		init = fuel_API.LMSInitialize("");
		CheckLmsStatus("Initialise");
		if(init != "true")
		{
			alert("LMS failed to initialize, please contact System administrator");
		}
		else if(fuel_API.LMSGetValue("cmi.core.lesson_status") != "completed")
		{
			CheckLmsStatus("cmi.core.lesson_status");
			fuel_API.LMSSetValue("cmi.core.lesson_status", "incomplete");
			CheckLmsStatus("cmi.core.lesson_status");
		}
	}
}

//=================================================================================
//  This function is triggered when the course is exited. All values saved and connection to lms is removed.
//=================================================================================
function fuel_adlOnunload() 
{
	strFunctionId = "Unload";
	showAlert("fuel_adlOnunload() called..");
	if (fuel_API != null) 
	{
		var currentSess = stopWatch();
		fuel_API.LMSSetValue("cmi.core.session_time", stopWatch());
		CheckLmsStatus("cmi.core.session_time");
		fuel_API.LMSCommit("");
		CheckLmsStatus("Commit");
		fuel_API.LMSFinish("");
		CheckLmsStatus("Finish");
		top.close();
	}
}
//=================================================================================
//  This function is triggered when the course is exited. All values saved and connection to lms is removed.
//=================================================================================
function fuel_setValue(cmi, value)
{
	strFunctionId = "Set Value";
	if(init != "true")
	{
		alert("LMS failed to initialize, please contact System administrator");
	}
	else if(fuel_API != null)
	{
		result = fuel_API.LMSSetValue(cmi, value);
		CheckLmsStatus(cmi);
		showAlert(cmi + " ::: [" + value +"] ::: status = " +result)
	}
}
//=================================================================================
//  This function gets value from LMS
//=================================================================================
function fuel_getValue(cmi)
{
	strFunctionId = "Get Value";
	if(init != "true")
	{
		alert("LMS failed to initialize, please contact System administrator");
	}
	else if(fuel_API != null)
	{
		result = fuel_API.LMSGetValue(cmi);
		CheckLmsStatus(cmi);
		showAlert(cmi + " ::: [" +result+"]")
		return result;
	}
}
//=================================================================================
//  This function shows alert when debug mode is true
//=================================================================================
function showAlert(p_sInfoText)
{
	if (_bDebug) 
	{
		alert(p_sInfoText)
	}
}
//=================================================================================
//  This function Checks for LMS Status for error
//=================================================================================
function CheckLmsStatus(Args)
{
	var StrErrorId;
	alert("how i came here..");
	StrErrorId = fuel_API.LMSGetLastError();
	if(StrErrorId != "0")
	{
		alert("Called from : "+strFunctionId + "\nCommand : "+Args+"\nError : "+fuel_API.LMSGetErrorString(StrErrorId));
	}	
}
//=================================================================================
// Valid cmi calls are
// cmi.core.lesson_status					-  • “passed” • “completed” • “failed” • “incomplete” • “browsed” • “not attempted”
// cmi.student_preference.language		- will return language of the user
// cmi.core.session_time					- for session time
// cmi.suspend_data							- for data
//
//
//=================================================================================
