var Cart = new Class({
	
	Implements: [Options, Events],
	
	initialize: function(options) {
		this.setOptions(options);
		this.addRequest = new Request.JSON({'url':'cart/add.json', 'onComplete': 
            function (res) {
                this.fillInfo(res);
                if (window.isIndex) {
                        top.location.href = 'http://vedamarket.local'
                    }
            }.bind(this)
        });
        
		this.quantityRequest	= new Request.JSON({'url':'cart/set_quantity.json', 'onComplete': this.fillInfo});
		this.removeRequest		= new Request.JSON({'url':'cart/remove.json', 'onComplete': this.fillInfo});
		this.infoRequest		= new Request.JSON({'url':'cart/load_cart.json', 'onComplete': this.fillInfo});
	},
	
	add: function(id, quantity, params) {
		if (!window.isIndex) {
		//$('button-view-shopping-cart').removeClass('none');
		//$('button-issue-order').removeClass('none');
		}
		this.addRequest.post({
			'id': id,
			'quantity': quantity || 1
		});
	},

	remove: function(id) {
		this.removeRequest.post({
			'id': id
		});
		if ($('cart-line-'+id)) $('cart-line-'+id).destroy();
	},
	
	setQuantity: function(id, quantity) {
		if (quantity.toInt() == 0 || isNaN(quantity.toInt())) {
			if (confirm('Вы хотите удалить товар из корзины ?')) {
				$('quantity-'+id).set('value', 0);
				app.Cart.remove(id);
				return true;
			} else {
				$('quantity-'+id).set('value', 1);
				return false;
			}
		}
		this.quantityRequest.post({
			'id':		id,
			'quantity':	quantity
		})
	},

	fillInfo: function(info) {
		$('shopping-cart-sum').set('text', info.cart.sum);
		$('shopping-cart-quantity').set('text', info.cart.quantity);

		$$('.product_order').each(function(el) { el.destroy(); });

		if ($type(info.cart.products) != 'array') {
			for (var i in info.cart.products) {
				var newOP = new Element('tr', { 'class': 'product_order' }).adopt([
					new Element('input', {
						'type':		'hidden',
						'class':	'input_hidden',
						'name':		'order[products][' + i + '][id_product]',
						'value':	i
					}),
					new Element('td', {
						'class':	'name'
					}).grab(
						new Element('a', {
							'title':	'Перейти к странице продукта',
							'href':		'/',
							'text':		info.cart.products[i].title
						})
					),
					new Element('td', {
						'class':	'amount'
					}).grab(
						new Element('input', {
							'class':	'order_input_text',
							'type':		'text',
							'name':		'order[products][' + i + '][amount]',
							'value':	info.cart.products[i].quantity,
							'events': {
								'keyup': onAmountOrderProductChange
							}
						})
					),
					new Element('td', {
						'class':	'price',
						'text':		info.cart.products[i].price.toInt() + ' грн'
					})
				]).inject($('product-order-summary'), 'before');
	
				new Element('td', {
					'class':	'product_remove',
					'html':		'&nbsp;',
					'events':	{
						'click':	removeOrderProduct.bind(newOP)
					}
				}).inject(newOP, 'bottom');
			
				newOP			= null;
			}
		}

		refreshOrder();
	},

	loadInfo: function() {
		this.infoRequest.post();
	}
});

if (!window.app) window.app = {};
app.Cart = new Cart();