$(document).ready(function(){
	
	
	$(".cttbl tr:even").css("background", "#f2f2f2");

	$(".inpr").focus( function(){
		var varid = $(this).attr("name");
		$("#bsadd_"+varid).hide();
		$("#bs_"+varid).show();
	});
	
	$(".plus").click(function(){
	
		var id = $(this).attr("id");

		var val = $("#c"+id).val();

		var varid = $(this).attr("name");
		
		if(val==""){
			val = 0;
		}else{
			val = eval(val)+1;
		};

		$("#c"+id).val(val);
		
		$("#bsadd_"+varid).hide();
		$("#bs_"+varid).show();

		var bask = $("#bask").val();

		if(bask == "ok"){
			pereschet();
		};
	
	});

	$(".minus").click(function(){
	
		var id = $(this).attr("id");

		var varid = $(this).attr("name");

		var val = $("#c"+id).val();

		if(val==""){
			val = 0;
			$("#c"+id).val(val);
			$("#bsadd_"+varid).hide();
			$("#bs_"+varid).show();
		}else{
			if(val!=0){
				val = eval(val)-1;
				$("#c"+id).val(val);
				$("#bsadd_"+varid).hide();
				$("#bs_"+varid).show();
			};
		};

		var bask = $("#bask").val();

		if(bask == "ok"){
			pereschet();
		};

	});

	$(".del").click(function(){
	
		var id = $(this).attr("id");
		var varid = $(this).attr("name");

		$("#block" + id).remove();

		var ids = $('#tbl_'+varid+' .inpr');

		if(ids.length == 0){
			$('#ctone'+varid).remove();
		};

		pereschet();

	});


});

function basket(id){

	var ids = $('#tbl_'+id+' .inpr');

	var elem = new Array();
	var colvo = new Array();
	var q = 0;
	$.each( ids, function(i, n){
		if($(this).val() != 0 && $(this).val() != 0){
			colvo[q] = $(this).val();
			elem[q] = $(this).attr('id');
			q++;
		};
	});

	$.ajax({
		type: 'post',
		url: '/basket/update/',
		data: 'id='+elem+'&colvo='+colvo,
		success: function(html){
			$('#basketdiv').empty();
			$('#basketdiv').append(html);
			$('#bs_'+id).hide();
			$('#bsadd_'+id).show();
		}
	});
};

function pereschet(){

	var ids = $('.ctone .inpr');

	var elem = new Array();
	var colvo = new Array();
	var q = 0;
	$.each( ids, function(i, n){
		if($(this).val() != 0 && $(this).val() != 0){
			colvo[q] = $(this).val();
			elem[q] = $(this).attr('id');
			q++;
		};
	});

	$.ajax({
		type: 'post',
		url: '/basket/pereschet/',
		data: 'id='+elem+'&colvo='+colvo+'&pereschet=1',
		success: function(html){
			$('#basketdiv').empty();
			$('#basketdiv').append(html);
		
			$.ajax({
				type: 'post',
				url: '/basket/itogo/',
				success: function(html){
					$('#itogholder').empty();
					$('#itogholder').append(html);
				}
			});
		
		}
	});

};
