$(document).ready(function() {
	
	$("#valueTotal").text(0);
	$(".amount").text(0);
	$(".sub-total").text(0);
	$("#orderForm .amountField").val(0);
	
	$(".buttonProceedToCheckout").click(function() {
		if ($("#valueTotal").text() == "0") {
			window.alert('Vyberte alespoň jeden produkt');
			return;
		}
		if ($("#confirm:checked").val() != "OK") {
			window.alert('Musíte souhlasit s obchodními podmínkami');
			return;
		}
		$('#orderForm').submit();
	});
	
	$(".buttonOrderMovies").click(function() {
		$('#checkoutForm').submit();
	});
	
	$(".less").click(function() {
		   	// $(this).parents("tr.product_line").find(".unit-price").hide();
			
			var price = parseFloat($(this).parents("tr.product_line").find(".unit-price").text());
			var count = parseFloat($(this).parents("tr.product_line").find(".amount").text());
			var sum = parseFloat($(this).parents("tr.product_line").find(".sub-total").text());
			var total = parseFloat($("#valueTotal").text());
			
		
			//window.alert("price> " + price + " ")
			if (count < 1) { 
				return;
				}
			
			if (count == 1) { 
				$(this).removeClass("buttonLess");
				$(this).addClass("buttonLess_disable");
				}
			
			if (count == 100) { 
				$(this).parents("tr.product_line").find(".more").addClass("buttonMore");
				$(this).parents("tr.product_line").find(".more").removeClass("buttonMore_disable");
				}

			
			$(this).parents("tr.product_line").find(".amount").text(--count);
			$(this).parents("tr.product_line").find(".amountField").val(count);
			$(this).parents("tr.product_line").find(".sub-total").text(sum - price);
			$("#valueTotal").text(total - price);

	});
	
	$(".more").click(function() {
	   	// $(this).parents("tr.product_line").find(".unit-price").hide();
		
		var price = parseFloat($(this).parents("tr.product_line").find(".unit-price").text());
		var count = parseFloat($(this).parents("tr.product_line").find(".amount").text());
		var sum = parseFloat($(this).parents("tr.product_line").find(".sub-total").text());
		var total = parseFloat($("#valueTotal").text());
		
		
		if (count > 99) { return; }	
		
		if (count == 99) { 
			$(this).removeClass("buttonMore");
			$(this).addClass("buttonMore_disable");
			}
		
		if (count == 0) {
			$(this).parents("tr.product_line").find(".less").addClass("buttonLess");
			$(this).parents("tr.product_line").find(".less").removeClass("buttonLess_disable");
			}
		
		$(this).parents("tr.product_line").find(".amount").text(++count);
		$(this).parents("tr.product_line").find(".amountField").val(count);
		$(this).parents("tr.product_line").find(".sub-total").text(sum + price);
		$("#valueTotal").text(total + price);
		
	});
});
