// JavaScript Document
$(function() {
		   
	$('#update').click(function() {
								window.location.reload(true);
								});
	$('#empty_cart').click(function() {
									
			if (confirm('Är du säker på att du vill tömma varukorgen?')) {
			$.ajax({

            url: '/includes/cart.php',
			type: "POST",
			data: "action=empty",
            timeout: 2000,
            error: function() {
              
            },
            success: function(r) { 
             window.location.reload(true);
            }
          });  							
		} 
		else {
			return false;
		}
									
	});
	
	$("#cart_table td.remove a ").click(function() {
		var orderCode = $(this).next('input').val();
		$.ajax({
			type: "POST",
			url: "/includes/cart.php",
			data: "action=remove&remove[]=" + orderCode,
			success: function() {
				$("#cart_table tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					//calcPrice();
						$.ajax({

            url: '/includes/cart_top.php',
            timeout: 2000,
            error: function() {
              
            },
            success: function(r) { 
              $('#cart span').html(r);
            }
          });  
				});
			},
			error: function() {
				//window.location("/includes/cart/cart_action.php?remove[]="+orderCode);
			}
		});
	});
	
	$("#cart_table tr input.quantity").change(function() {
		var orderCode = $(this).attr("name").slice(9, -1);
		var quantity = $(this).val();
		
		$.ajax({
			type: "POST",
			url: "/includes/cart.php",
			data: "action=quantity&quantity[" + orderCode + "]=" + quantity,
			success: function() {
				var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				$("#cart_table td.quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
			//	calcPrice();
		
			$.ajax({

            url: '/includes/cart_top.php',
            timeout: 2000,
            error: function() {
              
            },
            success: function(r) { 
              $('#cart span').html(r);
            }
          });  
			},
			error: function() {
				//window.location("/studio/extern_files/cart_action.php?quantity[" + orderCode + "]=" + quantity);
			}
		});
	});
});

function calcPrice() {
		$.ajax({
			type: "POST",
			data: "action=getTotal",
            url: '/includes/cart.php',
            timeout: 2000,
            error: function() {
              
            },
            success: function(r) { 
              $('#total_price').html(r);
            }
          });  
		
			$.ajax({
			type: "POST",
			data: "action=getTotalTax",
            url: '/includes/cart.php',
            timeout: 2000,
            error: function() {
              
            },
            success: function(r) { 
              $('#total_tax').html(r);
            }
          });  

}