function shopAjax() {
	this.cantAjax = "noajax";
	this.AjaxError = "error";
	this.cookiePath = "";	// Path to use for cookies
	this.root = "";			// Root of the website
	this.ajaxBuyNow = true;
	this.ajaxWishlist = true;
	this.buyNowSelector = ".buyNow a";
	this.wishlistSelector = ".actions .save a";
	this.shortBasket = "#shortBasket";
}

shopAjax.prototype.init = function() {
	if (this.ajaxBuyNow)
		this.addBasketLinks();
	if (this.ajaxWishlist)
		this.addWishlistLinks();
}

/****************************************************************************
 * STANDARD METHODS
 *****************************************************************************/
/**
 *	Loads the xml response of the call
 **/
shopAjax.prototype.loadResponse = function(data) {
	var xmlDoc = $.parseXML(data);
	return xmlDoc;
}

/**
 *	Checks whether the Ajax response is valid, if Ajax is not allowed for the operation 
 *	the original request is processed to gift user feedback
 **/
shopAjax.prototype.checkAjax = function(link, xml) {
	// Can't process item using AJAX, so redirect
	if (xml.documentElement.nodeName == this.cantAjax)
	{
		document.location = link;
		return false;
	}
	return true;
}

/**
 *	Checks whether the Ajax response failed
 **/
shopAjax.prototype.checkAjaxError = function(xml) {
	return xml.documentElement.nodeName == this.AjaxError;
}

/**
 *	Stores the returned session id to maintain a newly created basket
 **/
shopAjax.prototype.storeSessionCookie = function(xml) {
	var session = $(xml).find("sessionId");
	$.cookie("tSOToolkit", "Token=" + session.text(), { path: this.cookiePath, raw: true });
}

/**
 *	Replaces the content of the specified element with the response from the ajax call
 **/
shopAjax.prototype.replaceContent = function(element, xml) {
	var content = $(xml).find("content");
	$(element).html(content.text());
}

/****************************************************************************
 * ADD TO BASKET AJAX CALL
 *****************************************************************************/
/**
 *	Default message for feedback when an item is added to the basket.
 **/
shopAjax.prototype.addToBasketSuccess = function() {
	tb_show('Item added to basket', this.root + "html/BASKET-ADDED.HTML?TB_iframe=true&height=60&width=250");
}

/**
 *	Default message for feedback when an item can't be added to the basket.
 **/
shopAjax.prototype.addToBasketFailure = function(link) {
	document.location = link;
}

/**
 *	Default action when an item is added to the basket
 **/
shopAjax.prototype.addToBasketDisplay = function(control, xml) {
	this.replaceContent(this.shortBasket, xml);
}

/**
 *	Intercepts the buy now links to add an ajax call
 **/
shopAjax.prototype.addBasketLinks = function () {
	var container = this;
	$(this.buyNowSelector).each(function() {
		$(this).bind('click', function() {
			container.addToBasket($(this));
			return false;
		});
	});
}

/**
 *	Attempts to add the specified item to the basket without going to the full display
 **/
shopAjax.prototype.addToBasket = function (control) {
	var link = $(control).attr('href')
	var qs = link.substring(link.indexOf("?") + 1);
	var container = this;
	$.ajax({
		type: "GET",
		url: this.root + "ajax.asp",
		data: qs,
		success: function(data)
		{
			container.addToBasketResponse(control, link, data);
		}
	});
}

/**
 *	The response for an add to basket request
 **/
shopAjax.prototype.addToBasketResponse = function(control, link, data) {
	var xml = this.loadResponse(data);
	if (!this.checkAjax(link, xml))
		return;
	if (this.checkAjaxError(xml))
	{
		this.addToBasketFailure(link);
		return;
	}
	
	// Item added to the basket, extract new session detail
	this.storeSessionCookie(xml);
		
	// Update basket display
	this.addToBasketDisplay(control, xml);
	this.addToBasketSuccess();
}

/****************************************************************************
 * ADD TO WISHLIST AJAX CALL
 *****************************************************************************/
/**
 *	Default message for feedback when an item is added to the Wishlist.
 **/
shopAjax.prototype.addToWishlistSuccess = function() {
	tb_show('Item saved', this.root + "html/WISHLIST-ADDED.HTML?TB_iframe=true&height=70&width=250");
}

/**
 *	Default message for feedback when an item can't be added to the Wishlist
 **/
shopAjax.prototype.addToWishlistFailure = function(link) {
	document.location = link;
}

/**
 *	Default action when an item is added to the basket
 **/
shopAjax.prototype.addToWishlistDisplay = function(control, xml) {
}

/**
 *	Intercepts the save links to add an ajax call
 **/
shopAjax.prototype.addWishlistLinks = function () {
	var container = this;
	$(this.wishlistSelector).each(function() {
		$(this).bind('click', function() {
			container.addToWishlist($(this));
			return false;
		});
	});
}

/**
 *	Attempts to add the specified item to the wishlist without going to the full display
 **/
shopAjax.prototype.addToWishlist = function (control) {
	var link = $(control).attr('href')
	var qs = link.substring(link.indexOf("?") + 1);
	var container = this;
	$.ajax({
		type: "GET",
		url: this.root + "ajax.asp",
		data: qs,
		success: function(data)
		{
			container.addToWishlistResponse(control, link, data);
		}
	});
}

/**
 *	The response for an add to wishlist request
 **/
shopAjax.prototype.addToWishlistResponse = function(control, link, data) {
	var xml = this.loadResponse(data);
	if (!this.checkAjax(link, xml))
		return;
	if (this.checkAjaxError(xml))
	{
		this.addToWishlistFailure(link);
		return;
	}
	
	// Give some feedback
	this.addToWishlistDisplay(control, xml);
	this.addToWishlistSuccess();
}

var interfaceShopAjax = new shopAjax();
$(document).ready(function() {
	interfaceShopAjax.init();
});

