(function() {
	var reqObj = null;
	
	ShopProduct = function(productId, productParams)
	{
		this.id = productId;
		for(var param in productParams)
		{
			this[param] = productParams[param];
		}
		this.Calculate();
	};
	ShopProduct.prototype = {
		AcceptChanges : function() 
		{
			if(this.quantity > 0)
			{
				this.Calculate();
				
				
				yd.get("qty_" + this.id).value = this.quantity;
				yd.get("product_price_" + this.id).innerHTML = NB.Math.FormatNumber(this.subTotalPrice, 2, ",", " ") + " &euro;";
				
				if(reqObj != null && yc.isCallInProgress(reqObj))
				{
					yc.abort(reqObj);
					reqObj = null;
				}
				
				getUrl 	= "/request.php?action=update_qty";
				getUrl += "&product_id=" + this.id;
				getUrl += "&quantity=" + this.quantity;
				
				reqObj = yc.asyncRequest('GET', getUrl, null, null);
			}
			else
			{
				this.Remove();
			}
		},
		Calculate : function()
		{
			this.subTotalPrice 	= NB.Math.GetFloat(this.quantity * this.price, 2);
			this.deliveryCost 	= NB.Math.GetFloat(this.GetDeliveryCost(), 2);
			this.totalPrice 	= NB.Math.GetFloat(this.subTotalPrice + this.deliveryCost, 2);
		},
		GetDeliveryCost : function()
		{
			var deliveryCost = 0;
			
			var dependablePrice = (this.deliveryPrice2 != this.deliveryPrice1 && this.deliveryPrice3 != this.deliveryPrice2);
			
			if(dependablePrice)
			{
				if(this.quantity >= shopConfig.quantities.QTY3)
				{
					deliveryCost = this.deliveryPrice3;
				}
				else if(this.quantity >= shopConfig.quantities.QTY2)
				{
					deliveryCost = this.deliveryPrice2;
				}
				else
				{
					deliveryCost = this.deliveryPrice1;
				}
			}
			else
			{
				deliveryCost = this.quantity * this.deliveryPrice1;
			}
			
			
			
			
			return deliveryCost;
		},
		Remove : function()
		{
			var obj = yd.get("product_" + this.id);
			if(obj != null)
			{
				obj.parentNode.removeChild(obj);
			}
			
			obj = yd.get("splitter_" + this.id);
			if(obj != null)
			{
				obj.parentNode.removeChild(obj);
			}
			
			this.quantity = 0;	
			
			getUrl 	= "/request.php?action=remove_product";
			getUrl += "&product_id=" + this.id;
				
			reqObj = yc.asyncRequest('GET', getUrl, null, null);
		}
	}
})();
