var Observable = Class.create();
Observable.prototype = {
	initialize: function() {
		this.obs = new Array();
		this.size = 0;
	},
	addObserver: function(o) {
		this.obs[this.size++] = o;
	},
	notifyObservers: function() {
		for (var i = 0; i < this.obs.length; i++) {
			try {
				this.obs[i].update(this);
			} catch (e) {
			}
		}
	}
}
var Cart = Class.create();
Cart.prototype = {
	initialize: function(totalPriceId, initProperties) {
		this.fullId = "cartfull";
		this.emptyId = "cartempty";
		this.length = 0;
		this.cart = new Array();
		this.ids = new Array();
		if (totalPriceId && totalPriceId != null)
			this.totalPriceId = totalPriceId;
		if (initProperties && initProperties != null)
			this.url = initProperties["url"];
		this.id = "Cart";
	},
	repaint: function() {
		var container = $(this.totalPriceId);
		var container_iva = document.getElementById('total_iva');
		if (container) {
			if (isNaN(this.totalPrice)) {
				container.innerHTML = "-";
			} else {
				container.innerHTML = this.totalPrice.toFixed(2);
				if(container_iva)
					var v = parseFloat(this.totalPrice) + parseFloat(((this.totalPrice*10)/100));
					container_iva.innerHTML = v.toFixed(2);
			}
		}
		var full = $(this.fullId);
		var empty = $(this.emptyId);
		if (this.length > 0) {
			if (full)
				Element.show(full);
			if (empty)
				Element.hide(empty);
		} else {
			if (full)
				Element.hide(full);
			if (empty)
				Element.show(empty);
		}
	},
	update: function(observable) {
		this.totalPrice = 0;
		for (var i = 0; i < this.cart.length; i++) {
			 if (this.cart[i]){
				this.totalPrice += this.cart[i].price;
			}
		}
		this.repaint();
	},
	list: function() {
		var upThis = this;
		var pars = 'action=list';
		var ajax = new Ajax.Request(this.url, {
			method: "get",
			parameters: pars, 
			onSuccess: function(res){ upThis.updateSuccess(res); },
			onFailure: function(res){ alert("Error: "+ res.status +" "+ res.statusText);}
		});
	},
	add: function(prodId, price, qty) {
		var upThis = this;
		var pars = 'action=add' + '&prodID=' + prodId;
		if (price)
			pars += "&price=" + price;
		if (qty)
			pars += "&qty=" + qty;			
		var ajax = new Ajax.Request(this.url, {
			method: "get",
			parameters: pars, 
			onSuccess: function(res){ upThis.addSuccess(res);  window.location.href="carrello.pag";   window.open('carrello.pag','_self'); },
			onFailure: function(res){ alert("Error: "+ res.status +" "+ res.statusText);}
		});
	},
	get: function(prodId) {
		return this.cart[prodId];
	},
	set: function(row) {
		this.cart[row.prodId] = row;
		this.length++;
		row.addObserver(this);
	},
	increment: function(prodId) {
		if (this.get(prodId))
			this.get(prodId).increment();
	},
	decrement: function(prodId) {
		if (this.get(prodId))
			this.get(prodId).decrement();
	},
	remove: function(prodId) {
		var row = this.get(prodId);
		if (row) {
			this.length--;
			this.cart[prodId] = null;
			row.remove();
		}
	},
	addSuccess: function(res) {
	}
}

var CartRow = Class.create();
CartRow.prototype = Object.extend(new Observable(), {
	initialize: function(prodId, initProperties) {
		this.prodId = prodId;
		this.rowId = "row" + prodId;
		this.quantityId = "quantity" + prodId;
		this.totalPriceId = "total" + prodId;
		if (initProperties != null) {
			this.url = initProperties["url"];
			this.unitPrice = parseFloat(initProperties["unitPrice"].replace(',', '.'));
			this.quantity = initProperties["quantity"];
		} else {
			this.unitPrice = 0;
			this.quantity = 1;
		}
		this.setQuantity(this.quantity);
		this.id = "ChartRow"+ prodId;
	},
	setQuantity: function(quantity) {
		if (isNaN(this.quantity))
			this.quantity = 0;
		else
			this.quantity = quantity;
		if (isNaN(this.unitPrice))
			this.price = 0;
		else
			this.price = this.quantity * this.unitPrice;
		this.repaint();
		this.notifyObservers();
	},
	repaint: function() {
		if (this.quantity > 0) {
			var container = $(this.quantityId);
			if (container)
				container.innerHTML = this.quantity;

			var container = $(this.totalPriceId);
			if (container) {
				if (isNaN(this.unitPrice))
					container.innerHTML = "-";
				else
					container.innerHTML = this.price.toFixed(2);
			}
		} else if (this.quantity == 0) {
			var container = $(this.rowId);
			if (container) {
				Element.hide(container);
				Element.remove(container);
			}
		}
	},
	increment: function() {
		this.update(1);
	},
	decrement: function() {
		this.update(-1);
	},
	remove: function() {
		var upThis = this;
		var pars = 'action=delete' + '&prodID=' + this.prodId;
		var ajax = new Ajax.Request(this.url, {
			method: "get",
			parameters: pars, 
			onSuccess: function(res){ upThis.removeSuccess(res); }
		});
	},
	update: function(qty) {
		var upThis = this;
		var pars = 'action=update' + '&prodID=' + this.prodId + '&qty=' + qty;
		var ajax = new Ajax.Request(this.url, {
			method: "get",
			parameters: pars, 
			onSuccess: function(res){ upThis.updateSuccess(res); }
		});
	},
	updateSuccess: function(res) {
		var quantity = parseInt(res.responseXML.getElementsByTagName("quantity")[0].firstChild.nodeValue);
		if (quantity != this.quantity)
			this.setQuantity(quantity);
	},
	removeSuccess: function(res) {
		this.setQuantity(0);
	}
})

