var Product = new Class({
	initialize : function(id,quantity,name,price,list) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.quantity = quantity;
		this.list = list;
	},
	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();
		listItem1 = new Element('li', {
			html:('Contient : <strong>')+this.countProducts()+' article(s)</strong>'
		});
		this.list.adopt(listItem1);
	},
	request : function(id,quantity,list,func) {
		/* AJAX order request */
		if(id != undefined && id != '') {
			iTotalQuantity = 0;
			iTotalPrice = 0;
			if(list == undefined) {
				list = 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);
							if(product.id == id && transaction.message && product.list == list) {
								sProductName = product.name;
								if(list > 0 && quantity != 'empty') {
									var quantityList = $('quantity'+product.id);
									for(i=0; i<quantity; i++) {
										quantityList.getLast().destroy();
									}
									var eEmpty = new Element('p', {text: "-"});
									var eSold = new Element('p', {text: "Vendu"});
									if(quantityList.getChildren().length == 0) {
										eEmpty.replaces(quantityList);
										eSold.replaces($('buy'+product.id));
									}
								}
							}
						}.bind(this));
						
						if(quantity > 0) {
							var shopDiv = new Element('div');
							shopDiv.setProperty('class','cartPopup');
							
							var backgroundDiv = new Element('div');
							backgroundDiv.setProperty('class','backgroundPopup');
							backgroundDiv.setStyle('opacity',0.5);
							
							var bgImg = new Element('img', {src:"images/public/popup_box.png"});
							if(window.navigator.appName.search(/microsoft/gi) != -1){
								bgImg.setStyle('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/public/popup_box.png", sizingMethod="scale");');
								bgImg.setProperty('src','images/public/transp.gif');
								bgImg.setStyles({
									'height': 335,
									'width': 582
								});
							}
							if(Browser.Engine.trident4) {
								if(window.getSize().y > window.getScrollSize().y) {
									backgroundDiv.setStyle('height',window.getSize().y);
								}else{
									backgroundDiv.setStyle('height',window.getScrollSize().y);
								}
							}
							var contentDiv = new Element('div', {id: "popupContent"});
							var text1 = new Element('h2', {text: "Vous venez d'ajouter au panier le(s) produit(s) suivant(s) :"});
							text1.setProperty('class','cartAdd');
							shopDiv.adopt(bgImg);
							shopDiv.adopt(contentDiv);
							contentDiv.adopt(text1);
							
							var text2 = new Element('h3', {text:sProductName});
							contentDiv.adopt(text2);
							
							var text3 = new Element('p', {text:'Quantite : '+quantity});
							contentDiv.adopt(text3);
							
							var buttons = new Element('p');
							
							var button2 = new Element('a', {html:'<span>Terminer ma commande</span>'});
							button2.setProperty('class','rightButton');
							button2.setProperty('href',transaction.cartUrl);
							buttons.adopt(button2);
							
							var button1 = new Element('a', {html:'<span>Continuer mes achats</span>'});
							button1.setProperty('href',"javascript:void(0);");
							button1.setProperty('class','leftButton');
							buttons.adopt(button1);
							contentDiv.adopt(buttons);
							
							$(document.body).adopt(backgroundDiv);
							$(document.body).adopt(shopDiv);
							
							button1.onclick = function() {
								backgroundDiv.destroy();
								shopDiv.destroy();
							}
						}
					}else{
						this.update();
					}
					if(func != undefined) {
						eval(func);
					}
				}.bind(this)
			}).send('action=order&id='+id+'&quantity='+qty+'&list='+list);
		}
	},
	add : function(id,quantity,name,price,list) {
		product = this.find(id, list);
		if(product == undefined) {
			this.products.push(new Product(id,quantity,name,price,list,this));
		} else {
			product.add(quantity);
		}
		this.update();
	},
	set: function(id,quantity,func,list) {
		/* AJAX order request */
		var product = this.find(id,list);
		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);
					}.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+'&quantity='+quantity+'&list='+list);
	},
	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, list) {
		var i = 0;
		//alert('looping');
		var found = this.products.some(function(product,index){
			i=index;
			//alert(product.id+'=='+id);
			return product.id == id && product.list == list;
		});
		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;
		});
		var number = new Number(price);
		return parseFloat(number.toFixed(2));
		//return Math.ceil(price * (100+this.vat))/100;
	},
	subTotal : function() {
		var price = this.total();
		var number = new Number(price/(100 + this.vat)*100);
		return parseFloat(number.toFixed(2));
		//return Math.ceil((price/(100 + this.vat)*100)*100)/100;
	},
	getVAT : function() {
		var price = this.total();
		var subTotal = this.subTotal();
		var number = new Number(price - subTotal);
		return parseFloat(number.toFixed(2));
		//return Math.ceil((price - subTotal)*100)/100;
	},
	total : function() {
		return this.price();
	}
});

