;(function($) {
$.widget("ui.counter", {
    current: 0,
    _end: 0,
    _interval: null,
    _init: function()
    {
        this.reset();
    },

    reset: function()
    {
        if (this.element.text() != '')
        {
            this.current = parseInt(this.element.text().replace(/,/, ''));
        }
    },

    resetTo: function(value)
    {
        value = parseInt(value, 10);
        this.current = value;
    },

    countTo: function(value)
    {
        value = parseInt(value, 10);

        /**
         * Nothing to do
         */
        if (this.current == value)
        {
            return this;
        }

        /**
         * reference this
         */
        var self = this;

        /**
         * Set end game
         */
        self._end = value;

        /**
         * Get the step to take;
         * increment in 100's, 10', or 1's depending upon how far away we are
         */
        var getStep = function()
        {
            var diff = Math.abs(self.current - self._end);

            /**
             * If we are less than 20 away, increment in 1's
             */
            if (diff <= 19)
            {
                return 1;
            }

            /**
             * If we are less than 100 away, increment in 10's
             */
            if (diff <= 100)
            {
                return 10;
            }

            /**
             * If we are less than 500 away, increment in 50's
             */
            else if (diff <= 500)
            {
                return 50;
            }

            /**
             * Else, increment in 100's
             */
            else
            {
                return 100;
            }
        };

        var formatWithComma = function(number)
        {
        	number = number.toString();
        	var x = number.split('.');
        	var x1 = x[0];
        	var x2 = x.length > 1 ? '.' + x[1] : '';
        	var rgx = /(\d+)(\d{3})/;
        	while (rgx.test(x1))
        	{
        		x1 = x1.replace(rgx, '$1' + ',' + '$2');
        	}
        	return x1 + x2;
        }

        /**
         * Tick function
         */
        var tick = function()
        {
            /**
             * Get step
             */
            var step = getStep();

            /**
             * Moving up or down
             */
            if (self.current > self._end)
            {
                step = step * -1;
            }

            /**
             * Update current
             */
            self.current = self.current + step;

            /**
             * We're one away, force it to prevent an animation loop error
             */
            if (Math.abs(self.current - self._end) === 1)
            {
                self.current = self._end;
            }

            /**
             * Update display
             */
            self.element.text(formatWithComma(self.current));

            /**
             * We're done!
             */
            if (self.current === self._end)
            {
        		window.clearInterval(self._interval);
            }
        };

        self._interval = window.setInterval(tick, self.options.speed);
    }

});

$.ui.counter.defaults = {
    speed: 25
};

})(jQuery);
