var nochange;
var avansPos=25;
var yearPos=3;
function switch_avans(el)
{
	var avans = $('#avans-price');
	var model = el.id == 'span-avans-rub' ? 'currency' : 'percent';

	if (avans.attr('_state') == model)
	{
		return false;
	}

	var percent = $("#slider-procents").slider("value");

	$('#avans-price').val(model == 'currency'
		? Math.round($('#calc-price').val() / 100) * percent
		: percent);

	avans.attr('_state', model);
}

$(function() {
	$('#calc-price, #avans-price').keydown(function(e) {
		//console.log(e.keyCode);
		return e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 116 || e.keyCode == 110 || e.keyCode == 190
			|| (e.keyCode >= 35 && e.keyCode <= 40)
			|| (e.keyCode >= 48 && e.keyCode <= 57)
			|| (e.keyCode >= 96 && e.keyCode <= 105);
	});

	$('#avans-price').blur(function(){
		return;
		var val = null;
		var price = $('#calc-price');

		switch ($(this).attr('_state'))
		{
			case 'currency':
				if (parseFloat($(this).val()) > parseFloat(price.val()))
				{
					val = price.val();
				}
				break;

			case 'percent':
			default:
				var v = parseInt($(this).val());

				if (v < 0)
				{
					val = 0;
				}
				else if (v > 100)
				{
					val = 100;
				}
		}

		if (val != null)
		{
			$(this).val(val);
		}
	});

	var position_slider_percent = function(percent)
	{
		var p_width = $('div#slider-procents').width() / 100;

		if (percent < 10)
		{
			$('div#slider-procents a span').css('left', p_width * (10 - percent));
		}
		else if (percent > 80)
		{
			$('div#slider-procents a span').css('left', p_width * (80 - percent));
		}
		else
		{
			$('div#slider-procents a span').css('left', -5);
		}

		$('div#slider-procents a span').html(percent+'%');
	}

	$("#slider-procents").slider({
		value : avansPos?avansPos:30,
		min : 0,
		max : 100,
		step : 1,
		slide : function(event, ui) {
			position_slider_percent(ui.value);
			recalc_slider_avans(ui.value);
			//calculate();
		},
		change : function(event, ui) {
			position_slider_percent(ui.value);
			//recalc_slider_avans(ui.value);
			calculate();
			//calc($("#calc-price").val(), $("#avans-price").val(), $("#slider-procents").slider("value"), $("#slider-years").slider("value"));
		}
	});

	$("#amount").val("$" + $("#slider-procents").slider("value"));

	$("#slider-years").slider({
		value : yearPos?yearPos:2,
		min : 1,
		max : 5,
		step : 1,
		slide : function(event, ui) {
			$("#amount").val("$" + ui.value);
		},
		change : function(event, ui) {
			calculate();
			//calc($("#calc-price").val(), $("#avans-price").val(), $("#slider-procents").slider("value"), $("#slider-years").slider("value"));
		}
	});

	$("#price-rub-input").prev().click(function() {
		alert($(this).val());
	});

	//$('span[id="span-avans-procent"], span[id="span-avans-rub"]').click();
});

function calc(price, avans, procent, years)
{
	if (!isNumber(price) || !isNumber(avans))
	{
		return false;
	}

	var st;
	// ���������� ���������
	var SuD;
	// ����� �������
	var av;
	// ����� (��� ���)
	var LPM;
	// �������� ���������� �������
	var NDS = 0.18;
	var kurs_k = 1;
	// ����������, �� ������ ��� ������ ������� 1
	var sumfinans;
	// ����� ��������������
	var kom;
	// ��������
	var LP;
	// ������ ����������� ��������
	var lpravn;
	//���������� ������� � ���
	var PoGoD;
	// ����� ��������� ��������� �����
	var platezh;
	// ������
	var x;
	var lpravn12;
	var temp1, temp2;
	var SrD;
	price = Number(price.replace(/\s/g, ""));
	currentvalut = $('.currency-valut-value').html();
	currentrub = $('.currency-rub-value').html();
	currentdol = $('.currency-dol-value').html();

	if(currentvalut == 'dol') {
		price = price * currentdol;
	}

	//price=800000;

	avans = procent / 100;
	//years=3;
	procent = 50;
	st = price / (1 + NDS);
	av = avans * st;
	SuD = (st - av) * (1 + NDS);
	sumfinans = SuD * kurs_k;
	kom = 0.90;
	LP = 0.19;
	if(sumfinans > 600000) {
		kom = 0.90;
		LP = 0.18;
	};
	if(sumfinans > 1500000) {
		kom = 0.90;
		LP = 0.175;
	};
	if(sumfinans > 3000000) {
		kom = 0.90;
		LP = 0.17;
	};
	if(sumfinans > 6000000) {
		kom = 0.75;
		LP = 0.16;
	};
	if(sumfinans > 15000000) {
		kom = 0.50;
		LP = 0.15;
	};
	if(sumfinans > 30000000) {
		kom = 0.30;
		LP = 0.14;
	};
	LPM = LP / 12;
	lpravn = LP * (1 + NDS);
	PoGoD = parseInt(SuD / (years * 12 * (1 + NDS)));
	x = 1 + lpravn / 12;
	lpravn12 = lpravn / 12;
	SrD = years * 12;
	temp1 = Math.pow(x, SrD);
	temp2 = 1 / temp1;
	platezh = SuD * (lpravn12) / (1 - temp2);
	platezh = Number(platezh).toFixed(2)

	if(!isNaN(platezh)) {
		$('.calculater-payment-amount').html(platezh + ' <ins>���.</ins>');
	} else {
		$('.calculater-payment-amount').html('�������� ������');
	}

}

