/**
 * Konfigurator JavaScript Functions
 * needs prototype.js
 */

c = 0;
sc = 0;



function Warenkorb () {
	this.anzeigeKleinURL = "../konfigurator/warenkorb_klein.xml";
	this.anzeigeGrossURL = "../konfigurator/warenkorb_gross.xml";
	var that = this;
	/**
	 * aktualisiert die Anzeige der Warenkorbseite
	 */
	this.updateWKGross = function () {
		new Ajax.Updater("standkonfiguration", 
				this.anzeigeGrossURL, 
				{
					parameters: {
						locale: locale,
						bk: bk, 
						c:c,
						sc:sc
					}, 
					evalScripts: true
				}
		);
	};
	
	/**
	 * aktualisiert die Anzeige des kleinen Warenkorbs
	 */
	this.updateWKKlein = function () {
		var optionen = {
			parameters: {
				locale: locale,
				bk: bk, 
				c:c,
				sc:sc
			},
			evalScripts: true
		}
		new Ajax.Updater("warenkorbKlein", this.anzeigeKleinURL, optionen);
	};

	this.add = function (artikelID, menge) {
		$('ampel_status_' + artikelID).addClassName("ampel_wait");
		var optionen = {
				parameters: {
					id: artikelID,
					menge: menge,
					artikel: "add",
					locale: locale,
					bk: bk, 
					c:c,
					sc:sc
				},
				requestHeaders: {
					Accept: 'application/json'
				},
				onSuccess: this.addResponse
			};
		new Ajax.Request ("actionArtikel.do", optionen);
	}

	this.addResponse = function (transport) {
		var json = transport.responseText.evalJSON(true);
		$('ampel_status_' + json.id).removeClassName("ampel_wait");
		$('ampel_text_' + json.id).hide();
		if (json.status == "ok") {
			// Text Artikel wurde hinzugefügt anzeigen
			$('ampel_status_' + json.id).addClassName("ampel_success");
			that.showShortly(json.id, 'added_');
			// Weiter Button 
			that.showWeiterButton();
			// kleinen Warenkorb aktualisieren
			that.updateWKKlein();
			// show wk klein
			that.showWkKlein();
		} else if (json.status = "no-vfb"){
			// Fehlermeldung anzeigen
			$('ampel_status_' + json.id).addClassName("ampel_failure");
			alert(json.text);
			that.showShortly(json.id, 'not_added_');
			// Reload Window
			window.location.reload();
		}
	};
	
	this.change = function (artikelID, menge) {
		var optionen = {
				parameters: {
					id: artikelID,
					menge: menge,
					locale: locale,
					artikel: "change",
					bk: bk, 
					c:c,
					sc:sc
				},
				requestHeaders: {
					Accept: 'application/json'
				},
				onSuccess: this.changeResponse
		};
		new Ajax.Request ("actionArtikel.do", optionen);
	};
	
	this.remove = function (artikelID) {
		var optionen = {
				parameters: {
					id: artikelID,
					menge: 0,
					locale: locale,
					artikel: "remove",
					bk: bk, 
					c:c,
					sc:sc
				},
				requestHeaders: {
					Accept: 'application/json'
				},
				onSuccess: this.changeResponse,
				evalScripts: true
		};
		new Ajax.Request ("actionArtikel.do", optionen);
	};

	this.changeResponse = function (transport) {
		var json = transport.responseText.evalJSON(true);
		if (json.status == "ok") {
			// grossen Warenkorb aktualisieren
			if ($('standkonfiguration') != null) {
				that.updateWKGross();
			}
		} else if (json.status = "no-vfb"){
			alert(json.text);
		}
	}


	this.showWeiterButton = function () {
		$$("div.button_weiter").invoke('show');
	};

	this.showShortly = function (spanId, prefix) {
		var timeToShow = 2500;
		$(prefix + spanId).show();
		$('artikel-catalog-button_' + spanId).hide();
		window.setTimeout(
				"$('" + prefix + spanId + "').hide();" +
				"$('ampel_text_" + spanId + "').show();" +
				"$('artikel-catalog-button_" + spanId + "').show();" + 
				"$('ampel_status_" + spanId + "').removeClassName('ampel_failure');" +
				"$('ampel_status_" + spanId + "').removeClassName('ampel_success');", timeToShow);
	};

	/** kleinen WK sichtbar machen */
	this.showWkKlein = function () {
		$('warenkorbNavigation').show();
	};
	
}

