
jQuery.fn.animateCount = function(settings)
{
	settings = jQuery.extend(
		{
			counter:false,
			speed:400,
			price:124,
			time:20,
			prefix:"руб.",
			format:3,
			hideNull:true,
			maxlength:3,
			discount:false
		}, 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 oldvalue=0;
			var newvalue;
			var oldprice;
			var newprice;
			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();
				
				//alert(e.which);
				//alert("123");
				//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);
					oldprice = (100-parseInt(jQuery(settings.discount).text()))*oldvalue*settings.price/100;
					newprice = (100-parseInt(jQuery(settings.discount).text()))*newvalue*settings.price/100;
					jQuery(settings.counter).text(ownformat(oldprice));
					
					change = (newprice-oldprice)/(settings.speed/settings.time);

					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();

		})
};





jQuery.fn.animateCountSum = function(settings)
{
	settings = jQuery.extend(
		{
			speed:400,
			time:20,
			prefix:"руб.",
			format:3,
			hideNull:true,
			maxlength:3
		}, 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 oldvalue=0;
			var newvalue;
			var oldprice;
			var newprice;
			var change;
			var id;
			var id2;
			var newinner;
			var i;
			var j;
			//settings.counter = jQuery(this).next().next();		
		
			
		
			jQuery(this).keydown(function() {	

                oldprice = 0;
				jQuery("#cartFull .final-price").each(function() {
                     if (jQuery(this).text()) oldprice += parseInt(jQuery(this).text());  
                });

			});

			jQuery(this).keyup(function() {
				newprice = 0;
				jQuery("#cartFull .final-price").each(function() {
						if (jQuery(this).text()) newprice += parseInt(jQuery(this).text());  
				});
                                
				if (settings.counter) {
					clearInterval(id);
					clearInterval(id2);
					
					jQuery(settings.counter).text(ownformat(oldprice));
					
					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==true) jQuery(settings.counter).text('');
							else jQuery(settings.counter).text(ownformat('0'));
						}
					},settings.speed+settings.time);
				}
			}).keyup();

		})
};

