var Shop = new Class({

	/* initialize needed methods
	 ======================================================================= */
	initialize: function() {
		// if(!$chk($('basket-container'))) return false;
		this.growlBasket = $('growlbasket');
	},

	/* attach events and add the initially fired methods
	 ======================================================================= */
	hideGrowlBasket: function() {
		this.set({
			'morph': {
				duration: 500
			}
		});
		
		this.morph({
			'opacity': 0,
			'margin-top': 20
		});
	},

	basketInfo: function(action, productId) {
		var requestUrl = absoluteUrl+'shop/count_basket_items/1';
		new Request.HTML({
			url: requestUrl,
			update: 'basket-info',
			method: 'post',
			onSuccess: function() {
				$('basket-info').setStyle('display', 'block');
			}
		}).send();
	},
	
	basketAction: function(action, productId, fullform, onsuccess) {
		var requestUrl = absoluteUrl+'shop/basket_action/'+action+'/'+productId;
		var success = new Request.HTML({
			url: requestUrl,
			update: 'growlbasket',
			method: 'post',
			data: (fullform ? $(fullform).toQueryString() : ''),
			onFailure: function() {
				alert("Sorry, a problem occurred while adding item. Please try again.");
			}.bind(this),
			onSuccess: function(response, responseElements, responseHTML) {
				this.growlBasket.set({
					'html': responseHTML,
					'morph': {
						duration: 500
					}
				});
				this.growlBasket.morph({
					'opacity': 1,
					'margin-top': [-20, 0]
				});

				this.basketInfo(action, productId);
				this.hideGrowlBasket.delay(5000, this.growlBasket);
				if (responseHTML.indexOf('basket-error') === -1 && onsuccess) {
					onsuccess();
				}
			}.bind(this)
		}).send();
	}
});

// LEGACY
basket_action = function(action, productId, fullform) {
	var Basket = new Shop();
	Basket.basketAction(action, productId, fullform);
}
