/**	C O M M O N ******************************************/
var where2buy_position = 0;
jQuery.fn.anchorAnimate = function(settings) {
	settings = jQuery.extend({speed:1000}, settings);	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}
$(document).ready(function() {
	$('#js-openbox').click(function() {
		$('#js-box').slideToggle();
		$(this).hide();
	});

	$('.b-langs__item').hover(function() {
		var lang = ($(this).attr('id')=='ru') ? 'en' : 'ru';
		if (!$(this).hasClass('b-langs__item_active')) {
			$('.b-langs__item-'+lang).animate({top:'-12px'}, 0.3);
			$('.b-langs__link-'+lang).animate({opacity:'0.5'}, 0.3);
		}
	}, function() {
		var lang = ($(this).attr('id')=='ru') ? 'en' : 'ru';
		if (!$(this).hasClass('b-langs__item_active')) {
			$('.b-langs__item-'+lang).animate({top:'0px'}, 0.3);
			$('.b-langs__link-'+lang).animate({opacity:'1'}, 0.3);
		}
	})

//	$('#js-where2buy').tinyscrollbar({size:226});
/*	where2buy_position = '-'+($('#js-where2buy').height()-134)+'px';
	$('#js-where2buy').css('bottom', where2buy_position);
	$('#js-where2buy__toggle').toggle(function() {
		$('#js-where2buy').animate({bottom:'108px'}, 500);
	}, function() {
		$('#js-where2buy').animate({bottom:where2buy_position}, 500);
	})
*/
/*	$('.b-langs__item-en').hover(function() {
		$('.b-langs__item-ru').animate({top:'-12px'}, 0.3);
		$('.b-langs__link-ru').animate({opacity:'0.5'}, 0.3);
	}, function() {
		$('.b-langs__item-ru').animate({top:'0px'}, 0.3);
		$('.b-langs__link-ru').animate({opacity:'1'}, 0.3);
	})
*/
	$('.b-menu__item').hover(function() {
		$(this).children('.b-submenu').show();
	}, function() {
		$(this).children('.b-submenu').hide();
	})

	$('.b-btn-toggle').click(function () {
		$(this).children('span').toggleClass('g-minus');
		$(this).next('.b-box-toggle').slideToggle();
	});

//---- F O R M   O R D E R ---------------------------
	$('#obl').change(function() {
		var obl_id = $(this).val();
		$.post('/index-ajax.php', {cmd:'get_gorod', obl_id:obl_id, q:'assets/snippets/novaposhta/novaposhta.php'},
			function(data) {
				$('#gorod').html('<option>--- Выберите город ---</option>'+data);
			},
			'html'
		);
	});

	$('#gorod').change(function() {
		var gorod_id = $(this).val();
		$.post('/index-ajax.php', {cmd:'get_sklad', gorod_id:gorod_id, q:'assets/snippets/novaposhta/novaposhta.php'},
			function(data) {
				$('#sklad').html('<option>--- Выберите склад ---</option>'+data);
			},
			'html'
		);
	});

//---- C A L C U L A T E ---------------------------
	$('.increment-int, .increment-float').after('<div class="inc btn-inc">+</div><div class="dec btn-inc">-</div>');
	$('.btn-inc').click(function() {
		var $button = $(this);
		var input = $button.parent().find('input');
		var oldValue = input.val();
		
		if (!oldValue)
			oldValue = 0;

		if ($button.text() == '+') {
			if (input.hasClass('increment-int')) {
				var newVal = parseInt(oldValue) + 1;
			}
			else {
				var newVal = parseFloat(oldValue) + 0.1;
				newVal = newVal.toFixed(1);
			}
		} else {
			// Don't allow decrementing below zero
			if (input.hasClass('increment-int')) {
				if (oldValue >= 1)
					var newVal = parseInt(oldValue) - 1;
			}
			else {
				if (oldValue > 0.1) {
					var newVal = parseFloat(oldValue) - 0.1;
					newVal = newVal.toFixed(1);
				}
			}
		}
		if (!newVal) {
			if (input.hasClass('increment-int'))
				newVal = '0';
			else
				newVal = '0.1';
		}
		$button.parent().find('input').val(newVal);
		checkShowCalculate();
	});

	$('#js-engine1, #js-engine2').click(function() {
		checkShowCalculate();
	});//close click(

	$('#js-volume').keyup(function() {
		checkShowCalculate();
	});//close click(

	function checkShowCalculate() {
		console.log($('#js-engine1').attr('checked')+','+$('#js-engine2').attr('checked')+','+$('#js-volume').val());

		if (($('#js-engine1').attr('checked')==='checked' || $('#js-engine2').attr('checked')==='checked') && $('#js-volume').val())
			$('#js-calculate').removeAttr('disabled');
		else
			$('#js-calculate').attr('disabled', 'disabled');
	}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
	$('#js-calculate').click(function() {
		var regText = /\d+\.?\d*/;
		var count = 0;
		var vol = $('#js-volume').val();
		vol = vol.replace(/,/, ".");

		if ($('#js-engine1').attr('checked')) {

			if (regText.test(vol))	{
				if (vol == '')	alert('Введите объем двигателя');
				else if (vol <= 2.5)	count = 1;
				else if ((vol > 2.5) && (vol <= 5))	count = 2;
				else if (vol > 5)	count = 3;
			}
			else {
				alert('Не верно введен объем двигателя\n необходимо ввести в формате: 1.0');
			}
		}
		else if ($('#js-engine2').attr('checked')) {

			if (regText.test(vol))	{
				if (vol == "")	alert('Введите объем двигателя');
				else if (vol <= 2)	count = 1;
				else if ((vol > 2) && (vol <= 3))	count = 2;
				else if ((vol > 3) && (vol <= 8))	count = 3;
				else if (vol > 8)	count = 6;
			}
			else {
				alert('Не верно введен объем двигателя\n необходимо ввести в формате: 1.0');
			}
		}
		
		console.log(count);
		if (count > 0) {
			$('#count-fuelmax').html(count);

			$('#js-count-fuelmax').show();
			$('#count').fadeIn("slow");
		}

		return false;
	});

//	$('.anchorLink').anchorAnimate();
});
