PrdCart = Class.create(BasicControl, {
	initialize: function($super, config) {
		Object.extend(this, {
			config: {
				cntr: null
			},

			cntr: null
		});

		$super('prd_cart', config);

		this.freeze();
	},

	onReady: function() {
		var btnClear;

		this.cntr = $(this.config.cntr);
		this.unfreeze();

		if (btnClear = this.cntr.down('input[name=clear]'))
		{
			this.addAction(btnClear, 'click', this.clear.bind(this), true);
		}
	},

	clear: function() {
		this.request({clear: true});
	},

	add: function(productId, variantId) {
		this.request({add: true, productId: productId, variantId: variantId});
	},

	request: function(get) {
		if (this.cntr && !this.frozen)
		{
			this.freeze();
			this.cntr.startWaiting('bigWaiting');

			new Ajax.Request(this.getUrl(get), {
				method: 'GET',
				onFailure: function() {
					pAlert(
						this.getString('err_internal'), {title: this.getString('err')}
					);

					this.cntr.stopWaiting();
					this.unfreeze();
				}.bind(this),
				onSuccess: function(transport) {
					var data, btnClear;

					if (!(data = this.evalResponse(transport)) || data.error)
					{
						pAlert(
							(data ? data.error : null) || this.getString('err_internal'), {title: this.getString('err')}
						);
					}
					else
					{
						this.cntr.update(data.content);

						if (btnClear = this.cntr.down('input[name=clear]'))
						{
							this.addAction(btnClear, 'click', this.clear.bind(this), true);
						}
					}

					this.cntr.stopWaiting();
					this.unfreeze();
				}.bind(this)
			});
		}
	}
});