//Using shortcut instead of pipwerks namespace, SCORM 1.2

var SCORM_Manager = (function () {

    var _self = this;
    var sco = null;
    var timerID = null;
    var timerRunning = false;
    var timeValue = "";
    var timeCnt = 0;


    this.init = function (version, callback) {

        timerID = null;
        timerRunning = false;
        timeValue = "";
        timeCnt = 0

        sco = pipwerks.SCORM;
        sco.version = version || "1.2"

        var success = sco.init();
        if (success) {
            if(callback){
                callback();
            }
            start();
        }
        return success;
    }

    this.set = function (parameter, value) {
        try {
            var _status = sco.set(parameter, value);
            if (_status) {
                sco.save();
            }
            // console.log("scormManager- set: ", _status, parameter, value)
            return _status;
        } catch (err) {

        }
    }

    this.get = function (parameter) {
        return sco.get(parameter);
    }

    this.getUserDetail = function () {

        try {

            var _userId = _self.get("cmi.core.student_id");
            var _userName = _self.get("cmi.core.student_name");
            var _fname = '';
            var _lname = '';
            if (_userName) {
                try {
                    _userName = _userName.replace(/,/g, "");
                    var _strTemp = _userName.split(' ');
                    _fname = _strTemp[1];
                    _lname = _strTemp[0];
                } catch (err) {

                }
            }

            var _user = {
                'userId': _userId,
                'fullName': _userName,
                'firstName': _fname,
                'lastName': _lname
            }
            return _user
        } catch (err) {

        }
    }

    this.setUserData = function (uData) {
        try {
            var _uData = JSON.parse(uData);
            _self.set("cmi.suspend_data", uData.replace(/"/g, "'"));
            _self.setLocation(_uData['lesson_status']);
        } catch (err) {

        }
    }

    this.getUserData = function () {
        var _sData = _self.get("cmi.suspend_data");
        var _uData = _sData.replace(/'/g, '"')
        return _uData;
    }

    this.quit = function () {
        var _stopTime = stop();
        _self.set("cmi.core.session_time", String(_stopTime));
        sco.quit();
    }

    this.getLocation = function (value) {
        try {
            var _lesson_location = _self.get("cmi.core.lesson_location")
        } catch (err) {

        }
    };

    this.setLocation = function (value) {
        try {
            _self.set("cmi.core.lesson_location", value)
        } catch (err) {

        }
    };

    this.setScore = function (value) {
        try {
            _self.set("cmi.core.score.raw", value);
        } catch (err) {

        }
    };

    this.getStatus = function () {
        try {
            return sco.status("get")
        } catch (err) {

        }
    };

    this.setCourseStatus = function (value) {
        _self.setStatus(value)
    };

    this.setStatus = function (value) {
        try {
            sco.status("set", value)
        } catch (err) {

        }
        //Additional options, presented here in case you'd like to use them
        //case "completed"  : break;
        //case "incomplete" : break;
        //case "passed"     : break;    //SCORM 1.2 only
        //case "failed"     : break;    //SCORM 1.2 only
        //case "browsed"    : break;    //SCORM 1.2 only
    };



    //-----------------------------------------------
    //Time calculation Functions
    function stop() {
        clearTimeout(timerID);
        return timeValue;
    }


    //-------------------------------------------------------
    //Time calculation Functions
    var start = function () {
        showTime();
    }

    //----------------------------------------------------
    //Time calculation Functions
    var showTime = function () {

        try {
            timeCnt++;

            var newElapsedTime;
            var hours = Math.floor(timeCnt / 3600);
            newElapsedTime = timeCnt - (hours * 3600);

            var minutes = Math.floor(newElapsedTime / 60);
            newElapsedTime = newElapsedTime - (minutes * 60);

            var seconds = newElapsedTime;

            timeValue = "" + hours;
            if (hours < 10) {
                timeValue = "0" + hours;
            }

            timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
            timeValue += ((seconds < 10) ? ":0" : ":") + seconds;

            // Update display
            timerID = setTimeout(showTime(), 1000);

        } catch (err) {

        }
    }

    return this;
})