$(document).ready(function() {
	
	
	
	
	
	$(".articleposdelete").click(function() {
		
		var self = this;
		
		posId = $(this).parents("tr:first").find(".pId").val();
		posCount = $(this).parents("tr:first").find(".pCount").val();
		posSumPreis = Number($(this).parents("tr:first").find(".pSumPreis").html());
		tPreis = Number($("#totalPreis").html());
		
		totalPreisNew = tPreis - posSumPreis;
		totalPreisNew=totalPreisNew.toFixed(2);
		
		bodyContent = $.ajax({
			  url: "/cart_ajax.html",
			  //global: false,
			  type: "GET",
			  data: ({posId : posId, posCount : -posCount}),
			  dataType: "html",
			  async:false,
			  success: function(msg){
				 //alert(msg);
				 if (msg != "false") {
					 $("#cart").html(msg);
					 
					 $(self).parents("tr:first").hide();
					 
					 $("#totalPreis").html(totalPreisNew);
					 
				 }
			  }
		   }
		).responseText;
		return false;
	});
	
	
	
	$(".articleposincrement").click(function() {
		
		var self = this;
		
		posId = $(this).parents("tr:first").find(".pId").val();
		posCount = Number($(this).parents("tr:first").find(".pCount").val())+1;
		posPreis = Number($(this).parents("tr:first").find(".pPreis").html());
		posSumPreis = Number($(this).parents("tr:first").find(".pSumPreis").html());

		tPreis = Number($("#totalPreis").html());
		
		posSumPreisNew = Number(posSumPreis + posPreis);
		totalPreisNew = Number(tPreis + posPreis);

		posSumPreisNew=posSumPreisNew;
		totalPreisNew=totalPreisNew.toFixed(2);
		
		
		
		bodyContent = $.ajax({
			  url: "/cart_ajax.html",
			  //global: false,
			  type: "GET",
			  data: ({posId : posId, posCount : 1}),
			  dataType: "html",
			  async:false,
			  success: function(msg){
				 //alert(msg);
				 if (msg != "false") {
					 $("#cart").html(msg);
					 
					 $(self).parents("tr:first").find(".pCount").val(posCount);
					 $(self).parents("tr:first").find(".posCount").html(posCount);
					 $(self).parents("tr:first").find(".pSumPreis").html(posSumPreisNew);
					 $("#totalPreis").html(totalPreisNew);
					 
				 }
			  }
		   }
		).responseText;
		return false;
	});
	
	
	
	$(".articleposdecrement").click(function() {
		
		var self = this;
		
		posId = $(this).parents("tr:first").find(".pId").val();
		posCount = Number($(this).parents("tr:first").find(".pCount").val())-1;
		if (posCount > 0) {
			posPreis = Number($(this).parents("tr:first").find(".pPreis").html());
			posSumPreis = Number($(this).parents("tr:first").find(".pSumPreis").html());
			tPreis = Number($("#totalPreis").html());
			
			posSumPreisNew = posSumPreis - posPreis;
			totalPreisNew = tPreis - posPreis;
	
			posSumPreisNew=posSumPreisNew.toFixed(2);
			totalPreisNew=totalPreisNew.toFixed(2);
			
			bodyContent = $.ajax({
				  url: "/cart_ajax.html",
				  //global: false,
				  type: "GET",
				  data: ({posId : posId, posCount : -1}),
				  dataType: "html",
				  async:false,
				  success: function(msg){
					 //alert(msg);
					 if (msg != "false") {
						 $("#cart").html(msg);
						 
						 if (posCount > 0) {
							 $(self).parents("tr:first").find(".pCount").val(posCount);
							 $(self).parents("tr:first").find(".posCount").html(posCount);
							 
							 $(self).parents("tr:first").find(".pSumPreis").html(posSumPreisNew);
							 
						 }
						 else {
							 $(self).parents("tr:first").hide();
						 }
						 
						 $("#totalPreis").html(totalPreisNew);
						 
					 }
				  }
			   }
			).responseText;
		}
		return false;
	});
	
	
	$("#lulu").bind("keyup", function() { 
		$('#lulu2').val($(this).val()); 

	});
	
	
	
	
	
	
	
	
});



