/*******************************************************************************
**
** Advanced Distributed Learning Co-Laboratory (ADL Co-Lab) grants you 
** ("Licensee") a non-exclusive, royalty free, license to use and redistribute 
** this software in source and binary code form, provided that i) this copyright 
** notice and license appear on all copies of the software; and ii) Licensee does 
** not utilize the software in a manner which is disparaging to ADL Co-Lab.
**
** This software is provided "AS IS," without a warranty of any kind.  ALL 
** EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 
** IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 
** NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  ADL Co-Lab AND ITS LICENSORS SHALL NOT 
** BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 
** OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL ADL Co-Lab OR ITS
** LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
** INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 
** CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
** INABILITY TO USE SOFTWARE, EVEN IF ADL Co-Lab HAS BEEN ADVISED OF THE POSSIBILITY
** OF SUCH DAMAGES.
**
*******************************************************************************/

var API = null;

// Find the API in parent and opener windows
function findAPI(win) {
    var findAPITries = 0;
    while ((win.API == null) && (win.parent != null) && (win.parent != win)) {
        findAPITries++;
        if (findAPITries > 500) {
            alert("Error finding API -- too deeply nested.");
            return null;
        }
        win = win.parent;
    }
    return win.API;
}

// Get the API handle
function getAPI() {
    if (API == null) {
        API = findAPI(window);
        if ((API == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
            API = findAPI(window.opener);
        }
        if (API == null) {
            alert("Unable to find an API adapter");
        }
    }
    return API;
}

/*******************************************************************************
**
** SCORM Required Functions
**
*******************************************************************************/

function doLMSInitialize() {
    var api = getAPI();
    if (api == null) {
        return "false";
    }
    return api.LMSInitialize("");
}

function doLMSFinish() {
    var api = getAPI();
    if (api == null) {
        return "false";
    }
    return api.LMSFinish("");
}

function doLMSGetValue(parameter) {
    var api = getAPI();
    if (api == null) {
        return "";
    }
    return api.LMSGetValue(parameter);
}

function doLMSSetValue(parameter, value) {
    var api = getAPI();
    if (api == null) {
        return "false";
    }
    return api.LMSSetValue(parameter, value);
}

function doLMSCommit() {
    var api = getAPI();
    if (api == null) {
        console.error('API not found');
        return "false";
    }

    if (typeof api.LMSCommit !== 'function') {
        console.error('LMSCommit function not found in API');
        return "false";
    }

    try {
        // SCORM 1.2 specification requires empty string parameter
        return api.LMSCommit("");
    } catch (error) {
        console.error('Error in LMSCommit:', error);
        return "false";
    }
}

function doLMSGetLastError() {
    var api = getAPI();
    if (api == null) {
        return "";
    }
    return api.LMSGetLastError();
}

function doLMSGetErrorString(errorCode) {
    var api = getAPI();
    if (api == null) {
        return "";
    }
    return api.LMSGetErrorString(errorCode);
}

function doLMSGetDiagnostic(errorCode) {
    var api = getAPI();
    if (api == null) {
        return "";
    }
    return api.LMSGetDiagnostic(errorCode);
}
