
jQuery.fn.count = function(settings)
{
	settings = jQuery.extend(
		{
			counter:false,
			price:124,
			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() {
				
				
				//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 && unicode!=9 ) 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) {
					//alert(oldvalue);
					//oldprice = jQuery(settings.counter).text();
					//oldprice = oldvalue*settings.price;
					newprice = newvalue*settings.price;
					//jQuery(settings.counter).text(format(newstr));
					//jQuery(settings.counter).text(ownformat(oldprice));
					
					//alert ((settings.speed/settings.time));
					//alert ((newprice-oldprice));
					//alert (change);
					if (newprice>0) jQuery(settings.counter).text(ownformat(newprice));
					else {
							if (settings.hideNull) jQuery(settings.counter).text('');
							else jQuery(settings.counter).text(ownformat('0'));
						}
				}
			}).keyup();

		})
};
