var WebshopCart = {};
WebshopCart.anchorElem;
WebshopCart.productId;
WebshopCart.isAvailable = true;
WebshopCart.origElemInnerHTML;

WebshopCart.addProduct = function(elem, productId, categoryId)
{
	// Make sure this function is "not in use"
	if (!this.isAvailable) {
		return false;
	}

	// Category ID is optional
	if (Object.isUndefined(categoryId)) {
		var categoryId = '';
	}

	// Make sure the element is a Prototype element
	elem = $(elem);

	// Check for available quantity field
	if (!elem.previous('input.product-quantity') || Object.isUndefined(elem.previous('input.product-quantity').value)) {
		return false;
	}

	// Set to "in use"
	this.isAvailable = false;

	var quantity = elem.previous('input.product-quantity').value;
	quantity = parseFloat( quantity.replace(',', '.') );
	if (Object.isNumber(quantity) && !isNaN(quantity)) {
		quantity = quantity.round().abs();
	} else {
		quantity = 1;
	}

	var url = PbLib.getNewURI('l/webshop2/shoppingcart/add/' + productId + '/' + quantity + '?c=' + categoryId);
	this.anchorElem = elem;
	this.productId = productId;
	this.origElemInnerHTML = elem.innerHTML;

	var loaderImage = new Element('img', { 'src': PbLib.getNewURI('files/mod_webshop2/img/shopping-cart-loader.gif') });
	elem.update(loaderImage);
	new Ajax.Request(url, {
			'onSuccess': WebshopCart.addSuccess.bindAsEventListener(this),
			'onFailure': WebshopCart.addFailed.bindAsEventListener(this)
		});
}

WebshopCart.addFailed = function(transport)
{
	// Reset the element and show a dialog with an error
	this.anchorElem.innerHTML = this.origElemInnerHTML;
	var dialogElem = window.top.PbLib.createDialog(false, 400, 75);
	dialogElem.update("<div style='text-align: center;'>" +
		"<div>" + transport.responseJSON.msg + "</div>" +
		"<div><a href='#' onclick='Event.stop(event); window.top.PbLib.destroyDialog();'>" + PbLib.g('Close') + "</a></div>" +
		"</div>");

	// Set back to "not in use"
	this.isAvailable = true;
}

WebshopCart.addSuccess = function(transport)
{
	// Check the outcome of the transport
	if (Object.isUndefined(transport.responseText) || !transport.responseText.match(/^ok$/i)) {
		this.addFailed(transport);
		return false;
	}

	// Reset the element and show an indication that it was successful
	var checkImage = new Element('img', { 'src': PbLib.getNewURI('files/mod_webshop2/img/shopping-cart-check.png') });
	this.anchorElem.update(checkImage);

	var elem = this.anchorElem;
	var elemInnerHTML = this.origElemInnerHTML;
	(function (elem, elemInnerHTML) {(function () {elem.innerHTML = elemInnerHTML}).delay(2);})(elem, elemInnerHTML);

	// Add class 'product-in-shopping-cart'
	if (elem.up(1).down('span.add-to-shopping-cart')) {
		elem.up(1).down('span.add-to-shopping-cart').addClassName('product-in-shopping-cart');
	}

	// Set back to "not in use"
	this.isAvailable = true;

	// Reload the quick-view shopping cart
	if ($('webshop-shopping-cart-snippet')) {
		var siteId = 0;
		var header = $('webshop-shopping-cart-snippet').down('h3');
		if (!Object.isUndefined(header.id)) {
			siteId = header.id.gsub('site-', '');
		}

		var url = PbLib.getNewURI('l/webshop2/shoppingcart/reload/' + this.productId);
		if (siteId > 0) {
			url += '/' + siteId;
		}

		new Ajax.Request(url, {
				'onSuccess': function (transport) {
					$('webshop-shopping-cart-snippet').innerHTML = transport.responseText;
				}
			});
	}
}