/******************************************
 * 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: 'pink'});
		}

		// product view  page
		this.form = $('product_addtocart_form');
		if (this.form) {
			this.addLink = this.form.down('a.add-to-shopping-bag');
			if (this.addLink) {
				this.validator = new Validation(this.form);
				// add-to-cart link
				this.addLink.observe('click', this.addToCart.bindAsEventListener(this));
				this.loadingIcon = $('ajax-loading');
				this.message = new onepica.Message('product-view-message-container');
			}
			// else, product may be out of stock
		}

		this.justAdded = false;
		this.isLoading = false;
	},

	/**
	 * Adds an item to the cart via AJAX.
	 */
	addToCart: function (event) {
		if(this.isLoading) return;
		event.stop();
		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.innerHTML = response.cart_html;
				this.cartQty.innerHTML = "Shopping Bag (" + response.cartQty + ")";
				this.cartQty.title = "Shopping Bag (" + response.cartQty + ")";
				Cufon.replace('#mini-cart .cufon');
				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";
			Cufon.replace(h3, { fontFamily: 'Futura' });
			this.justAdded = false;
		}
	},
	
	/**
	 * Show/hides the AJAX loader
	 */
	_toggleLoader: function (show) {
		this.isLoading = show;
		if (show) {
			this.loadingIcon.setStyle({visibility: 'visible'});
			this.addLink.addClassName('btn-disabled');
		}
		else {
			this.loadingIcon.setStyle({visibility: 'hidden'});
			this.addLink.removeClassName('btn-disabled');
		}
	}
});