function KailisShoppingCart()
{
	var shoppingCart = {
		items: new Array(),
		products: new Array(),
		listTimeout: null,
		buttonTimeout: null,

		initialise: function()
		{
			this.updateCart();

			$(document).ready(function()
				{
					$('.addToCart').click(function(e)
						{
							var productId = $(e.target).attr('class').replace(/.*product_(\d+).*/, "$1");
							shoppingCart.add(productId);
						});

					$('.kailisShoppingCartImage').click(function(e)
						{
							shoppingCart.displayItemList(e);
						});

					$('.sizingChart').click(function(e)
						{
							e.preventDefault();

							var chartId = $(e.target).attr('class').replace(/.*chart_(\d+).*/, "$1");
							window.open('/jewellery/size-chart/' + chartId, 'sizeChart', 'menubar=0,location=0,toolbar=0,status=0,scrollbars=1,resizeable=1,width=750px,height=750px');
						});
				});

			setInterval('sc.updateCart();', 60000);
		},

		updateCart: function()
		{
			$.post("/en/jewellery/cart/ajax",
				{
					action: 'list'
				},
				function(data)
				{
					if( true == data.status )
					{
						shoppingCart.items = data.payload;
						shoppingCart.displayItemCount();
					}
					else
					{
						shoppingCart.popupMessage(data.message);
					}
				},
				'json');
		},

		add: function(productId)
		{
			var sizeId = null;

			if( $("#addToCartSize").length > 0 )
			{
				if( $("#addToCartSize").val() > 0 )
				{
					sizeId = $("#addToCartSize").val();
				}
				else
				{
					shoppingCart.popupMessage('Please select a size before adding to basket.');
					return;
				}
			}

			var payload = {
					action: 'add',
					productId: productId
				};
			if( sizeId != null )
			{
				payload.sizeId = sizeId;
			}

			$.post("/en/jewellery/cart/ajax",
				payload,
				function(data)
				{
					if( true == data.status )
					{
						shoppingCart.items = data.payload;
						shoppingCart.displayItemCount();
						shoppingCart.popupMessage(data.message);
						//$('.kailisShoppingCartImage').click();
						//setTimeout('sc.hideItemList();', 3000);
					}
					else
					{
						shoppingCart.popupMessage(data.message);
					}
				},
				'json');
		},

		popupMessage: function(message)
		{
			if( $('#sizeErrorMessage').length == 0 )
			{
				$('body').append('<div id="sizeErrorMessage"><p></p><input type="image" src="/images/shopping-cart/btn-close.gif" id="closeSizeErrorMessage" alt="CLOSE" class="close" width="64" height="19"/></div>');
			}
			$('#sizeErrorMessage p').text(message);

					$("#sizeErrorMessage").overlay({
							expose: {
								color: '#000000',
								loadSpeed: 200,
								opacity: 0.5
							},
							closeOnClick: false,
							api: true,
							top: '30%'
						}).load();
		},

		remove: function(productId)
		{
			$.post("/en/jewellery/cart/ajax",
				{
					action: 'remove',
					productId: productId
				},
				function(data)
				{
					if( true == data.status )
					{
						shoppingCart.items = data.payload;
						shoppingCart.displayItemCount();
					}
					else
					{
						shoppingCart.popupMessage(data.message);
					}
				},
				'json');
		},

		clear: function()
		{
			$.post("/en/jewellery/cart/ajax",
				{
					action: 'clear'
				},
				function(data)
				{
					if( true == data.status )
					{
						shoppingCart.items = data.payload;
						shoppingCart.displayItemCount();
					}
					else
					{
						shoppingCart.popupMessage(data.message);
					}
				},
				'json');
		},

		displayItemCount: function()
		{
			var itemCount = this.items.length;

			if( 0 == itemCount )
			{
				$('.kailisShoppingCartItemCount').text('');
			}
			else
			{
				var itemTotal = 0;

				for( var i = 0; i < this.items.length; i++ )
				{
					itemTotal += this.items[i].quantity;
				}
				$('.kailisShoppingCartItemCount').text(itemTotal + ' items in basket ');
			}
		},

		displayItemList: function(e)
		{
			$target = $(e.target);
			$header = $(".kailisHeader");

			var newTop = $target.offset().top + $target.height() - $header.offset().top + 1;
			var newLeft = $target.offset().left - $header.offset().left + 1;

			if( $('.kailisHeader .kailisShoppingCartList').length > 0 )
			{
				this.hideItemList();
				return;
			}

			var html = '<div class="kailisShoppingCartList" style="display: none;"><ul>';
			if( shoppingCart.items.length == 0 )
			{
				html += '<li class="ui-helper-clearfix noItems">No items in basket</li>';
			}
			else
			{
				for( var i = 0; i < shoppingCart.items.length; i++ )
				{
					var item = shoppingCart.items[i];

					html += '<li class="ui-helper-clearfix"><a href="' + item.product.url + '"><img src="' + item.product.smallThumbnail + '" width="91" height="61" /></a><div>';
					if( item.quantity > 1 )
					{
						html += item.quantity + ' x ';
					}
					html += '<a href="' + item.product.url + '">' + item.product.name + '</a><br/>$' + item.price + '</div></li>';
				}
				html += '<li class="ui-helper-clearfix controls"><a href="/jewellery/cart"><img src="/images/shopping-cart/btn-checkout.gif" alt="CHECKOUT" height="19" width="97" /></a></li>';
			}
			html += '</ul></div>';
			$('.kailisHeader').append(html);
			$('.kailisHeader .kailisShoppingCartList').css('top', newTop + 'px')
				.css('left', newLeft + 'px')
				.mouseenter(function(e)
				{
					clearTimeout(shoppingCart.listTimeout);
				})
				.mouseleave(function(e)
				{
					shoppingCart.listTimeout = setTimeout('sc.hideItemList()', 1000);
				})
				.slideDown('normal', function(e)
					{
						clearTimeout(shoppingCart.listTimeout);
						shoppingCart.listTimeout = setTimeout('sc.hideItemList()', 1000);
					});
		},

		hideItemList: function()
		{
			clearTimeout(shoppingCart.listTimeout);
			clearTimeout(shoppingCart.buttonTimeout);
			$('.kailisHeader .kailisShoppingCartList').slideUp('slow',
					function()
					{
						$(this).remove();
					});
		}
	};

	shoppingCart.initialise();

	return shoppingCart;
}

var sc = new KailisShoppingCart();
