$(document).ready(function()
{
	$(".module .modTitle h1").prepend("&raquo; ");
	
	if($("#shoppingCartModule").length > 0)
	{
		$("#shoppingCartModule .removeButton").click(function()
		{
			var productSN = $(this).parent().attr("id").substring(8);
			$.ajax(
			{
				type: "POST",
				url: "/transaction/remove_from_cart.jsp",
				data: "productSN="+encodeURIComponent(productSN),
				success: function(cartModuleHTML)
				{
					if(cartModuleHTML.substring(0,5)!="<ul/>")
					{
						$("#shoppingCartModule .modBody").empty();
						$("#shoppingCartModule .modBody").append(cartModuleHTML);
						$("#shoppingCartModule #clearCart").click(function()
						{
							$.ajax(
							{
								type: "GET",
								url: "/transaction/clear_all.jsp",
								success: function()
								{
									$("#shoppingCartModule").remove();
								}
							});
						});
						$("#shoppingCartModule #checkout").click(function(event)
						{
							event.preventDefault();
							window.location = "https://"+window.location.hostname+"/checkout.jsp";
						});
						$("#shoppingCartModule .removeButton").click(function()
						{
							var productSN = $(this).parent().attr("id").substring(8);
							$.ajax(
							{
								type: "POST",
								url: "/transaction/remove_from_cart.jsp",
								data: "productSN="+encodeURIComponent(productSN),
								success: function(cartModuleHTML)
								{
									if(cartModuleHTML.substring(0,5)!="<ul/>")
									{
										$("#shoppingCartModule .modBody").empty();
										$("#shoppingCartModule .modBody").append(cartModuleHTML);
									}
									else
									{
										$("#shoppingCartModule").remove();
									}
								}
							});
						});
					}
					else
					{
						$("#shoppingCartModule").remove();
						$("#checkoutLink").remove();
					}
				}
			});
		});
		$("#shoppingCartModule #clearCart").click(function()
		{
			$.ajax(
			{
				type: "GET",
				url: "/transaction/clear_all.jsp",
				success: function()
				{
					$("#shoppingCartModule").remove();
					$("#checkoutLink").remove();
				}
			});
		});
		$("#shoppingCartModule #checkout").click(function(event)
		{
			event.preventDefault();
			window.location = "https://"+window.location.hostname+"/checkout.jsp";
		});
	}
});