var pointsClass = {
    tplURL : '/global/components/points/points.html',
    tplReference : '#points',
    eventPointChanged : $.Event('pointsChange'),
    changed : true, // mi serve per verificare se tra un'apertura e l'altra son cambiati i dati (altrimenti non renderizzo)
    userPoints : { // lista dei punti utente
    //        total : 7832,
    //        activities : {
    //            browse : 165,
    //            visit : 432,
    //            write_chat : 343,
    //            send_image : 30,
    //            click_pub : 200,
    //            find_target : 4000,
    //            visit_received : 1256,
    //            mosaic : -500
    //        }
    },
    pointsDescription :  { // tutte le voci dei punti col proprio valore
    //        browse : 1,
    //        visit : 1,
    //        write_chat : 1,
    //        send_image : 3,
    //        click_pub : 100,
    //        find_target : 500,
    //        visit_received : 1,
    //        mosaic : -500
    },

    init : function(_pointsObject)
    {
        this.userPoints = _pointsObject;
        
        $.getJSON('/static/global/pointsDescription.json', function(data) {
            pointsClass.pointsDescription = data;
            pointsClass._init();
        });
    },
    _init : function(){
        this._triggerPointChange();
        this._render();
        $(this.tplReference).hide();
    },
    sum : function(_activity)
    {
        var activityvalue = this.pointsDescription[_activity];
        this.userPoints.activities[_activity] += activityvalue;
        this.userPoints.total += activityvalue;
        //console.log('sum',activityvalue,_activity);
        this._triggerPointChange(activityvalue,_activity);

        this.changed = true;
        this._render();
    },
    toggle : function()
    {
        $(this.tplReference).toggle();
        this._render();
    },
    _render : function()
    {
        if($(this.tplReference).css('display') == 'none' || this.changed == false) return;
        $(this.tplReference+' #pointstable').empty();
        for(var activity in this.userPoints.activities)
        {
            var itemtext    = activity; // definire la traduzione
            var itemvalue   = this.pointsDescription[activity];
            var itemvaluesymbol = itemvalue > 0 ? '+' : '';
            var itemtotal   = this.userPoints.activities[activity];

            var tablerow = '<tr>';
            tablerow    +=      '<td class="right-spaced"> '+itemtext+'</td>';
            tablerow    +=      '<td align="right"><span class="left-spaced light">('+itemvaluesymbol+itemvalue+')</span></td>';
            tablerow    +=      '<td align="right"><span class="left-spaced color">'+itemtotal+' pt.</span></td>';
            tablerow    += '</tr>';
            $(this.tplReference+' #pointstable').append(tablerow);
        }
        
    },
    _triggerPointChange : function(_activityvalue,_activity)
    {
        var eventPointChanged = $.Event('pointsChange');
        eventPointChanged.activity = _activity ? _activity : '';
        eventPointChanged.value = _activityvalue ? _activityvalue : '';
        eventPointChanged.total = this.userPoints.total;
        //console.log('trig',_activityvalue,_activity);
        $(this.tplReference).trigger(eventPointChanged);
    }
};

