
Warenkorb = Class.create();

Object.extend(Warenkorb.prototype, { 
	initialize:					function() {
		this.loading = false;
		this.container_headline = null;
		this.container_inhalt = null;
		this.data = null;
	},
	
	load:		function () {
		if (this.locading) return;
		this.loading = true;
		var me = this;
		var fetcher = new Warenkorb.API('fetch', function (data) { me.handleResponseLoad(data) });
		fetcher.get();
	},
	
	handleResponseLoad:		function (data) {
		this.loading = false;
		this.data = data;
		if (this.container_headline) this._paint();
	},
	
	paint:		function () {
		this.container_headline = $('warenkorb_headline');
		this.container_inhalt   = $('warenkorb_inhalt');
		if (this.data) this._paint();
	},
	
	_paint:		function () {
		var head = this.container_headline;
		
		if (this.data.anzahl != 1) {
			head.innerHTML = "Im Warenkorb befinden sich<br> <strong>" + this.data.anzahl + " Produkte";
		} else {
			head.innerHTML = "Im Warenkorb befindet sich<br> <strong>1 Produkt";
		}
		if (this.data.anzahl > 0) {
			head.innerHTML += ':';
		}
		head.innerHTML += '</strong>';
		
		head.style.display = '';


		var inhalt = this.container_inhalt;
		var i; var html = "";
		var produkte = this.data.produkte;

		for (i = 0; i < produkte.length; i++) {
			html += '<div class="separator"><img src="/shared/pics/t.gif" width="1" height="1"></div><div class="zeile">' + produkte[i].name + '</div>';
		}
		
		inhalt.innerHTML = html;

		inhalt.style.display = '';
	},
	
	add:		function (id, menge) {
		if (this.locading) return;
		this.loading = true;
		if (!menge) {
			var o = $('stack_size');
			if (o) menge = o.value;
		}
		var me = this;
		var fetcher = new Warenkorb.API('add', function (data) { me.handleResponseAdd(data) });
//		fetcher.debug = true;
		fetcher.post({ "produkt": id, "menge": menge });
	},
	
	handleResponseAdd:		function (data) {
		this.loading = false;
		this.data = data;
		if (data && data.success) alert(unescape("Produkt wurde dem Warenkorb hinzugef%FCgt."));
		if (this.container_headline) this._paint();
	}
});