function isNumber(val) {
	return /^-?((\d+\.?\d?)|(\.\d+))$/.test(val);
}

function changecalcslider() {

	if (!isNumber($('#calc-price').val()) || !isNumber($('#avans-price').val()))
	{
		return false;
	}

	var slidervalue;
	var avans;
	var price;
	var currentvaluttotalprice;
	var currentvalutavans;
	var currentrub;
	var currentdol;
	nochange = 1;
	avans = $("#avans-price").val();
	price = $("#calc-price").val();
	currentvaluttotalprice = $('.currency-valut-value').html();
	currentvalutavans = $('.currency-avans-value').html();
	currentrub = $('.currency-rub-value').html();
	currentdol = $('.currency-dol-value').html();

	if(currentvalutavans == 'rub' && currentvaluttotalprice == 'dol') {
		slidervalue = avans * 100 / (price * currentdol);
	}
	if(currentvalutavans == 'rub' && currentvaluttotalprice == 'rub') {
		slidervalue = avans * 100 / price;
	}

	if(currentvalutavans == 'dol' && currentvaluttotalprice == 'dol') {
		slidervalue = avans * 100 / price;
	}
	if(currentvalutavans == 'dol' && currentvaluttotalprice == 'rub') {
		slidervalue = avans * currentdol * 100 / price;
	}

	if(currentvalutavans == 'procent') {
		slidervalue = avans;
		if(slidervalue > 100) {
			slidervalue = 100
		};
		if(slidervalue < 0) {
			slidervalue = 0
		};
	}
	slidervalue = Number(slidervalue).toFixed();

	$("#slider-procents").slider({
		value : slidervalue
	});
}

function recalc_price(valut) {

	var currentvalut;
	var currentcurs;
	var currentrub;
	var currentdol;
	var oldprice;
	var newprice;
	oldprice = $("#calc-price").val();
	currentvalut = $('.currency-valut-value').html();
	currentrub = $('.currency-rub-value').html();
	currentdol = $('.currency-dol-value').html();

	if(valut == 'rub') {
		//			$('#span-avans-dol').attr("disabled","disabled");
		//			$('#span-avans-rub').attr("disabled","enabled");
	} else {
		//			$('#span-avans-rub').attr("disabled","disabled");
		//			$('#span-avans-dol').attr("disabled","enabled");
	}

	if(currentvalut != valut) {
		if(valut == 'rub') {
			newprice = oldprice * currentdol;
		} else {
			newprice = oldprice / currentdol;
		}
		document.getElementById('calc-price').value = newprice.toFixed(2);

	}

	$('.currency-valut-value').html(valut);
	calc($("#calc-price").val(), $("#avans-price").val(), $("#slider-procents").slider("value"), $("#slider-years").slider("value"));
}
function recalc_avans(valut) {
	var currentvalut;
	var currentrub;
	var currentdol;
	var oldprice;
	var totalprice;
	var newprice;
	var currentvaluttotalprice;
	oldprice = $("#avans-price").val();
	totalprice = $("#calc-price").val();
	currentvaluttotalprice = $('.currency-valut-value').html();
	currentvalut=$('.currency-avans-value').html();
	if(valut!=='procent'&&currentvaluttotalprice!=valut) {
		$('input[spanid=span-avans-'+currentvalut+']').click();
		alert('������ ������ ���� ����������!');
		return false;
	};
	currentrub = $('.currency-rub-value').html();
	currentdol = $('.currency-dol-value').html();
	if(currentvalut != valut) {
		if(currentvalut == 'procent' && valut == 'rub') {
			if(currentvaluttotalprice == 'rub') {
				newprice = totalprice * oldprice / 100;
			}
			if(currentvaluttotalprice == 'dol') {
				newprice = totalprice * currentdol * oldprice / 100;
			}
		}
		if(currentvalut == 'dol' && valut == 'rub') {
			newprice = oldprice * currentdol;
		}

		if(currentvalut == 'procent' && valut == 'dol') {
			if(currentvaluttotalprice == 'dol') {
				newprice = totalprice * oldprice / 100;
			}
			if(currentvaluttotalprice == 'rub') {
				newprice = totalprice * oldprice / (100 * currentdol);
			}
		}
		if(currentvalut == 'rub' && valut == 'dol') {
			newprice = oldprice / currentdol;
		}

		if(currentvalut == 'rub' && valut == 'procent') {
			if(currentvaluttotalprice == 'rub') {
				newprice = oldprice * 100 / totalprice;
			}
			if(currentvaluttotalprice == 'dol') {
				newprice = oldprice * 100 * currentdol / totalprice;
			}

		}
		if(currentvalut == 'dol' && valut == 'procent') {
			if(currentvaluttotalprice == 'dol') {
				newprice = oldprice * 100 / totalprice;
			}
			if(currentvaluttotalprice == 'rub') {
				newprice = oldprice * 100 * currentdol / totalprice;
			}
		}
		document.getElementById('avans-price').value = newprice.toFixed(0);
	}

	$('.currency-avans-value').html(valut);
	changecalcslider();
}

function recalc_slider_avans(percent)
{
	$('#avans-price').val($('input[spanid="span-avans-rub"]').prop('checked')
		? Math.round($('#calc-price').val() / 100) * percent
		: percent);
}

