
jQuery.fn.animateCount = function(settings)
{
	settings = jQuery.extend(
		{
			counter:false,
			counterRes:false,
			speed:400,
			price:124,
			time:20,
			prefix:"руб.",
			format:3,
			hideNull:true,
			maxlength:5,
			special:0,
			sum:0
		}, 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;
			}
			
			function priceParseInt(price) {
				while (price.indexOf(" ")>=0) price = price.replace(" ","");
				if (!price) return 0;
				else return parseInt(price);
			}
			
			var oldvalue=0;
			var newvalue;
			var oldprice;
			var newprice;
			var constprice;
			var change;
			var id;
			var id2;
			var newinner;
			var i;
			var j;
			//settings.counter = jQuery(this).next().next();		
		
			
		
			jQuery(this).keydown(function() {
				
				
				//clearInterval(id);
				oldvalue = jQuery(this).val();
				if (settings.sum) oldvalue = priceParseInt(jQuery('#calc-price1').text()) + priceParseInt(jQuery('#calc-price2').text()) + priceParseInt(jQuery('#calc-price3').text());
				//if (settings.sum) {
				//constprice = priceParseInt(jQuery('#calc-price1').text()) + priceParseInt(jQuery('#calc-price2').text()) + priceParseInt(jQuery('#calc-price3').text());
				//	oldprice = constprice;
				//}
				//alert(e.which);
				//alert(oldprice);
				//oldprice = oldvalue*settings.price;
				//jQuery(settings.counter).text(oldprice);
			});
			jQuery(this).keypress(function(e) {
				
				//alert(e.which);
				//alert("123");
				var unicode = e.which ? e.which : e.keyCode;
				if ((unicode < 48 || unicode > 57) && unicode!=46 && (unicode < 37 || unicode > 40) && unicode!=8 ) return false;						  
				if (unicode >= 48 && unicode <= 57 && jQuery(this).val().length >= settings.maxlength) return false;
				//alert (unicode);
			});
			jQuery(this).keyup(function() {
				var newvalue = jQuery(this).val();
				if (settings.counter) {
					clearInterval(id);
					clearInterval(id2);
					//alert(oldvalue);
					//oldprice = jQuery(settings.counter).text();
					
					if (settings.special) {
				
						if (newvalue>=0 && newvalue<100) settings.price=2000;
						if (newvalue>=100 && newvalue<200) settings.price=1500;
						if (newvalue>=200 && newvalue<400) settings.price=1300;
						if (newvalue>=400) settings.price=1000;
						
					}
					
					//if (settings.sum==0) {
					oldprice = oldvalue*settings.price;
					newprice = newvalue*settings.price;
					//}
					//else {
					//newprice = newvalue*settings.price + constprice;
					//}
					//jQuery(settings.counter).text(format(newstr));
					jQuery(settings.counter).text(ownformat(oldprice));
					
					change = (newprice-oldprice)/(settings.speed/settings.time);
					//alert ((settings.speed/settings.time));
					//alert ((newprice-oldprice));
					//alert (change);
					id=setInterval(function() {
						//if (!newinner) { newinner = parseInt(jQuery(settings.counter).text()); }
						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);
				}
			}).keyup();

		})
};
