var oldprice=0;

var counter = '#calc-price';
var speed = 400;
var time = 20;
var prefix = "руб.";
var format = 3;
var hideNull = true;
var maxlength = 5;

var id;
var id2;
var i;
var j;

function ownformat(str) {
				if (str==0) return "";
				var newstr="";
				str = parseInt(str)+"";
				//newstr = str;
				for (i=0;i<str.length;i=i+format) {
					j=str.length-i;
					if (j<format) newstr = str.substring(0,j) + " " + newstr;
					else newstr = str.substring(j-format,j) + " " + newstr;
				}
				newstr = newstr+" "+prefix;
				return newstr;
			}


function calculateValue() {
	
	var price=0;
	if (parseInt($('#calc .choose1 input:last').val())<100) price = 2000;
	else if (parseInt($('#calc .choose1 input:last').val())<200 && parseInt($('#calc .choose1 input:last').val())>=100) price = 1500;
	else if (parseInt($('#calc .choose1 input:last').val())<400 && parseInt($('#calc .choose1 input:last').val())>=200) price = 1300;	
	else if (parseInt($('#calc .choose1 input:last').val())>=400) price = 1000;
		
	if (parseInt($('#calc .choose1 input:first').val())) var price1 = parseInt($('#calc .choose1 input:first').val());
	else var price1 = 0;

	if (parseInt($('#calc .choose1 input:eq(1)').val())) var price2 = parseInt($('#calc .choose1 input:eq(1)').val());
	else var price2 = 0;

	if (parseInt($('#calc .choose1 input:last').val())) var price3 = parseInt($('#calc .choose1 input:last').val());
	else var price3 = 0;

	var oldvalue = price1*400 + price2*400 + price3*price;

	return oldvalue;
	
}

$(document).ready(function()
		{
		
						
		$('#calc .choose-type a').click(function() {
		
			if ($(this).hasClass('active-link')) return false;
			$('#calc .choose-type a').removeClass('active-link');
			$(this).addClass('active-link');

		});
		
		$('#calc .choose-type a:first').click(function() {
			$('.choose2').slideUp("fast");
			$('#calc-price').empty();
			setTimeout(function() { $('.choose1').slideDown("fast"); },200);
		}).click();
		
		$('#calc .choose-type a:last').click(function() {
			$('.choose1').slideUp("fast");
			$('#calc-price').empty();
			setTimeout(function() { $('.choose2').slideDown("fast"); },200);
		});
				
					             
		$('#calc .material').click(function() {
		
			if ($(this).hasClass('active-link')) return false;
			$('#calc .material').removeClass('active-link');
			$(this).addClass('active-link');
			$('.step2').show();
			$('.inner-table').slideUp("fast");
			$('#calc-price').empty();
			$('.inner-table:eq('+$(this).attr('rel')+')').slideDown("fast");

		});
		
		$('#calc .mat1').click(function() {
		
			if ($(this).hasClass('active-link')) return false;
			$('#calc .mat1').removeClass('active-link');
			$(this).addClass('active-link');
			$('.step3').show();
			$('.inner-input1').slideUp("fast");
			$('.inner-input2').slideUp("fast");
			$('.inner-input3').slideUp("fast");
			$('#calc-price').empty();
			$('.inner-input1:eq('+$(this).attr('rel')+')').slideDown("fast");

		});
		
		$('#calc .mat2').click(function() {
		
			if ($(this).hasClass('active-link')) return false;
			$('#calc .mat2').removeClass('active-link');
			$(this).addClass('active-link');
			$('.step3').show();
			$('.inner-input1').slideUp("fast");
			$('.inner-input2').slideUp("fast");
			$('.inner-input3').slideUp("fast");
			$('#calc-price').empty();
			$('.inner-input2:eq('+$(this).attr('rel')+')').slideDown("fast");

		});
		
		$('#calc .mat3').click(function() {
		
			if ($(this).hasClass('active-link')) return false;
			$('#calc .mat3').removeClass('active-link');
			$(this).addClass('active-link');
			$('.step3').show();
			$('.inner-input1').slideUp("fast");
			$('.inner-input2').slideUp("fast");
			$('.inner-input3').slideUp("fast");
			$('#calc-price').empty();
			$('.inner-input3:eq('+$(this).attr('rel')+')').slideDown("fast");


		});
		
		//$('#calc .choose1 input:first').each(function() { $(this).animateCount({counter:'#calc-price', counterRes:'#calc-price1', price:'400' }); } );
		//$('#calc .choose1 input:eq(1)').each(function() { $(this).animateCount({counter:'#calc-price', counterRes:'#calc-price2', price:'400' }); } );
		//$('#calc .choose1 input:last').each(function() { $(this).animateCount({counter:'#calc-price', counterRes:'#calc-price3', price:'1500', special:'1'}); } );

		$('#calc .inner-input1 input:first').each(function() { $(this).animateCount({counter:'#calc-price', price:'30000' }); } );
		$('#calc .inner-input1 input:eq(1)').each(function() { $(this).animateCount({counter:'#calc-price', price:'25000' }); } );
		$('#calc .inner-input1 input:last').each(function() { $(this).animateCount({counter:'#calc-price', price:'25000' }); } );

		$('#calc .inner-input2 input:first').each(function() { $(this).animateCount({counter:'#calc-price', price:'20000' }); } );
		$('#calc .inner-input2 input:eq(1)').each(function() { $(this).animateCount({counter:'#calc-price', price:'25000' }); } );
		$('#calc .inner-input2 input:last').each(function() { $(this).animateCount({counter:'#calc-price', price:'28000' }); } );
		
		$('#calc .inner-input3 input:first').each(function() { $(this).animateCount({counter:'#calc-price', price:'21000'}); } );
		$('#calc .inner-input3 input:eq(1)').each(function() { $(this).animateCount({counter:'#calc-price', price:'26000' }); } );
		$('#calc .inner-input3 input:last').each(function() { $(this).animateCount({counter:'#calc-price', price:'28000' }); } );	
		
		$('#calc .choose1 input').keydown(function() {
			oldprice = calculateValue();
		});
		
		$('#calc .choose1 input').keypress(function(e) {
				
				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);
				//alert (unicode);
		});
		
		$('#calc .choose1 input').keyup(function() {
				
				var newprice = calculateValue();

				if (counter) {
					clearInterval(id);
					clearInterval(id2);
						
					jQuery(counter).text(ownformat(oldprice));
					
					var change = (newprice-oldprice)/(speed/time);

					id=setInterval(function() {
						oldprice += change;
						jQuery(counter).text(ownformat(oldprice));
					},time);
					
					id2=setTimeout(function() { 
						clearInterval(id); 
						if (newprice>0) jQuery(counter).text(ownformat(newprice));
						else {
							if (hideNull) jQuery(counter).text('');
							else jQuery(counter).text(ownformat('0'));
						}
					},speed+time);
				}
			}).keyup();
		
		
		
});
