jQuery(document).ready(function(){

    $("#l1").click(function(event){

		event.preventDefault();

		$("#t2, #t3").css('z-index', 0);

		$("#t1").css('z-index', 1);

	})

	$("#l2").click(function(event){

		event.preventDefault();

		$("#t1, #t3").css('z-index', 0);

		$("#t2").css('z-index', 1);

	})

	$("#l3").click(function(event){

		event.preventDefault();

		$("#t2, #t1").css('z-index', 0);

		$("#t3").css('z-index', 1);

	})

	

	/*call back*/

	$("#call_back").click(function(){

		window.open("http://www.shelestshin.octopusline.ru/widgets/callme_new.php?service_instance_id=111", "CallMe", "width=320, height=350, resizable=no, toolbar=0, status=0, location=0, directories=0, menubar=0, scrollbars=0");

	})

	

	/*basket click*/

	$("#shop_pnl").click(function(){

		window.location.href="http://www.shelestshin.ru/basket/";

	})

	

	

	/*filling form*/

	$("#mid").change(function(){

		var vendor = $("#mid option:selected").val();

		$.post("http://www.shelestshin.ru/header_script/choose_model.php", { head_vendor : vendor}, function(data){

			$("#mid2").html(data);

			$("#mid2").removeAttr("disabled");

		})

	})

	

	$("#mid2").change(function(){

		var vendor = $("#mid option:selected").val();

		var model = $("#mid2 option:selected").val();

		$.post("http://www.shelestshin.ru/header_script/choose_year.php", { head_vendor : vendor, head_model : model}, function(data){

			$("#mid3").html(data);

			$("#mid3").removeAttr("disabled");

		})

	})

	

	$("#mid3").change(function(){

		var vendor = $("#mid option:selected").val();

		var model = $("#mid2 option:selected").val();

		var year = $("#mid3 option:selected").val();

		$.post("http://www.shelestshin.ru/header_script/choose_modification.php", { head_vendor : vendor, head_model : model, head_year : year}, function(data){

			$("#mid4").html(data);

			$("#mid4").removeAttr("disabled");

		})

	})

	

	

	

	//tabs

	

	$(".short_active a").click(function(event){

		event.preventDefault();

	})

	

	$(".short_inactive a").click(function(event){

		$(".short_ul li").removeClass('short_active').addClass('short_inactive');

		$(this).parent("li").removeClass('short_inactive').addClass('short_active');

		$(".short_content").hide();

		event.preventDefault();

	})

	

	$(".short_ul li a:first").click();

	$("#short_description").show();

	

	

	

	$("#a_description").click(function(event){

		$("#short_description").show();

		event.preventDefault();

	})

	

	$("#a_delivery").click(function(event){

		$("#short_delivery").show();

		event.preventDefault();

	})

	

	$("#a_payment").click(function(event){

		$("#short_payment").show();

		event.preventDefault();

	})

	

	$("#a_guarantee").click(function(event){

		$("#short_guarantee").show();

		event.preventDefault();

	})

	

	$("#a_insurance").click(function(event){

		$("#short_insurance").show();

		event.preventDefault();

	})

	

	$("#a_fitting").click(function(event){

		$("#short_fitting").show();

		event.preventDefault();

	})
	$(".selector a").click(function(){
		$("#currentprice").text($(this).attr('rel'));
	    $(".selector a").each(function () {
	      	$(this).removeClass('active');	     
	    });
		$(this).addClass('active');
	})
	
	$("#submitbtn").click(function(){
		window.location='/invoice.php?return=/basket/&command=add_complect&args=101010c&args='+$('#complectid').val()+'c'+$(".selector a.active").text();
		return false;
	})
	$(".plus").click(function(){
		var counter = $("#"+$(this).attr('rel'));
		if(counter.val() >= 0){
			counter.val(counter.val()*1 +1);
		}else{
			counter.val(1)
		}
		SummAll();
	})
	$(".minus").click(function(){
		var counter = $("#"+$(this).attr('rel'));
		if(counter.val() > 0){
			counter.val(counter.val() -1);
		}else{
			counter.val(0)
		}
		SummAll();
	})		
});
function SummAll(){
		$("#totalprice").text(0);
		$("#var_count").val(0);
		$("#var_price").val(0);
		
	    $(".summ").each(function () {
			var id = $(this).attr("id").replace(/[^0-9]/g, '');
			$("#var_count").val($("#var_count").val()*1 + $("#id"+id).val()*1);
			$("#var_price").val($("#var_price").val().replace(/[^0-9]/g, '')*1 + $("#id"+id).val()*$("#const_price_"+id).val().replace(/[^0-9]/g, ''));
	    });	
		var k = 0;
		if($("#var_price").val().replace(/[^0-9]/g, '')*1 <= $("#var_freedel").val().replace(/[^0-9]/g, '')*1 && $("#var_count").val()*1 > 0){
			k = Math.ceil($("#var_deliveryprice").val() / $("#var_count").val());
		}

		$(".summ").each(function () {
			var id = $(this).attr("id").replace(/[^0-9]/g, '');
			$("#price_"+id).text($("#const_price_"+id).val().replace(/[^0-9]/g, '')*1 + k); 
			$("#pricet_"+id).text(($("#const_price_"+id).val().replace(/[^0-9]/g, '')*1 + k)*$("#id"+id).val()); 

		});
		if($("#var_price").val().replace(/[^0-9]/g, '')*1 <= $("#var_freedel").val().replace(/[^0-9]/g, '')*1){
			$("#totalprice").text($("#var_price").val().replace(/[^0-9]/g, '')*1 + $("#var_deliveryprice").val()*1);
		}else{
			$("#totalprice").text($("#var_price").val().replace(/[^0-9]/g, ''));
		}
		if($("#var_price").val().replace(/[^0-9]/g, '')*1<1){
			$("#totalprice").text(0);
		}
		document.forms.basket.command.value = 'update';
		document.forms.basket.submit();
}


