
jQuery.fn.animateCount = function(settings)
{
	settings = jQuery.extend(
		{
			counter:false,
			speed:600,
			time:200,
			prefix:"руб.",
			format:3,
			oldvalue:0,
			newvalue:0,
			hideNull:true
		}, settings
	);

	return this.each(
		function()
		{
			
			function ownformat(str) {
				if (str==0) return "";
				var newstr="";
				str = parseInt(str)+"";
				//newstr = str;
				for (i=0;i<str.length;i=i+settings.format) {
					j=str.length-i;
					if (j<settings.format) newstr = str.substring(0,j) + " " + newstr;
					else newstr = str.substring(j-settings.format,j) + " " + newstr;
				}
				newstr = newstr+" "+settings.prefix;
				return newstr;
			}
			
			var newprice = settings.newvalue;
			var oldprice = settings.oldvalue;
			
			change = (newprice-oldprice)/(settings.speed/settings.time);
			id=setInterval(function() {
				oldprice += change;
				jQuery(settings.counter).text(ownformat(oldprice));
			},settings.time);
			id2=setTimeout(function() { 
				clearInterval(id); 
				if (newprice>0) jQuery(settings.counter).text(ownformat(newprice));
				else {
					if (settings.hideNull) jQuery(settings.counter).text('');
					else jQuery(settings.counter).text(ownformat('0'));
				}
			},settings.speed+settings.time);

		})
};

