// JavaScript Document


(function($) {
		  
	// Declare our class
	$.BasketSummaryClass = function ( )
	{	// This is the handler for our constructor
		this.construct();
	};
	
		// Extend jQuery elements for Lightbox
	$.fn.basketSummary = function ( options )
	{	// Init a el for Lightbox
		// Eg. $('#gallery a').lightbox();
		
		// If need be: Instantiate $.LightboxClass to $.Lightbox
		$.BasketSummary = $.BasketSummary || new $.BasketSummaryClass();
		
		// Establish options
		$.BasketSummary.options = $.extend({
											url:"/cgi-bin/basket_summary.pl",
											compareUrl:"/cgi-bin/compare.pl"
										   }, options);
		
		$.BasketSummary.data = {};
		
		// Get group
		$.BasketSummary.el = this;
		$.BasketSummary.elComp = $("#compareSummary");
		
		$.BasketSummary.getJSON();
		
		// And chain
		return this;
	};
	
	
	$.fn.basketSummary.update = function ( data )
	{	
		$.BasketSummary.update(data);
	};
	
	

	
	// Define our class
	$.extend($.BasketSummaryClass.prototype,
	{	// Our LightboxClass definition
	
		getJSON: function()
		{
			$.getJSON(this.options.url + "?d=" + (new Date()).getTime() + Math.random(), this.recieveJSON);
		},
	
		recieveJSON: function(data) 
		{
			$.BasketSummary.data = data;
			$.BasketSummary.updateDisplay(0);
			
		},
		
		productIds: function (offerType) 
		{
			return $.BasketSummary.data.productIds[offerType];
		},
		
		compareProductIds: function () 
		{
			return $.BasketSummary.data.productCompareIds;
		},
		
		
		clearComparison: function ( )
		{	
			if (confirm("Are you sure you want to clear the products that you have selected for comparison?")) {
				this.elComp.empty();
				this.elComp.append("<img src='/images/working.gif'>");
				$('.product .compare').attr("checked", false);
				
				
				$.get(
						this.options.compareUrl + "?d=" + (new Date()).getTime() + Math.random() + "&com=clear",
						$.BasketSummary.update()
				);
			}
			
		},
		
		update: function(data) 
		{
			if (!data) {
				this.getJSON();
			} else {
				this.data = data;
				this.updateDisplay(1000);
			}
		},

		
		containsProductId: function(productId, offerType)
		{
	
	
			return ($.inArray(productId.toString(), $.BasketSummary.productIds(offerType)) > -1);
		},
		
		compareContainsProductId: function(productId)
		{
			return ($.inArray(productId.toString(), $.BasketSummary.compareProductIds()) > -1);
		},
	
		updateDisplay: function(delay)
		{
			
			var offerType =
								$("#offerType").length
								? ($("#offerType").val().replace(/[^0-9]/g, "") || 0) - 0
								: 0;	// we will only find this (hopefully!) on product details pages
			var currentProductId = ($(".productRef").text().replace(/[^0-9]/g, "") || 0) -0;	// we will only find this (hopefully!) on product details pages
		
		
			if ((currentProductId > 0) && $.BasketSummary.containsProductId(currentProductId, offerType)) {
				
				$(".addToBasket").empty();
				$(".addToBasket").append("This product is in <a href='/cgi-bin/basket.pl'>your basket</a>.");
			}
			
			if ((currentProductId > 0) && $.BasketSummary.compareContainsProductId(currentProductId)) {
				
				$(".addToComparison").empty();
				$(".addToComparison").append("This product is already in <a href='/cgi-bin/summary.pl'>your product comparison</a>.");
			}
			
		
			this.el.empty();
			this.el.append("Basket summary: <strong>" + ($.BasketSummary.data.productCount || 0) + " item" + ($.BasketSummary.data.productCount > 1 ? 's' : '') + "</strong>. Total <strong>£" + ($.BasketSummary.data.totalGross || 0) + "</strong><br>Delivery: <strong>&pound;" + $.BasketSummary.data.deliveryGross  + "</strong><div id='basketSummaryLinks'><a href='/cgi-bin/basket.pl'>View basket</a></div>");
			if ($.BasketSummary.data.productCount > 0) {
				this.el.show(delay || 0);
			} else {
				this.el.hide(delay || 0);
			}
			
			
			// show the product compare summary if there is one...
			if ($.BasketSummary.data.productCompareCount > 0) {
		
				var clearLink = $("<a href='#'>Clear comparisons</a>");
				clearLink.bind("click", function() {$.BasketSummary.clearComparison(); return false;});
				var viewLink = $("<a href='/cgi-bin/compare.pl'>Compare items</a>");
				var linksDiv = $("<div id='compareSummaryLinks'></div>").append(clearLink).append(" | ").append(viewLink);
				
				this.elComp.empty();
				
				this.elComp.append("Comparison summary: <strong>" + ($.BasketSummary.data.productCompareCount || 0) + " item" + ($.BasketSummary.data.productCompareCount > 1 ? 's' : '') + "</strong> to compare.<br></strong>");
				this.elComp.append(linksDiv);
				
				this.elComp.show(delay || 0);
				
				
				// if we already have products to compare - tick their boxes
				$($.BasketSummary.data.productCompareIds).each(function() {
					$(".product .compare[rel=" + this + "]").attr("checked", "true");
				});
			} else {
				this.elComp.hide(delay || 0);
			}

		},
		
		construct: function()
		{	// Construct our Lightbox
			
			// Set baseurl
		//	this.baseurl = $('script[src$='+this.files.js.lightbox+']').attr('src');
		//	this.baseurl = this.baseurl.substring(0, this.baseurl.length-this.files.js.lightbox.length);
			
			// Apply baseurl to files
		//	var me = this;
		//	$.each(this.files, function(group, val){
		//		$.each(this, function(file, val){
		//			me.files[group][file] = me.baseurl+val;
		//		});
		//	});
			
			// All good
		//	return true;
		},
		
		domReady: function ( )
		{
	
			// do stuff here to initiate the page in someway if you need to.
			
		}
		
		
	
	});
	
	
	
		// --------------------------------------------------
	// Finish up
	
	// On document load, Instantiate our class
	$(function() {
		// Instantiate
		$.BasketSummary = $.BasketSummary || new $.BasketSummaryClass();
		
		// domReady
		$.BasketSummary.domReady();
	});
	
	
})(jQuery);