
var Product = new Class({
	initialize : function(id,quantity,name,price) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.quantity = quantity;
	},
	add : function(quantity) {
		this.quantity += quantity;
	}
});

var Cart = new Class({
	initialize : function(url,parent,symbol,vat,delivery,lng) {
		this.parent = $(parent);
		this.list = new Element('ul');
		this.products = new Array();
		this.parent.adopt(this.list);
		this.symbol = symbol;
		this.vat = vat;
		this.delivery = delivery;
		this.url = url;
		this.lng = lng;
		this.update();
	},
	update : function() {
		this.list.empty();
		this.list.adopt(new Element('li').setText((this.lng == 'nl' ? 'Artikel(en) : ':'Article(s) : ')+this.countProducts()));
		this.list.adopt(new Element('li').setHTML((this.lng == 'nl' ? 'Totaal : ':'Total : ')+this.price()+' '+this.symbol+(this.lng == 'nl' ? ' BTW excl. verzendingskosten':' TTC hors livraison')));
	},
	request : function(id,quantity,func) {
		/* AJAX order request */
		if(id != undefined && id != '') {
			var product = this.find(id);
			var qty = (quantity == 'empty' ? -(product != undefined ? product.quantity : 0) : quantity);
			var ajax = new Ajax(this.url,{
				method:'post',
				onSuccess:function(){
					this.products = new Array();
					transaction = Json.evaluate(ajax.response.text);
					if(transaction.purchases.length > 0) {
						transaction.purchases.each(function(product){
							this.add(product.id,product.quantity,product.name,product.price);
							if(product.id == id && transaction.message) {
								alert(transaction.message);
							}
						}.bind(this));
					} else {
						this.update();
					}
					if(func != undefined) {
						eval(func);
					}
				}.bind(this)
			}).request(Object.toQueryString({'action':'order','id':id,'quantity':qty}));
		}
	},
	add : function(id,quantity,name,price) {
		product = this.find(id);
		if(product == undefined) {
			this.products.push(new Product(id,quantity,name,price,this));
		} else {
			product.add(quantity);
		}
		this.update();
	},
	set: function(id,quantity,func) {
		/* AJAX order request */
		var product = this.find(id);
		var ajax = new Ajax(this.url,{
			method:'post',
			onSuccess:function(){
				this.products = new Array();
				transaction = Json.evaluate(ajax.response.text);
				if(transaction.purchases.length > 0) {
					transaction.purchases.each(function(product){
						this.add(product.id,product.quantity,product.name,product.price);
					}.bind(this));
				} else {
					this.update();
				}
				if(func != undefined) {
					eval(func);
				}
				this.delivery = transaction.delivery.price ? Math.round(parseFloat(transaction.delivery.price)*100)/100 : 'N/A';
			}.bind(this)
		}).request(Object.toQueryString({'action':'set','id':id,'quantity':quantity}));
	},
	remove : function(product) {
		if(product != undefined) {
			this.products.remove(product);
		}
		this.update();
	},
	empty : function() {
		this.products = new Array();
		this.update();
		var ajax = new Ajax(this.url,{
			method:'post'
		}).request(Object.toQueryString({'action':'empty'}));
	},
	find : function(id) {
		var i = 0;
		//alert('looping');
		var found = this.products.some(function(product,index){
			i=index;
			//alert(product.id+'=='+id);
			return product.id == id;
		});
		if(found) {
			//alert('found id#'+id+' at position '+i);
			return this.products[i];
		} else {
			//alert('not found id#'+id);
			return undefined;
		}
	},
	countProducts : function() {
		var count = 0;
		this.products.each(function(product) {
			count += product.quantity;
		});
		return count;
	},
	price : function() {
		var price = 0;
		this.products.each(function(product) {
			price += product.price;
		});
		return Math.round(price * (100+this.vat))/100;
	},
	subTotal : function() {
		var price = 0;
		this.products.each(function(product) {
			price += product.price;
		});
		return Math.round(price*100)/100;
	},
	getVAT : function() {
		var price = this.subTotal();
		return Math.round(price * this.vat)/100;
	},
	total : function() {
		return this.price();
	}
});

