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.selectProductVariant = function(elem, productId, categoryId)
{
	// Make sure this function is "not in use"
	if (!this.isAvailable) {
		return;
	}

	// 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;
	}

	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;
	}

	this.anchorElem = elem;
	this.origElemInnerHTML = elem.innerHTML;

	PbLib.createDialog(PbLib.getNewURI('k/webshop2/product/' + productId + '/selectvariant?quantity=' + quantity), 600, 'fit', {
		enableMaximize: false,
		enableResize: false,
		enableDragging: false,
		shadeWindow: false
	});
}

WebshopCart.addProductVariant = function (productId, quantity) {

	var url = PbLib.getNewURI('l/webshop2/shoppingcart/add/' + productId + '/' + quantity);

	this.productId = productId;

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


	PbLib.destroyDialog();
}

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
	this.reloadShoppingCartSnippet(this.productId);
}

WebshopCart.reloadShoppingCartSnippet = function (productId)
{
	var snippet = $('webshop-shopping-cart-snippet');

	if (snippet) {

		var siteId = 0;
		var header = snippet.down('h3');
		if (!Object.isUndefined(header.id)) {
			siteId = header.id.gsub('site-', '');
		}

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

		new Ajax.Request(url, {
			'onSuccess': function (response) {
				snippet.update(response.responseText);
			}
		});
	}
}

WebshopCart.addProductSingle = function(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 = '';
	}

	var url = PbLib.getNewURI('l/webshop2/shoppingcart/add/' + productId + '/1' + '?c=' + categoryId);
	new Ajax.Request(url, {
		'onSuccess': WebshopCart.addSuccessSingle.bindAsEventListener(this),
		'onFailure': WebshopCart.addFailedSingle.bindAsEventListener(this)
	});
}

WebshopCart.addFailedSingle = 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>");
}

WebshopCart.addSuccessSingle = function(transport)
{
	PbLib.notice('success', PbLib.g('Product added to cart'));
}


