(function ($) {
    $(document).ready(function () {
        var rqHandler = new $.AclRequestHandler(__rCore.ajax);

        $('.count-to-float').countTo({ decimals : 3 });
        $('.count-to').countTo();

        if(typeof aclChartConfig !== 'undefined') {
            var tsm = new Chart(document.getElementById("line_chart").getContext("2d"), aclChartConfig);
        }

        if(typeof aclDonutData !== 'undefined') {
            Morris.Donut({
                element: 'donut_chart',
                data: aclDonutData,
                colors: ['rgb(244, 64, 54)', 'rgb(75, 175, 80)', 'rgb(158, 157, 158)'],
                formatter: function (y) {
                    return y + '%'
                },
                resize: true
            });
        }

        if(typeof tsm !== 'undefined' && typeof aclChartConfig !== 'undefined' && tsm && aclChartConfig) {
            $('.acl-change-chart').on('click.aclchart', function (e) {
               e.preventDefault();
                rqHandler.Arc({
                    context: __rCore.context.dcl,
                    intent: true,
                    data: {
                        type: $(this).attr('acl-type'),
                    },
                    luacl: $(this),
                    aclChart: tsm,
                    targeclass: '.acl-change-chart',
                    active: 'active',
                    self: $(this)
                });
            });
        }
    });

    $.AclRequestHandler.prototype['response'+__rCore.context.dcl] = function(request) {
        if(this.response.label && this.response.data && this.response.data[0] && this.response.data[1] && this.response.data[2]) {
            request.aclChart.data.labels = this.response.label;
            request.aclChart.data.datasets[0].data = this.response.data[0];
            request.aclChart.data.datasets[1].data = this.response.data[1];
            request.aclChart.data.datasets[2].data = this.response.data[2];
            request.aclChart.update();
            $(request.targeclass).removeClass(request.active);
            request.self.addClass(request.active);
        }
    };

    $.AclRequestHandler.prototype['error'+__rCore.context.dcl] = function(request, response) {};

    $.AclRequestHandler.prototype['loading'+__rCore.context.dcl] = function(elem) {
        if(elem.attr('acl-is-loading')){
            elem.button('reset');
            elem.removeAttr('acl-is-loading')
        } else {
            elem.button('loading');
            elem.attr('acl-is-loading', true)
        }
    };

}(jQuery));