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', {
			text:(this.lng == 'nl' ? 'Artikel(en) : ':'Article(s) : ')+this.countProducts()
		});
		listItem2 = new Element('li', {
			html:(this.lng == 'nl' ? 'Totaal : ':'Total : ')+this.price()+' '+this.symbol+(this.lng == 'nl' ? ' BTW':' TTC')
		});
		this.list.adopt(listItem1);
		this.list.adopt(listItem2);
	},
	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);
							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 popupImage = new Element('img');
							popupImage.setProperty('src','files/images/popup_bg.png');
							popupImage.setProperty('class','popupBg');
							if(window.navigator.appName.search(/microsoft/gi) != -1){
								popupImage.setStyle('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src="files/images/popup_bg.png", sizingMethod="scale");');
								popupImage.setProperty('src','files/images/transp.gif');
								popupImage.setStyles({
									'height': 250,
									'width': 472
								});
							}
							shopDiv.adopt(popupImage);
							
							if(this.lng == 'fr') {
								sTitle = 'Réservation';
								sQuantity = 'Quantité';
								sTotal = 'Montant total';
								sOrder = 'Réserver';
								sContinue = 'Continuer mes achats';
							}else if(this.lng == 'nl') {
								sTitle = 'Bestelling';
								sQuantity = 'Hoeveelheid';
								sTotal = 'Totaal bedrag';
								sOrder = 'Réserver';
								sContinue = 'Mijn aankopen voortzetten';
							}else{
								sTitle = 'Order';
								sQuantity = 'Quantity';
								sTotal = 'Total amount';
								sOrder = 'Bestellen';
								sContinue = 'Keep buying';
							}
							
							var popupTitle = new Element('h1', {text: sTitle});
							shopDiv.adopt(popupTitle);
							
							var productBox = new Element('div');
							productBox.setProperty('class', 'popupBox');
							
							var productName = new Element('h2', {html: '<span>'+sQuantity+' : '+quantity+'</span>'+sProductName});
							productBox.adopt(productName);
							
							var productTotal = new Element('p', {html: sTotal+' : <strong>'+(Math.round(iTotalPrice*100)/100)+'</strong> €'});
							productBox.adopt(productTotal);
							
							shopDiv.adopt(productBox);
							
							var button1 = new Element('a', {html:'<span>'+sOrder+'</span>'});
							button1.setProperty('class','button floatRight');
							button1.setProperty('href',transaction.cartUrl);
							shopDiv.adopt(button1);
							
							var button2 = new Element('a', {html:'<span>'+sContinue+'</span>'});
							button2.setProperty('href',"javascript:void(0);");
							button2.setProperty('class', 'button');
							shopDiv.adopt(button2);
							
							$(document.body).adopt(backgroundDiv);
							$(document.body).adopt(shopDiv);
							
							if(window.navigator.appName.search(/microsoft/gi) != -1){
								DD_roundies.addRule('div.popupBox', '10px');
							}
							
							button2.onclick = function() {
								window.location.reload();
							}
						}
						
					} 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();
	}
});
