(function ($) {
    var settings = {
		duration: 100
	};

	$.fn.counter = function (options) {
		options = $.extend({}, settings, options),
		counterEl = $(this);
				
        init();
		
		function init() {
			$.fn.counter.app.count(counterEl,options.duration);
		}
	};
})(jQuery);

$.fn.counter.app = {
	count: function(counterEl,duration) {
		setTimeout(function() {
			counterEl.text($.fn.counter.app.incrementVal(counterEl.text()));
			$.fn.counter.app.count(counterEl,duration);
		}, duration);
	},
	
	incrementVal: function(val) {
		val = parseInt(val.replace(',','')) + 1;
		return $.fn.counter.app.commify(val);
	},
	
	commify: function(str) {
		str += '';
		var x = str.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;
	}	
}
