/******************************************
 * The Cart class
 ******************************************/

onepica.Cart = Class.create({
	initialize: function () {
		// mini cart
		this.miniCart = $('mini-cart');
		this.cartQty = $('cart_qty');
		var miniLink = $$('.header-container .quick-access a.cart-link');
		if (miniLink.length) {
			this.shoppingBagLink = miniLink[0];
			this.shoppingBagLink.href = 'javascript:;';
			this.tip = new Tip(this.shoppingBagLink, this.miniCart, {style: 'mini-cart'});
		}

		// product view  page
		this.form = $('product_addtocart_form');
		if (this.form) {
			this.addLinks = this.form.select('a.add-to-shopping-bag');
			this.addLinks.each(function(addLink) {
				this.validator = new Validation(this.form);
				// add-to-cart link
				addLink.observe('click', this.eventAddToCart.bindAsEventListener(this));
				this.loadingIcon = $('ajax-loading');
				this.message = new onepica.Message('product-view-message-container');
			}.bind(this));
			// else, product may be out of stock
		}

		this.justAdded = false;
		this.isLoading = false;
	},

	/**
	 * Add to cart click event.
	 */
	eventAddToCart: function (event) {
		event.stop();
		this.addToCart();
	},

	/**
	 * Adds an item to the cart via AJAX.
	 */	
	addToCart: function () {
		if (this.isLoading) {
			return;
		}
		this.message.hideMessage();

		if (this.validator.validate()) {
			var options = {
				method: 'post',
				onSuccess: this.addToCartSuccess.bindAsEventListener(this),
				onCreate: this.addToCartCreate.bindAsEventListener(this),
				onFailure: this.addToCartFailure.bindAsEventListener(this)
			}
			this.form.request(options);
		}
		else {
			this.message.showErrorMessage('Please select a size to continue.');
			$('collateral-tabs').style.height = $('tab:details').getHeight() + 'px';
		}		
	},

	/**
	 * AJAX add-to-cart success
	 */
	addToCartSuccess: function (transport) {
		this._toggleLoader(false);
		try {
			var response = eval('(' + transport.responseText + ')');
			if (response.isError) {
				this.message.showStyledMessage(response.message);
			}
			else {
				this.miniCart.scrollTo();
				this.miniCart.innerHTML = response.cart_html;
				this.cartQty.innerHTML = "(" + response.cartQty + ")";
				this.cartQty.title = "Shopping Bag (" + response.cartQty + ")";
				this.shoppingBagLink.prototip.show();
				this.justAdded = true;
				setTimeout('screenManager.getCart().hideMinicart()', 3500);
			}
		}
		catch (e) {}
	},

	/**
	 * AJAX add-to-cart create
	 */
	addToCartCreate: function (transport) {
		this._toggleLoader(true);
	},

	/**
	 * AJAX add-to-cart failure
	 */
	addToCartFailure: function (transport) {
		this._toggleLoader(false);
	},

	showMinicart: function () {
		this.shoppingBagLink.prototip.show();
	},

	hideMinicart: function () {
		this.shoppingBagLink.prototip.hide();
		if (this.justAdded) {
			var h3 = $('mini-cart').select('h3.item-added');
			if(h3.size() > 0 ) {
				h3 = h3[0];
			}
			h3.innerHTML = 'Shopping Bag';
			this.justAdded = false;
		}
	},
	
	/**
	 * Show/hides the AJAX loader
	 */
	_toggleLoader: function (show) {
		this.isLoading = show;
		if (show) {
			this.loadingIcon.show();
			this.addLinks.invoke('addClassName', 'btn-disabled');
		}
		else {
			this.loadingIcon.hide();
			this.addLinks.invoke('removeClassName', 'btn-disabled');
		}
	}
});
