/**
 * @author Tim
 */
/*
 * SCORM API communication stuff.
 */
FLN.ScormAdapter = function(){

    /*
     * Find SCORM API routines from:
     * http://www.ostyn.com/standards/scorm/samples/api_discovery_ff_issue.htm
     */
    var gAPI = null; // Stand-in for the API object, if found.
    function _ScanForAPI(win) // Revised to handle x-scripting errors
    {
        // This function is called by GetAPI
        var nFindAPITries = 500; // paranoid to prevent runaway
        var objAPI = null;
        var bOK = true;
        var wndParent = null;
        while ((!objAPI) && (bOK) && (nFindAPITries > 0)) {
            nFindAPITries--;
            try {
                objAPI = win.API;
            } 
            catch (e) {
                bOK = false;
            }
            if ((!objAPI) && (bOK)) {
                try {
                    wndParent = win.parent;
                } 
                catch (e) {
                    bOK = false;
                }
                if ((!bOK) || (!wndParent) || (wndParent == win)) {
                    break;
                }
                win = wndParent;
            }
        }
        return objAPI;
    }
    
    function GetAPI(win) // Revised to handle x-scripting errors
    {
        // Sets gAPI to be a reference to the API object provided by the RTE,
        // or leave it as null if no API object could be found.
        // Parameter win is the SCO's window
        var wndParent = null;
        var wndOpener = null;
        try {
            wndParent = win.parent;
        } 
        catch (e) {
        }
        try {
            wndOpener = win.opener;
        } 
        catch (e) {
        }
        if ((wndParent != null) && (wndParent != win)) {
            gAPI = _ScanForAPI(wndParent);
        }
        if ((gAPI == null) && (wndOpener != null)) {
            gAPI = _ScanForAPI(wndOpener);
        }
    }
    
    /*
     * private properties
     */
    var _scormAPIinitialized = false;
    GetAPI(window);
    var _scormAPI = gAPI;
	var _scormAPIDiscovered = _scormAPI ? true : false;
    var _scormAPIExtended = false;
    var _suppressCheckLMSError = true;
    
    /*
     * public functions
     */
    return {
		/**
		 * returns if a SCORM API was ever discovered in the lifetime of this sco. Getter function for _scormAPIDiscovered.
		 * @return {Boolean} _scormAPIDiscovered
		 */
		apiEverDiscovered: function(){
			return _scormAPIDiscovered;
		},
        isInitialised: function(){
            return _scormAPIinitialized;
        },
        initialise: function(){
            if (!_scormAPIinitialized && _scormAPI) {
                _scormAPI.LMSInitialize("");
                /*try {
                    _scormAPI.extendAPI();
                    _scormAPIExtended = true;
                } 
                catch (e) {
                    //Yahoo.log("extendAPI() failed");
                }*/
                _scormAPIinitialized = true;
            }
            else {
                //Yahoo.log("SCORM API not Initialized. ");
            }
        },
        finish: function(){
            if (_scormAPIinitialized && _scormAPI) {
                _scormAPI.LMSFinish("");
                _scormAPIinitialized = false;
            }
        },
        /**
         * sets a value on the lms and does error checking
         * @param {String} name scorm data model element name
         * @param {String} value value for the element
         */
        setLMSValue: function(name, value){
            if (_scormAPIinitialized) {
                _scormAPI.LMSSetValue(name, value);
                if (!_suppressCheckLMSError) {
                    this.checkLMSError("LMSSetValue(\"" + name + ", " + value + ")");
                }
            }
            else {
                //Yahoo.log("SCORM API not Initialized. LMSSetValue(\"" + name + ", " + value + ") failed.");
            }
        },
        commitLMS: function(){
            if (_scormAPIinitialized) {
                _scormAPI.LMSCommit("");
                if (!_suppressCheckLMSError) {
                    this.checkLMSError("LMSCommit(\"\")");
                }
            }
            else {
                //Yahoo.log("SCORM API not Initialized. LMSCommit(\"\") failed.");
            }
        },
        /**
         * gets a value from the LMS and does error checking
         *  @param {String} name the LMS value to get
         *  @return {String} result the string value returned from the LMS or null if the API is not initialised.
         */
        getLMSValue: function(name){
            var result = null;
            if (_scormAPIinitialized) {
                result = _scormAPI.LMSGetValue(name);
                if (!_suppressCheckLMSError) {
                    this.checkLMSError("LMSGetValue(\"" + name + "\")");
                }
            }
            else {
                //Yahoo.log("LMSGetValue(\"" + name + "\")");
            }
            return result;
        },
        
        /**
         * checks the last LMS error and outputs them along with a diagnostic using //Yahoo.log();
         * @param {String} m a message to output
         */
        checkLMSError: function(m){
            if (!_suppressCheckLMSError) {
                var error = _scormAPI.LMSGetLastError();
                //Yahoo.log(m + " - Error code: " + _scormAPI.LMSGetErrorString(error));
                //Yahoo.log("Diagnostic: " + _scormAPI.LMSGetDiagnostic(error));
                return error;
            }
        }
        
        
    }
}();

