var ShellWrapper = function() {

    Utils.clear();

    var _self = this;
    var _shell_wrapper = null;
    var _mTimer = null;
    var _module_score_metter_slider = null;
    var _score_metter_bar_width = 0;
     

    var _moduleData = {
        'time': '00:00:01',
        'sr': 0,
        'mnsr': 0,
        'mxsr': 1,
        'bT': '00:10:00',
        'tT': '00:00:00'
    }

    this.init = function() {

        _mTimer = $("#module_timer");

        /*  if (navigator.userAgent.match(/iPhone|iPad|iPod|Mac/i) || navigator.userAgent.match(/Android/i)) {
             $("#btn_exit").hide();
         } */
         
        addEvents();
    }




    var addEvents = function() {

        _shell_wrapper = $("#shell_wrapper");


        _shell_wrapper.off("updateModuleTime").on("updateModuleTime", function(e, timeData) {
            e.stopPropagation();
            try {
                var _time = (timeData['time'])
                    // var _periods = (timeData['periods'])
                    // var _time = Utils.addZero(_periods[4]) + ":" + Utils.addZero(_periods[5]) + ":" + Utils.addZero(_periods[6]) || "00:00:00";

                updateTime(_time)
            } catch (err) {

            }
        })

        _shell_wrapper.off("updateModuleScore").on("updateModuleScore", function(e, score) {
            e.stopPropagation();
            $("#score-points").html(score);
            updateScore(score);
        })

        _shell_wrapper.off("changeModule").on("changeModule", function(e, data) {
            e.stopPropagation();
            changeModuleData(data)
        })

      

        // addSlider();

    }


    var addSlider = function() {

        _module_score_metter_slider = $('#shell_header .slider')
        _module_score_metter_slider.slider({
            'range': true,
            'value': 0,
            'step': 1,
            'disabled': false
        });

        _module_score_metter_slider.off();
 

    }



    var updateTime = function(tTime) {
        try {
            //   console.log("ShellWrapper:  updateTime: ", time)
            _moduleData['tT'] = tTime;
            _mTimer.html(tTime);
        } catch (err) {
            Utils.error("ShellWrapper:  updateTime:   ", err)
        }
        _shell_wrapper.trigger('updateProgressTime', tTime)
    }

    var updateScore = function(score) {

        try {
            _score_metter_bar_width = $(".score_metter_bar").width()

            var _score = parseFloat(score);
            _moduleData['sr'] = _score;

            var _minScore = parseFloat(_moduleData['mnsr']);
            var _maxScore = parseFloat(_moduleData['mxsr']);

            var _left = _score_metter_bar_width / 2;

            if (_score < 0) {
                _left = _left - ((100 / _minScore) * _score);
            } else if (_score > 0) {
                _left = _left + ((100 / _maxScore) * _score);
            }
            $(".scorenob").css('left', _left + 'px');

            // _module_score_metter_slider.val(_score);
            _shell_wrapper.trigger('updateProgressScore', _score)
        } catch (err) {
            Utils.error("ShellWrapper:  updateScore:   ", err)
        }

        /*
        try{
            _module_score_metter_slider.slider('refresh');
        }catch(err){

        }
*/

    }

    var updateMinMaxScoreMetter = function(sData) {
        try {
            var _minScore = parseInt(sData['mnsr']) || "-100"
            var _maxScore = parseInt(sData['mxsr']) || "100"
                /*   _module_score_metter_slider.attr({
                      'min': _minScore,
                      'max': _maxScore
                  }) */
            _shell_wrapper.trigger('updateProgressData', sData)
        } catch (err) {
            Utils.error("ShellWrapper:  updateMinMaxScoreMetter:   ", err)
        }
    }

    var changeModuleData = function(mData) {
        try {
            _moduleData = mData;
            updateMinMaxScoreMetter(mData)
            updateTime(mData['tT'])
            updateScore(mData['sr'])
        } catch (err) {
            Utils.error("ShellWrapper:  changeModuleData:   ", err)
        }
    }

    var update = function(mData) {
        try {
            updateTime(mData['tT'])
            updateScore(mData['sr'])
        } catch (err) {
            Utils.error("ShellWrapper:  update:   ", err)
        }
    }

    return this;

}