$(document).ready(function(){

$("#headerzaliv").click(function() {	
	if ($("#allzaliv").is(":hidden"))
		$("#allzaliv").slideDown(200);
	else $("#allzaliv").slideUp(200);
	return false;
});

$('#headeravto').click(function() {
	if ($('#allavto').is(":hidden")) $("#allavto").slideDown(200);
	else $("#allavto").slideUp(200);
	return false;
});

$('#header1').click(function() {
	if ($('#alliznos').is(":hidden")) $("#alliznos").slideDown(200);
	else $("#alliznos").slideUp(200);
	return false;
});

$('#header2').click(function() {
	if ($('#all2').is(":hidden")) $("#all2").slideDown(200);
	else $("#all2").slideUp(200);
	return false;
});


$("input.calc").checkBox();

$('#start').click(function(){
	var error = '';

	var deltat;
	var deltal;

	var now = new Date().getFullYear();
	var years = now - $('#year').val();

	if (years < 0 || !$('#year').val() || $('#year').val() < 0)
		error += 'Неправильно введен год\n';

	var mileage = $('#mileage').val()/1000;

	if (mileage < 0 || !mileage)
		error += 'Неправильно введен пробег\n';

	var warranty = $('#warranty').val();

	if (warranty < 0 || !warranty)
		error += 'Неправильно введена гарантия от сквозной коррозии\n';

	var protector = $('#protector').val();

	if (protector < 0 || !protector)
		error += 'Неправильно введена высота протектора\n';

	var battery = $('#battery').val();

	if (protector < 0 || !protector)
		error += 'Неправильно указан возраст аккумулятора\n';

	if (error)
	{
		alert(error);
		return false;
	}
	
	switch ($('#type').val())
	{
		case '1':
			deltat = 0.055;
			deltal = 0.0028;
		break;

		case '2':
			deltat = 0.08;
			deltal = 0.0024;
		break;

		case '3':
			deltat = 0.072;
			deltal = 0.0016;
		break;

		case '4':
			deltat = 0.12;
			deltal = 0.002;
		break;

		case '5':
			deltat = 0.11;
			deltal = 0.0016;
		break;

		case '6':
			deltat = 0.122;
			deltal = 0.0008;
		break;

		case '7':
			deltat = 0.04;
			deltal = 0.002;
		break;

		case '8':
			deltat = 0.044;
			deltal = 0.0024;
		break;

		case '9':
			deltat = 0.05;
			deltal = 0.0026;
		break;

		case '10':
			deltat = 0.036;
			deltal = 0.0016;
		break;

		case '11':
			deltat = 0.072;
			deltal = 0.0017;
		break;

		case '12':
			deltat = 0.096;
			deltal = 0.0008;
		break;
	}

	var body = Math.round(100*(1-Math.pow(2.72, -((4*years)/(20+4*warranty)))));
	var other = Math.round(100*(1-Math.pow(2.72, -((deltat*years+deltal*mileage)))));
	var plastic = Math.round(100*(1-Math.pow(2.72, (-0.1*years))));

	var batterytime = (mileage/years) > 40 ? 4 : 3;
	var accumulator = Math.round((battery/batterytime)*100);
	
	var tire = Math.round((10-protector)/(10-1.6)*100);
	
	var timetogo_body = 0;
	var timetogo_other = 0;
	var timetogo_plastic = 0;
	var timetogo_accumulator = 0;
	var timetogo_tire = 0;

	$('.line').css('background','#7baf5f');

	var timer_body = window.setInterval(function()
	{
	    $('#counter_body').text(timetogo_body);
	    $('#line_body').width(timetogo_body*3);

	    if (timetogo_body <= 66 && timetogo_body >= 33)
	    	$('#line_body').css('background','#f6c749');
	    else if (timetogo_body >= 66)
	    	$('#line_body').css('background','#b30114');

	    if (timetogo_body >= body || timetogo_body >= 80)
			window.clearInterval(timer_body);

	    timetogo_body++;
	}, 30);

	var timer_other = window.setInterval(function()
	{
	    $('#counter_other').text(timetogo_other);
	    $('#line_other').width(timetogo_other*3);

	    if (timetogo_other <= 66 && timetogo_other >= 33)
	    	$('#line_other').css('background','#f6c749');
	    else if (timetogo_other >= 66)
	    	$('#line_other').css('background','#b30114');

	    if (timetogo_other >= other || timetogo_other >= 80)
			window.clearInterval(timer_other);

	    timetogo_other++;
	}, 30);

	var timer_plastic = window.setInterval(function()
	{
	    $('#counter_plastic').text(timetogo_plastic);
	    $('#line_plastic').width(timetogo_plastic*3);

	    if (timetogo_plastic <= 66 && timetogo_plastic >= 33)
	    	$('#line_plastic').css('background','#f6c749');
	    else if (timetogo_plastic >= 66)
	    	$('#line_plastic').css('background','#b30114');

	    if (timetogo_plastic >= plastic || timetogo_plastic >= 80)
			window.clearInterval(timer_plastic);

	    timetogo_plastic++;
	}, 30);

	var timer_accumulator = window.setInterval(function()
	{
	    $('#counter_accumulator').text(timetogo_accumulator);
	    $('#line_accumulator').width(timetogo_accumulator*3);

	    if (timetogo_accumulator <= 66 && timetogo_accumulator >= 33)
	    	$('#line_accumulator').css('background','#f6c749');
	    else if (timetogo_accumulator >= 66)
	    	$('#line_accumulator').css('background','#b30114');

	    if (timetogo_accumulator >= accumulator || timetogo_accumulator >= 80)
			window.clearInterval(timer_accumulator);

	    timetogo_accumulator++;
	}, 30);

	var timer_tire = window.setInterval(function()
	{
	    $('#counter_tire').text(timetogo_tire);
	    $('#line_tire').width(timetogo_tire*3);

	    if (timetogo_tire <= 66 && timetogo_tire >= 33)
	    	$('#line_tire').css('background','#f6c749');
	    else if (timetogo_tire >= 66)
	    	$('#line_tire').css('background','#b30114');

	    if (timetogo_tire >= tire || timetogo_tire >= 80)
			window.clearInterval(timer_tire);

	    timetogo_tire++;
	}, 30);
	
	return false;
});

$('#start2').click(function(){
	var error = '';

	var deltat;
	var deltal;

	var now = new Date().getFullYear();
	var years = now - $('#year2').val();

	if (years < 0 || !$('#year2').val() || $('#year2').val() < 0)
		error += 'Неправильно введен год\n';

	var mileage = $('#mileage2').val();

	if (mileage < 0 || !mileage)
		error += 'Неправильно введен пробег\n';

	if (error)
	{
		alert(error);
		return false;
	}
	
	switch ($('#type2').val())
	{
		case '1':
			deltat = 0.07;
			deltal = 0.0035;
		break;

		case '2':
			deltat = 0.1;
			deltal = 0.003;
		break;

		case '3':
			deltat = 0.09;
			deltal = 0.002;
		break;

		case '4':
			deltat = 0.15;
			deltal = 0.0025;
		break;

		case '5':
			deltat = 0.14;
			deltal = 0.002;
		break;

		case '6':
			deltat = 0.16;
			deltal = 0.001;
		break;

		case '7':
			deltat = 0.05;
			deltal = 0.0025;
		break;

		case '8':
			deltat = 0.055;
			deltal = 0.003;
		break;

		case '9':
			deltat = 0.065;
			deltal = 0.0032;
		break;

		case '10':
			deltat = 0.045;
			deltal = 0.002;
		break;

		case '11':
			deltat = 0.09;
			deltal = 0.002;
		break;

		case '12':
			deltat = 0.12;
			deltal = 0.001;
		break;
	}

	var sigma = (deltat * years) + (deltal * mileage)/1000;
	var body = Math.round(100*(1-Math.pow(2.718, -sigma)));
		
	var timetogo_body = 0;

	$('.line2').css('background','#7baf5f');

	var timer_body = window.setInterval(function()
	{
	    $('#counter_body2').text(timetogo_body);
	    $('#line_body2').width(timetogo_body*3);

	    if (timetogo_body <= 66 && timetogo_body >= 33)
	    	$('#line_body2').css('background','#f6c749');
	    else if (timetogo_body >= 66)
	    	$('#line_body2').css('background','#b30114');

	    if (timetogo_body >= body || timetogo_body >= 80)
			window.clearInterval(timer_body);

	    timetogo_body++;
	}, 30);


	return false;
});

$(".calc").click(function() {
	var checkcount = $(".calc:checked").length;
	var summ = 0;
	if (checkcount) summ = 4500 + (checkcount * 1500);
	$("#summzaliv").html(summ + " руб.");
});

if (jQuery.browser.msie)
{
	$('.count').css('padding-top', '6px');
}

$('.selectBlock').sSelect();

$('.count').live('keyup change', function() {
	var count = $(this).val();
	var summ = count * 300;
	if (summ > 6000) summ = 9000;
	else if (summ != 0) summ = 3000 + summ;
	$('#summavto').html(summ + ' руб.');
});

});
