function Cart (container)
{
	this.__construct = function (container)
	{
		this.container = $(container);//alert('cart');
		this.items = {};
		this.panels = {};
		
		this.Deploy();
		this.Load();
		this.Update();
	}
	
	this.Checkout = function ()
	{//alert('checkout');
		var parts = [];
		var item;
		for (offer in this.items)
		{
			item = this.items[offer];
			parts.push(item.amount > 1 ? offer+':'+item.amount : offer);
		}
		parts = parts.join(',');
		//alert('http://intimshop.ru/kernel.php?section=users/cart&action=fill&partner=273&items='+encodeURIComponent(parts));
		window.location = 'http://intimshop.ru/kernel.php?section=users/cart&action=fill&partner='+window.partner_id+'&ids='+encodeURIComponent(parts);
	}
	
	this.Deploy = function ()
	{
		this.gui = {};
		
		this.container.className = 'cart';
		
		this.gui.empty = $('+span',{content: 'Корзина пуста', className: 'empty_message'});
		this.container.appendChild(this.gui.empty);
		
		this.gui.status = $('+span');
		this.container.appendChild(this.gui.status);
		
		this.gui.caption = $('+a',{href: $('base_url').href+'offers/list/?ids=', content: 'Корзина:', className: 'caption'});
		this.gui.status.appendChild(this.gui.caption);
		
		this.gui.count_label = $('+span',{content:'Товаров: ', className: 'count_label'});
		this.gui.status.appendChild(this.gui.count_label);
		
		this.gui.count_value = $('+span',{className: 'count_value'});
		this.gui.status.appendChild(this.gui.count_value);
		
		this.gui.price_label = $('+span',{content:' на сумму: ', className: 'price_label'});
		this.gui.status.appendChild(this.gui.price_label);
		
		this.gui.price_value = $('+span',{className: 'price_value'});
		this.gui.status.appendChild(this.gui.price_value);
		
		this.gui.checkout_button = $('+input',{type:'button',value: 'Оформить заказ'});
		this.gui.status.appendChild(this.gui.checkout_button);
		this.gui.checkout_button.control = this;
		this.gui.checkout_button.on('click',function () { this.control.Checkout(); });
		
		this.DeployPanels();
	}
	
	this.DeployPanels = function ()
	{
		elems = document.getElementsByTagName('span');
		for (var i=0; i<elems.length; i++)
		{
			if (elems[i].className == 'cart')
			{
				offer = elems[i].getAttribute('offer');
				price = elems[i].getAttribute('price');
				this.panels[offer] = new CartPanel(this,elems[i],offer,price);
			}
		}
	}
	
	this.GetItem = function (offer)
	{
		return this.items[offer];
	}
	
	this.Load = function ()
	{
		var cart = unescape(document.cookies.get('cart'));
		var parts = cart.split('&');
		for (var i=0; i<parts.length; i++)
		{
			if (m = parts[i].match(/(\d+)\=(\d+)\,(\d+(?:\.\d+)?)/))
			{
				this.SetItem(m[1],m[2],m[3]);
			}
		}
	}
	
	this.Save = function ()
	{
		var parts = [];
		for (offer in this.items)
		{
			parts.push(offer+'='+this.items[offer].amount+','+this.items[offer].price);
		}
		parts = parts.join('&');
		document.cookies.set('cart',escape(parts));
	}
	
	this.SetItem = function (offer, amount, price)
	{
		if (amount > 0)
		{
			this.items[offer] = {'amount': amount, 'price': price};
		}
		else
		{
			delete this.items[offer];
		}
		this.Save();
		this.Update();
		if (panel = this.panels[offer])
		{
			panel.Update();
		}
	}
	
	this.Update = function ()
	{
		var items_count = 0;
		var items_price = 0;
		var offers_ids = [];
		for (offer in this.items)
		{
			items_count++;
			items_price += parseFloat(this.items[offer].price);
			offers_ids.push(offer);
		}
		this.gui.count_value.innerHTML = items_count;
		this.gui.price_value.innerHTML = items_price+' р.';
		this.gui.caption.href = this.gui.caption.href.replace(/\?.+$/,'?ids='+encodeURIComponent(offers_ids.join(',')));
		
		if (items_count)
		{
			this.gui.status.style.display = 'inline';
			this.gui.empty.style.display = 'none';
		}
		else
		{
			this.gui.status.style.display = 'none';
			this.gui.empty.style.display = 'inline';
		}
	}
	
	this.__construct(container);
}

function CartPanel (cart, container, offer, price)
{
	this.__construct = function (cart, container, offer, price)
	{
		this.cart = cart;
		this.container = $(container);
		this.offer = offer;
		this.price = price;
		
		this.Deploy();
	}
	
	this.Deploy = function ()
	{
		this.gui = {};
		
		this.container.className = 'cart_panel';
		
		this.gui.add_link = $('+span',{content: 'Положить в корзину'});
		this.container.appendChild(this.gui.add_link);
		this.gui.add_link.className = 'add_link';
		this.gui.add_link.control = this;
		this.gui.add_link.on('click',function () { this.control.Set(1); });
		//$('d').value += this.gui.add_link.tagName;
		
		this.gui.remove_link = $('+span',{content: 'Убрать из корзины'});
		this.container.appendChild(this.gui.remove_link);
		this.gui.remove_link.className = 'remove_link';
		this.gui.remove_link.control = this;
		this.gui.remove_link.on('click',function () { this.control.Set(0); });
		
		this.gui.checkout_link = $('+span',{content: 'Оформить заказ'});
		this.container.appendChild(this.gui.checkout_link);
		this.gui.checkout_link.className = 'checkout_link';
		this.gui.checkout_link.control = this;
		this.gui.checkout_link.on('click',function () { this.control.cart.Checkout(); });
		
		this.Update();
	}
	
	this.Set = function (amount)
	{
		this.cart.SetItem(this.offer, amount, this.price);
		this.Update();
	}
	
	this.Update = function ()
	{
		if (info = this.cart.GetItem(this.offer))
		{
			// in cart
			this.gui.add_link.style.display = 'none';
			this.gui.remove_link.style.display = 'inline';
			this.gui.checkout_link.style.display = 'block';
		}
		else
		{
			// not in cart
			this.gui.add_link.style.display = 'inline';
			this.gui.remove_link.style.display = 'none';
			this.gui.checkout_link.style.display = 'none';
		}
	}
	
	this.__construct(cart,container,offer,price);
}
