fcms_include('fcms');

function statHandler(container) {
    this.container = container;
    this.parameters = {};
    
    that = this; // umweg, um in callcack functionen auf classenmember zugreifen zu können

    this.updateCounterPerArticleTree = function(treeId, noFcmsPixel) {

        if (noFcmsPixel === true) {
            noFcmsPixel = '1';
        } else {
            noFcmsPixel = '0';
        }

        this.parameters = {
            'type'        : 'getCounter',
            'module'      : 'article',
            'noFcmsPixel' : noFcmsPixel,
            'treeId'      : treeId
        };
        this.update();
    }

    this.updateCounterPerPicid = function(picid, noFcmsPixel) {

        if (noFcmsPixel === true) {
            noFcmsPixel = '1';
        } else {
            noFcmsPixel = '0';
        }

        this.parameters = {
            'type'        : 'getCounter',
            'module'      : 'picture',
            'noFcmsPixel' : noFcmsPixel,
            'objectId'    : picid
        };
        this.update();
    }

    this.updateCounterPerStreamsetId = function(srid, noFcmsPixel) {

        if (noFcmsPixel === true) {
            noFcmsPixel = '1';
        } else {
            noFcmsPixel = '0';
        }

        this.parameters = {
            'type'        : 'getCounter',
            'module'      : 'streamset',
            'noFcmsPixel' : noFcmsPixel,
            'objectId'    : srid
        };
        this.update();
    }

    this.update = function() {

        // besser kapseln -> grundsätzliche js Logik nötig
        new Ajax.Request('/_/tools/ajax_handler.html', {
            method: 'post',
            encoding: 'UTF-8',
            parameters: this.parameters,
            onSuccess: function(transport) {
                var counter = fcms.decode_utf8(transport.responseJSON); // content-type application/json needed
                if (counter) {
                    fcms.replaceContent(that.container, counter);
                }
            }
        });        
    };
}
