
str('cart',{
	'label': 'Корзина',
	'checkout':'Оформить заказ'
	});

str('cart_panel',{
	'add_to_cart':'В корзину',
	'added_label':'В корзине: ',
	'checkout':'Оформить заказ',
	'remove_from_cart':'Выложить'
	});

function Cart (container, items)
{
	this.__construct = function (container, items)
	{
		this.container = $(container);
		this.uri = '/kernel.php?section=users/cart&';
		this.api = new DataSource('json',this.uri,new Callback(this,'QueryReply'));
		
		this.items = items;
		this.panels = {};
		
		if (navigator.userAgent.search(/MSIE/) == -1) //if (false)
		{
			this.Deploy();
			this.Update();
		}
	}
	
	/*this.Add = function (item_id, amount)
	{
		this.Query('action=add&item_id='+item_id+'&amount='+amount);
	}*/
	
	this.Deploy = function ()
	{
		this.container.className = 'cart';
		while(this.container.hasChildNodes())
		{
			this.container.removeChild(this.container.lastChild);
		}
		
		this.gui = {};
		
		this.gui.amount = $('+span');
		this.gui.amount.className = 'amount';
		this.gui.amount.innerHTML = '0';
		this.container.appendChild(this.gui.amount);
		
		this.gui.checkout = $('+a');
		this.gui.checkout.className = 'checkout';
		this.gui.checkout.setAttribute('href','/kernel.php?section=users/inventory&action=show');
		this.gui.checkout.appendChild(document.createTextNode(str('cart.checkout')));
		this.container.appendChild(this.gui.checkout);
		
		this.enabled = true;
	}
	
	/*this.Edit = function (item_id, amount)
	{
		this.Query('action=edit&item_id='+item_id+'&amount='+amount);
	}*/
	
	this.Get = function (item_id)
	{
		return this.items[item_id];
	}
	
	this.InsideCart = function ()
	{
		return (document.location.toString().search(/section\=users\/inventory/) != -1);
	}
	
	this.Query = function (args)
	{
		this.api.uri = this.uri+args;
		//alert(this.api.uri);
		this.api.Get();
	}
	
	this.QueryReply = function (data)
	{
		if (data.success == 1)
		{
			if (data.amount)
			{
				this.items[data.item_id] = {price: data.price, amount: data.amount};
			}
			else
			{
				delete this.items[data.item_id];
			}
			if (this.panels[data.item_id])
			{
				this.panels[data.item_id].Update();
			}
			this.Update();
		}
	}
	
	this.RegisterPanel = function (item_id, obj)
	{
		this.panels[item_id] = obj;
	}
	
	/*this.Remove = function (item_id, amount)
	{
		this.Query('action=remove&item_id='+item_id+'&amount='+amount);
	}*/
	
	this.Set = function (item_id, amount)
	{
		this.Query('action=set&item_id='+item_id+'&amount='+amount);
		if (this.InsideCart())
		{
			document.location = document.location.toString().replace(/\&rand\=\d+/,'')+'&rand='+Math.round(Math.random()*100000);
		}
	}
	
	this.Update = function ()
	{
		// panels
		var items_count = 0;
		for (item_id in this.items)
		{
			if (this.panels[item_id])
			{
				this.panels[item_id].Update();
			}
			items_count++;
		}
		
		// self
		this.gui.checkout.style.display = (items_count && !this.InsideCart()) ? 'inline' : 'none';
		this.gui.amount.innerHTML = items_count ? items_count : '0';
	}
	
	this.__construct(container, items);
}

// ============================================================================

