//
//
// SCORM 1.2 wrapper
//
// scorm api communication functions
//
// code (loosely) based on the ADL SCORM Test Suite 1.2.7 file 
// lmsrtefunctions.js
//
//
//


// locals
var apiAdapter = null;		// a reference to the API adapter
var scormDebugMode = false; // true for additional messages



//
// api adapter locator functions
//
//

function getAPIAdapter() {
// searches for and returns the SCORM api adapter.  if not found within the current window
// hierarchy, searches within the window.opener hierarchy

	var adapter = findAPI(window);

	if ((adapter == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
		adapter = findAPI(window.opener);
	}

	return adapter;
}

function findAPI(win) {
// recursively searches up the hierarchy for the api adapter.  limit of 500 tries 
// comes from the IEEE API standard
//
// returns the api adapter found, or null

	var findAPITries = 0;

  	while ((win.API == null) && (win.parent != null) && (win.parent != win)) {
		findAPITries++;
		if (findAPITries > 500) {
			if (scormDebugMode) alert("Error finding API adapter -- too deeply nested.");
			return null;
		}
		win = win.parent;
	}

	return win.API;
}




//
// api adapter wrapper functions
//
// each function attempts to perform the action, and either returns
// the result from the adapter, or false if there is no adapter
//
//


// initialize, terminate
// 
function doLMSInitialize() {
// the LMSInitialize function *must* be called before any others.  therefore,
// this function is the only one in this class that attempts to update the
// apiAdapter member variable - all others assume the adapter is valid

	if (apiAdapter == null) apiAdapter = getAPIAdapter();
	
	if (apiAdapter == null) {
		if (scormDebugMode) alert("Unable to locate API adapter.  LMSInitialize() not invoked.");
		return false;
	} else {
		return apiAdapter.LMSInitialize("").toString();
	}
}

function doLMSFinish() {

	if (apiAdapter == null) {
		if (scormDebugMode) alert("API adapter not available.  LMSFinish() not invoked.");
		return false;
	} else {
		return apiAdapter.LMSFinish("").toString();
	}
}


// get value, set value
//
function doLMSGetValue(name) {

	if (apiAdapter == null) {
		if (scormDebugMode) alert("API adapter not available.\n  LMSGetValue(" + name + ") not invoked.");
		return "";
	} else {
		return apiAdapter.LMSGetValue(name).toString();
	}
}

function doLMSSetValue(name,value) {

	if (apiAdapter == null) {
		if (scormDebugMode) alert("API adapter not available.\n  LMSSetValue(" + name + "," + value + ") not invoked.");
		return "";
	} else {
		return apiAdapter.LMSSetValue(name,value).toString();
	}
}

function doLMSCommit() {

	if (apiAdapter == null) {
		if (scormDebugMode) alert("API adapter not available.  LMSCommit() not invoked.");
		return false;
	} else {
		return apiAdapter.LMSCommit("").toString();
	}

}


// error handling
//
function doLMSGetLastErrorCode() {

	if (apiAdapter == null) {
		if (scormDebugMode) alert("API adapter not available.  LMSGetLastError() not invoked.");
		return false;
	} else {
		return apiAdapter.LMSGetLastError().toString();
	}
}

function doLMSGetLastErrorString() {

	if (apiAdapter == null) {
		if (scormDebugMode) alert("API adapter not available.  LMSGetLastError() not invoked.");
		return false;
	} else {
		var errorCode = apiAdapter.LMSGetLastError();
		return apiAdapter.LMSGetErrorString(errorCode).toString();
	}
}













