/**
 * => Mootools 1.2
 * 	=> Core
 * @classDescription
 * Classe représentant le panier (box) du client
 * @author M@nu/Baphira
 */
var ShoppingCartBox = new Class({

	EMPTY_CLASS: 'cartEmpty', // classe du premier élément de la liste du panier pour indiquer que le panier est vide

	initialize: function(idShoppingCart, cartEmptyMsg){
		this.cart = $(idShoppingCart);
		this.cartEmptyMsg = cartEmptyMsg;
		
		this.cart.ul = this.cart.getElement('ul');
		/**
		 * Structure XHTML d'un produit (x qty) dans le panier :
		 * <li id="idProductN"><span>qty</span> x <a href="href">productName</a></li>
		 */
		this.updateProductList();
		var cartTotal = this.cart.getElement('.cart-total');
		if($chk(cartTotal)) {
			this.total = this.cart.getElement('.cart-total').getFirst();
		}
		var cartCount = this.cart.getElement('.cart-count');
		if ($chk(cartCount)) {
			this.counter = this.cart.getElement('.cart-count').getElement('span');
		}
    },
	
	updateProductList: function() {
		this.products = this.cart.ul.getChildren();
	},
	
	isEmpty: function() {
		return this.products[0].hasClass(this.EMPTY_CLASS);
	},
	
	/**
	 * Mets à jour le panier
	 * @param {Object} productInfos
	 * {
	 * 	idProduct: '',
	 * 	href: '',
	 * 	qty_sum: '',
	 * 	qty: '',
	 * 	productName: '',
	 * 	productSum: '',
	 * 	total: ''
	 * }
	 */
	updateCart: function(productInfos) {
		if(productInfos.qty <= 0) {
			this.removeProduct(productInfos.idProduct);
		} else {
			this.addOrUpdateProduct(productInfos);
		}
		if ($chk(this.counter)) {
			this.counter.set('text', productInfos.qty_sum);
		}
		if($chk(this.total)) {
			this.total.set('html', productInfos.total);			
		}
	},
	
	addOrUpdateProduct: function(productInfos) {
		var product = this.getProduct(productInfos.idProduct);
		if (product == null) {
			this.addProduct(productInfos);
		} else {
			product.getElement('span').set('text', productInfos.qty);
		}
	},
	
	addProduct: function(productInfos) {
		if(this.isEmpty()) {
			this.cart.ul.empty(); //empty msg removed
		}
		var newProduct = new Element('li', {'id': 'idProduct' + productInfos.idProduct});
		newProduct.set('html', '<span>'+productInfos.qty+'</span> x <a href="'+productInfos.href+'">'+productInfos.productName+'</a>');
		this.cart.ul.adopt(newProduct);
		this.updateProductList();
	},
	
	removeProduct: function(idProduct) {
		var product = this.getProduct(idProduct);
		if(product != null) {
			product.destroy();
			this.updateProductList();
			if(this.products.length <= 0) {
				this.clearCart();
			}
		}
	},
	
	getProduct: function(idProduct) {
		for(var i = 0; i < this.products.length; i++) {
			var product = this.products[i]; 
			var currentId = product.id.replace('idProduct','');
			if(idProduct == currentId) {
				return product;
			}
		}
		return null;
	},
	
	getProductQty: function(idProduct, attributes) {
		if($chk(attributes)) {
			attributes.each(function(el) {
				if(el.idAttrValue > -1) {
					idProduct += '{'+el.idAttr+'}'+el.idAttrValue;					
				}
			});
		}
		var product = this.getProduct(idProduct);
		if (product != null) {
			return product.getElement('span').get('text').toInt();
		}
		return 0;
	},
	
	clearCart: function() {
		this.cart.ul.set('html', '<li class="'+this.EMPTY_CLASS+'">'+this.cartEmptyMsg+'</li>');
		this.updateProductList();
	}
});