var Product = new Class({
	initialize : function(id,quantity,name,price,list,vat) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.quantity = quantity;
		this.list = list;
		this.vat = vat;
	},
	add : function(quantity) {
		this.quantity += quantity;
	}
});

var Cart = new Class({
	initialize : function(url,parent,symbol,delivery,lng) {
		this.parent = $(parent);
		this.products = new Array();
		this.symbol = symbol;
		this.delivery = delivery;
		this.url = url;
		this.lng = lng;
		this.update();
	},
	update : function() {
		this.parent.getElement('#productsQt').set('text', this.countProducts());
		this.parent.getElement('#cartTotal').set('html', this.price(true)+' '+this.symbol);
	},
	request : function(id,className,quantity,list,func,purchase,options) {
		/* AJAX order request */
		if(id != undefined && id != '') {
			iTotalQuantity = 0;
			iTotalPrice = 0;
			if(list == undefined) {
				list = 0;
			}
			if(options == undefined) {
				options = 0;
			}
			var product = this.find(id);
			var qty = (quantity == 'empty' ? -(product != undefined ? product.quantity : 0) : quantity);
			var ajax = new Request({
				url:this.url,
				method:'post',
				onSuccess:function(response){
					this.products = new Array();
					transaction = JSON.decode(response);
					if(transaction.purchases.length > 0) {
						transaction.purchases.each(function(product){
							iTotalQuantity += product.quantity;
							iTotalPrice += product.price;
							this.add(product.id,product.quantity,product.name,product.price,list,product.vat);
						}.bind(this));
						if(quantity > 0) {
							var shopDiv = new Element('div');
							shopDiv.set('html', transaction.message);
							shopDiv.inject(document.body);
						}
						
					} else {
						this.update();
					}
					if(func != undefined && func != null) {
						eval(func);
					}
				}.bind(this)
			}).send('action=order&id='+id+'&className='+className+'&quantity='+qty+'&list='+list+'&options='+options+'&purchase='+purchase);
		}
	},
	add : function(id,quantity,name,price,list,vat) {
		this.products.push(new Product(id,quantity,name,price,list,vat.toInt(),this));
		this.update();
	},
	set: function(id,className,quantity,func,list,purchase) {
		/* AJAX order request */
		var product = this.find(id);
		var ajax = new Request({
			url:this.url,
			method:'post',
			onSuccess:function(response){
				this.products = new Array();
				transaction = JSON.decode(response);
				if(transaction.purchases.length > 0) {
					transaction.purchases.each(function(product){
						this.add(product.id,product.quantity,product.name,product.price,product.list,product.vat);
					}.bind(this));
				} else {
					this.update();
				}
				if(func != undefined) {
					eval(func);
				}
				this.delivery = transaction.delivery.price ? Math.ceil(parseFloat(transaction.delivery.price)*100)/100 : 0;
			}.bind(this)
		}).send('action=set&id='+id+'&className='+className+'&quantity='+quantity+'&list='+list+'&purchase='+purchase);
	},
	remove : function(product) {
		if(product != undefined) {
			this.products.remove(product);
		}
		this.update();
	},
	empty : function() {
		this.products = new Array();
		this.update();
		var ajax = new Request({
			url:this.url,
			method:'post'
		}).send('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(bVat) {
		var price = 0;
		this.products.each(function(product) {
			if(bVat) {
				price += product.price;
			}else{
				price += (product.price * 100 / (100 + product.vat));
			}
		});
		var number = new Number(price);
		return parseFloat(number.toFixed(2));
	},
	subTotal : function() {
		var price = this.price(false);
		var number = new Number(price);
		return parseFloat(number.toFixed(2));
	},
	getVAT : function() {
		var price = this.total();
		var subTotal = this.subTotal();
		var number = new Number(price - subTotal);
		return parseFloat(number.toFixed(2));
	},
	total : function() {
		return this.price(true);
	}
});