function CartPanel (item_id, container, args)
{
	this.__construct = function (item_id, container, args)
	{
		this.item_id = item_id;
		this.container = $(container);
		this.args = args;
		this.cart = window.cart;
		
		if (this.cart.enabled)
		{
			this.Deploy();
			this.Update();
		}
	}
	
	this.Add = function ()
	{
		this.cart.Set(this.item_id,1);
		//this.cart.Add(this.item_id,1);
	}
	
	this.Checkout = function ()
	{
		document.location = '/kernel.php?section=users/inventory&action=show';
		//alert('OK');
	}
	
	this.Deploy = function ()
	{
		// container
		this.container.className = 'cart_panel';
		while(this.container.hasChildNodes())
		{
			this.container.removeChild(this.container.lastChild);
		}
		
		this.gui = {};
		
		this.gui.add = $('+a');
		this.gui.add.className = 'add_to_cart';
		this.gui.add.control = this;
		this.gui.add.setAttribute('href','javascript:void(0);');
		this.gui.add.appendChild(document.createTextNode(str('cart_panel.add_to_cart')));
		this.container.appendChild(this.gui.add);
		this.gui.add.on('click',function () { this.control.Add(); });
		
		this.gui.label = $('+span');
		this.gui.label.className = 'label';
		this.gui.label.appendChild(document.createTextNode(str('cart_panel.added_label')));
		this.container.appendChild(this.gui.label);
		
		/*this.gui.plus = $('+a');
		this.gui.plus.className = 'amount_plus';
		this.gui.plus.control = this;
		this.gui.plus.setAttribute('href','javascript:void(0);');
		this.gui.plus.appendChild(document.createTextNode('+'));
		this.container.appendChild(this.gui.plus);
		this.gui.plus.on('click',function () { this.control.Plus(); });*/
		
		this.gui.amount = $('+input');
		this.gui.amount.className = 'amount';
		this.gui.amount.control = this;
		this.gui.amount.setAttribute('type','text');
		this.gui.amount.appendChild(document.createTextNode('0'));
		this.container.appendChild(this.gui.amount);
		this.gui.amount.on('click',function () { this.control.SetCustomAmount(); });
		this.gui.amount.on('keyup',function () { this.control.cart.Set(this.control.item_id,parseInt(this.value)); });
		
		/*this.gui.minus = $('+a');
		this.gui.minus.className = 'amount_minus';
		this.gui.minus.control = this;
		this.gui.minus.setAttribute('href','javascript:void(0);');
		this.gui.minus.appendChild(document.createTextNode('–'));
		this.container.appendChild(this.gui.minus);
		this.gui.minus.on('click',function () { this.control.Minus(); });*/
		
		this.gui.checkout = $('+a');
		this.gui.checkout.className = 'checkout';
		this.gui.checkout.control = this;
		this.gui.checkout.setAttribute('href','javascript:void(0);');
		this.gui.checkout.appendChild(document.createTextNode(str('cart_panel.checkout')));
		this.container.appendChild(this.gui.checkout);
		this.gui.checkout.on('click',function () { this.control.Checkout(); });
		
		this.gui.remove = $('+a');
		this.gui.remove.className = 'remove_from_cart';
		this.gui.remove.control = this;
		this.gui.remove.setAttribute('href','javascript:void(0);');
		this.gui.remove.appendChild(document.createTextNode(str('cart_panel.remove_from_cart')));
		this.container.appendChild(this.gui.remove);
		this.gui.remove.on('click',function () { this.control.Remove(); });
		
		this.cart.RegisterPanel(this.item_id,this);
	}
	
	this.GetAmount = function ()
	{
		return (item = this.cart.Get(this.item_id)) ? item.amount : 0;
	}
	
	this.IsPrimaryPanel = function ()
	{
		return (document.location.toString().search(new RegExp('(offer|_o)'+this.item_id+'\.html')) != -1);
	}
	
	this.Minus = function ()
	{
		this.cart.Set(this.item_id,this.GetAmount()-1);
		//this.cart.Remove(this.item_id,1);
	}
	
	this.Plus = function ()
	{
		this.cart.Set(this.item_id,this.GetAmount()+1);
		//this.cart.Add(this.item_id,1);
	}
	
	this.Remove = function ()
	{
		this.cart.Set(this.item_id,0);
		//this.cart.Remove(this.item_id,0);
	}
	
	this.SetCustomAmount = function ()
	{
		if (amount = window.prompt('Введите количество:'))
		{
			this.cart.Set(this.item_id,parseInt(amount));
		}
	}
	
	this.Update = function ()
	{
		info = this.cart.Get(this.item_id);
		if (info)
		{
			// edit/remove
			this.gui.add.style.display = 'none';
			this.gui.label.style.display = 'inline';
			if (this.args.max > 1)
			{
				//this.gui.plus.style.display = 'inline';
				this.gui.amount.style.display = 'inline';
				this.gui.amount.value = info.amount;
				//this.gui.minus.style.display = 'inline';
			}
			else
			{
				//this.gui.plus.style.display = 'none';
				this.gui.amount.style.display = 'none';
				//this.gui.minus.style.display = 'none';
			}
			this.gui.remove.style.display = 'inline';
			this.gui.checkout.style.display = this.IsPrimaryPanel() ? 'inline' : 'none';
		}
		else
		{
			// add
			this.gui.add.style.display = 'inline';
			this.gui.label.style.display = 'none';
			//this.gui.plus.style.display = 'none';
			this.gui.amount.style.display = 'none';
			//this.gui.minus.style.display = 'none';
			this.gui.remove.style.display = 'none';
			this.gui.checkout.style.display = 'none';
		}
	}
	
	this.__construct(item_id, container, args);
}