/*
	jquery.counter.js
	
	@author  		MGA
	@date			20101015
	@version 		1.0
	@dependencies 	jquery 1.4.2
*/
;(function($) {
	$.fn.counter=function(options){
		// Set default values
		var defaults={
			start:0,
			end:100,
			increment:1,
			speed:1000,
			callback:function(elm){},
			format:function(str){return str;}
		}
		//extending default with options
		var options=$.extend(defaults,options);            
		// Set initial value
		var counterCurrent=Math.round(options.start);
		$(this).html(options.format(counterCurrent));
		// The actual function that does the counting
		var _increment=function(elm){
			if (counterCurrent>=options.end){
				clearInterval(parseInt($(elm).attr("data-counting"),10));
				elm.html(options.format(options.end));
				options.callback.call(elm);
			} else {
				counterCurrent+=options.increment;
				elm.html(options.format(counterCurrent));
			}
		}
		// Call the counter function in a closure to avoid conflicts
		var $this=$(this);
		(function(elm){
			$(elm).attr("data-counting",setInterval(function(){
				_increment(elm);
			}, options.speed))
		})($(this));
	}
})(jQuery);
