// -----------------------------------------------------------------------------
// This file is part of [nice] frame
// (c) 2007 Dynamic Media eLearning GmbH
// All rights reserved.
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// $Id: scormapi.js 2699 2008-05-05 14:12:32Z dynamicmedia\Arnold $
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Globals
// -----------------------------------------------------------------------------

// The SCORM 2004 API object
var API_1484_11 = null;
// The SCORM 1.2 API object
var API = null;

// -----------------------------------------------------------------------------
// SCORM 2004 API
// -----------------------------------------------------------------------------

// The SCORM API object. Wraps the SCORM 2004 API object provided by main.swf
// into a Javascript object.
function SCORMWrapper(API) {
	
	// -------------------------------------------------------------------------
	// Private
	// -------------------------------------------------------------------------
	
	// A reference the API provided by main.swf
	var _API = API;
	
	function _applyHack(str) {
		
		// For some reason, it is not possible to exchange an empty string ("")
		// between Javascript and Flash. It will always be converted to the
		// literal string "null". Since the SCORM API expects an empty string
		// as parameter in a lot of situations, the unicode character
		// <ZERO WIDTH SPACE> is used to indicate an empty string.
		return (str === "" ? String.fromCharCode(0x200B) : str);
		
	}
	
	function _result(res) {
		
		// Translate a <ZERO WIDTH SPACE> back to a regular empty string
		return (res === String.fromCharCode(0x200B) ? "" : res);
		
	}
	
	// -------------------------------------------------------------------------
	// SCORM 2004 API
	// -------------------------------------------------------------------------
	
	this.Initialize = function(param) {
		
		return _result(_API.Initialize(_applyHack(param)));
		
	}
	
	this.Terminate = function(param) {
		
		return _result(_API.Terminate(_applyHack(param)));
		
	}
	
	this.GetValue = function(id) {
		
		return _result(_API.GetValue(id));
		
	}
	
	this.SetValue = function(id, value) {
		
		return _result(_API.SetValue(id, _applyHack(value)));
		
	}
	
	this.Commit = function(param) {
		
		return _result(_API.Commit(_applyHack(param)));
		
	}
	
	this.GetLastError = function() { return _API.GetLastError(); }
	
	this.GetErrorString = function(errorcode) {
		
		return _result(_API.GetErrorString(errorcode));
	
	}
	
	this.GetDiagnostic = function(param) {
		
		return _result(_API.GetDiagnostic(_applyHack(param)));
		
	}
	
	// -------------------------------------------------------------------------
	// SCORM 1.2 API
	// -------------------------------------------------------------------------
	
	this.LMSInitialize = function(param) {
		
		return _result(_API.Initialize(_applyHack(param), "1.2"));
		
	}
	
	this.LMSFinish = function(param) {
		
		return _result(_API.Terminate(_applyHack(param)));
		
	}
	
	this.LMSGetValue = function(id) {
		
		return _result(_API.GetValue(id));
		
	}
	
	this.LMSSetValue = function(id, value) {
		
		return _result(_API.SetValue(id, _applyHack(value)));
		
	}
	
	this.LMSCommit = function(param) {
		
		return _result(_API.Commit(_applyHack(param)));
		
	}
	
	this.LMSGetLastError = function() { return _API.GetLastError(); }
	
	this.LMSGetErrorString = function(errorcode) {
		
		return _result(_API.GetErrorString(errorcode));
	
	}
	
	this.LMSGetDiagnostic = function(param) {
		
		return _result(_API.GetDiagnostic(_applyHack(param)));
		
	}
	
	// -------------------------------------------------------------------------
	// Miscellaneous
	// -------------------------------------------------------------------------
	
	this.API = function() { return _API; }
	
	this.SetDMValue = function(id, value, scope, persistent) {
		
		return _API.SetDMValue(id, value, scope, persistent);
		
	}
	
	this.GetDMValue = function(id, scope) {
		
		return _API.GetDMValue(id, scope);
		
	}

}
