/**
 * @author 		Serve it V.O.F.
 * @copyright	Copyright (c) Serve it V.O.F. 2010
 */
Ext.ns("Webwinkel");
Webwinkel.Winkelmandje = Ext.extend(Ext.util.Observable, {
	el: null,
	elTotalen: null,

	/** @var Webwinkel.Control */
	objControl: null,

	/** @var Product */
	arrProducten: [],

	constructor: function(config) {
		Ext.apply(this, config || {});

		//this.addEvents();

		Webwinkel.Winkelmandje.superclass.constructor.call(this, config);

		this.el = Ext.get("divWebwinkelWinkelmandje");
		this.elTotalen = this.el.select("span.totalen").item(0);
	},

	toevoegen: function(objProduct) {
		if (!this.inWinkelmandje(objProduct.intID)) {
			this.arrProducten.push(objProduct);
			this.updateView();
		}

		var arrIDs = [];
		for (var i = 0, len = this.arrProducten.length; i < len; i++)
			arrIDs.push(this.arrProducten[i].intID);

		this.objControl.updateWinkelmandje(arrIDs);
	},

	updateView: function() {
		var intAantal = this.arrProducten.length;
		var html = "";
		if (intAantal == 0)
			html = "Geen producten";
		else {
			var fltTotaal = this.berekenTotaalPrijs();
			html = intAantal + " product" + (intAantal > 1 ? "en" : "") + " (€ " + fltTotaal + ")";
		}

		this.elTotalen.update(html);
		
		$(this.elTotalen.dom).effect("shake", {
			times: 1,
			distance: 8
		}, 125);
	},

	berekenTotaalPrijs: function() {
		var flt = 0.00;
		for (var i = this.arrProducten.length; i--;)
			flt += this.arrProducten[i].fltPrijsInclBTW;

		return flt.toFixed(2).replace(".", ",");
	},

	setWinkelmandje: function(arrProducten) {
		this.arrProducten = arrProducten;
	},

	inWinkelmandje: function(intProduct) {
		for (var i = this.arrProducten.length; i--;)
			if (this.arrProducten[i].intID == intProduct)
				return true;

		return false;
	}
});
