(function($,undefined){
		  
	// The init function
	$.fn.basketOverview = function(){
		
		var $wrapper = $(this);
		$.basketOverview.updateBasket();
		
		var config = {
			interval: 100,
			sensitivity: 4,
			timeout: 500,			
			over: function()
			{
				// Only hover if there are items inside the area
				if($wrapper.find('.item').length > 0)
				{											
					$.basketOverview.show(function(){
						$wrapper.css({'overflow':'visible'});
					});
				};
			},
			out:  function()
			{
				$wrapper.css({'overflow':'hidden'});
				$.basketOverview.hide();
			}
		};		
		
		$wrapper.hoverIntent(config);

	};
	
	// The object
	$.basketOverview = {
		/**
		 * Hide
		 * @param	function	Callback to execute when fully hidden
		 */
		'hide' 	: function(callback)
		{
			$('#basket_items').slideUp(function(){
				(callback || $.noop)();									
			});	
		},
		
		/**
		 * Show
		 * @param	function	Callback to execute when fully shown				 
		 */
		'show' 	: function(callback)
		{
			$('#basket_items').slideDown(function(){
				(callback || $.noop)();									
			});
		},
		
		/**
		 * Update Basket
		 * @param	function	Callback to execute when fully loaded
		 */
		'updateBasket' : function(callback)
		{
			var protocol = (document.location.protocol === 'https:') ? 'https://' : 'http://';			
			var time = new Date().getTime();
			
			$.getJSON(window.site.url + '/shopping_basket/json/basket-preview.json.php?r='+time,{},function(response){
				
				// Populate all the divs
				$('#bp_items').text(response.total_items);
				$('#bp_subtotal').text(response.subtotal);	
				
				// Change the wording of 'Items' to 'Item' if item count is 1
				(response.total_items == 1)
					? $('#plural_check').text('Item')
					: $('#plural_check').text('Items');
							
				$('#bp_delivery').html('<span class="float_left">' + response.delivery_type + '</span>' + ' <span class="float_right">&pound;' + response.delivery + '</span>');
				$('#bp_total span:last-child').html("&pound;" + response.order_total);
				$('#basket_items .item').remove();
				
				$.each(response.products,function(hash, product){

					$('#basket_items .no_items').hide();
					
					// Set item to be the response template
					var item = response.template;
					
					var replacements = {
						'hash'		:	hash,
						'id'		:	product.id,
						'image'		:	product.image,
						'title'		:	product.title,
						'size'		:	product.size,							
						'quantity'	:	product.quantity,
						'discounts'	:	product.discounts,
						'price'		:	product.price								
					};

					for(var key in replacements)
						item = item.replace(new RegExp('{{'+key+'}}','gi'), replacements[key]);
					
					var $inserted_item = $(item).prependTo('#basket_items');

					// Hide the discounts item if there are none
					if( parseFloat(product.discounts) <= 0 )
						$inserted_item.find('.savings').hide();	
					
					// Add click function to the items
					$inserted_item.click(function(){
						window.location.href = $(this).find('h4 a').attr('href');
					});
				});
				
				(callback || $.noop)();	
			});	
		}
	};

})(jQuery);
