// File: media-counter.js

MediaCounter = Class.create();

MediaCounter.prototype = {

    eltMediaCounter : null,
    floatMediaCount: null,
    floatImagesPerSecond: null,
    
    initialize: function(millionImagesPerDay) {
        this.eltMediaCounter = $$('#cellImageCounter H3')[0];

        if (this.eltMediaCounter) {
            this.floatMediaCount = this.eltMediaCounter.innerHTML.gsub(',','') * 1;
            this.floatImagesPerSecond = (millionImagesPerDay * 1000000) / 86400;
            new PeriodicalExecuter(this.updateMediaCount.bind(this), 1);
        }
    },
    updateMediaCount: function() {
        this.floatMediaCount += this.floatImagesPerSecond;
        Element.update(this.eltMediaCounter, this.commify(Math.round(this.floatMediaCount)));
    },
    commify: function(number) {
	    number += '';
	    x = number.split('.');
	    x1 = x[0];
	    x2 = x.length > 1 ? '.' + x[1] : '';
	    var rgx = /(\d+)(\d{3})/;
	    while (rgx.test(x1)) {
		    x1 = x1.replace(rgx, '$1' + ',' + '$2');
	    }
	    return x1 + x2;
    }
};


