Tx_Mevshop_Product.Configurable = new Class({

	Extends: Tx_Mevshop_Product,

	/**
	 * Constructor
	 */
	initialize: function(element, options) {
		this.parent(element, options);

		if(this.element) this.element.getElement('td.submit input[name*="addList"]').destroy();
		this.options.guiElements.amount.hide();
		this.options.guiElements.toCart.setStyle('visibility', 'hidden');
	},


	/**
	 * Initialisiert den Validator für die Formulare, wird in this.getConfigurationInterface der erbenden Klassen aufgerufen.
	 * @return void
	 */
	initFormValidator:function () {
		var self = this;

		this.validator = new Form.Validator(this.configurationInterface, {
			onElementFail: function(testedElement, failedValidatorsArray) {
				failedValidatorsArray.each(function(failedValidator){
					var pos = testedElement.getPosition();
					var size = testedElement.getSize();
					var posArray = [pos.x + size.x, pos.y - size.y];

					if ('qmValidator' === failedValidator) {
						new Message({
							iconPath: tx_mevshop_div.options.extPath + 'res/img/jslib/Message/',
							icon: 'cautionMedium.png',
							title: 'Hinweis',
							message: this.validators[failedValidator].getError(testedElement)
						}).tell();
					} else {
						Tip(
							this.validators[failedValidator].getError(testedElement),
							FIX, posArray,
							CLICKCLOSE, true
						);
					}
				}.bind(this));
			}
		});

		this.validator.addAllThese([
			['breiteValidator', {
				errorMsg: 'Bitte geben Sie eine Breite im Bereich ' + this.data.breiteMin + ' bis ' + this.data.breiteMax + ' an.',
				test: function(element) {
					var numVal = element.value.toInt();
					return numVal >= this.data.breiteMin && numVal <= this.data.breiteMax;
				}.bind(this)
			}],
			['laengeValidator', {
				errorMsg: 'Bitte geben Sie eine Länge im Bereich ' + this.data.laengeMin + ' bis ' + this.data.laengeMax + ' an.',
				test: function(element) {
					var numVal = element.value.toInt();
					return numVal >= this.data.laengeMin && numVal <= this.data.laengeMax;
				}.bind(this)
			}],
			['qmValidator', {
				errorMsg: this.lang.get('qmValidatorErrorMsg', this.data.qmMax),
				test: function(element) {
					var numVal = element.value.toInt();
					var laenge = this.configurationInterface.getElement('input.breiteValidator').get('value').toInt();
					var breite = this.configurationInterface.getElement('input.laengeValidator').get('value').toInt();
					var qm = laenge * breite / 1000000 * numVal;

						// wenn keine qm gesetzt sind, als Akzeptiert definieren
					if (isNaN(qm)) return true;
					return this.data.qmMax > qm;
				}.bind(this)
			}]
		]);


	},


	/**
	 * wird gefeuert, wenn das "innere" Preis-Display angeklickt ist, in welchem ggf. auch "konfigurieren" usw. steht.
	 */
	pseudoPriceDisplayClicked: function() {
		if (this.getAmount() && this.validator.validate()) {
			this.refreshProductPrice();
		}
	},


	/**
	 * Teilegruppe
	 * @return String F oder G o.ä.
	 */
	getTeileGruppe: function() {
		return this.options.tgr;
	},


	/**
	 * Gibt die Konfigurations-Werte als Key/Value zurück
	 * @return Hash
	 */
	getConfigurationOptions: function() {
		var ret = new Hash();
		this.configurationInterface.getElements('input.txtBox[name], select[name]').each(function(item){
			ret.set(item.name, item.value);
		});

		return ret;
	},


	/**
	 * zeigt das Konfig-Interface.
	 * @param integer asOverlayPosition Falls gesetzt, wird es als Overlay an der gewünschten Pos. von Oben dargestellt
	 */
	showConfigurationInterface: function(asOverlayPosition) {
		var configurationInterface = this.getConfigurationInterface();

		if (asOverlayPosition) {
			configurationInterface.setStyles({
				position: 'absolute',
				'z-index': 3010,
				width: 750,
				top: asOverlayPosition,
				left: 170
			}).inject(document.body);
		} else {
				// wir gehen davon aus, dass es sich um eine Tabellenzelle handelt wie in der Produktliste
			if (!configurationInterface.getParent('td.configInterface')) {
				var colspan = this.element.getElements('td').length;
				var fakeRow = new Element('tr').grab(new Element('td', {'class':'configInterface', colspan:colspan,style:'padding:0;'}).grab(configurationInterface));
				fakeRow.inject(this.element, 'after');
				configurationInterface.slide('hide')
			}

			configurationInterface.slide('in').getParent('td.configInterface').setStyle('border-bottom','2px solid #E8E8E8');
		}

		this.configInterfaceShown = true;
	},


	/**
	 * Interface ausblenden
	 * @return void
	 */
	hideConfigurationInterface: function() {
		this.configurationInterface.slide('out').get('slide').chain(function(){
			this.configurationInterface.getParent('td.configInterface').setStyle('border-bottom','0 none');
		}.bind(this));

		this.configInterfaceShown = false;
	},


	/**
	 * Die Zelle für Konfigurieren bzw. Schließen etc. wurde geklickt.
	 * //Notiz: mit this.validator.validate() wird das Formular validiert.
	 * @return void
	 */
	itemPriceCellClick: function() {
		if (!this.configInterfaceShown) {
			this.showConfigurationInterface();
		} else {
			this.hideConfigurationInterface();
		}

		this.setInteractionItemsState();
	},


	/**
	 * @return void
	 */
	addToCart: function() {
		if (this.validator && this.validator.validate()) {
			this.parent();
		}
	},


	/**
	 * Setzt die Zellen für Menge, Preis Pro Stück, Warenkorb usw. je nach Situation
	 * @return void
	 */
	setInteractionItemsState: function() {
		this.options.guiElements.toCart.setStyle('visibility', 'hidden');
		this.options.guiElements.itemPriceCell
				.setStyle('cursor', 'pointer')
				.set('html', MooTools.lang.get('Tx_Mevshop_Product', (this.configInterfaceShown ? 'closeConfigInterfaceLabel' : 'pleaseConfigureLabel')));

		this.options.guiElements.toCart.setStyle('visibility', (this.getAmount() ? 'visible' : 'hidden'));

		if (this.configurationInterface) {
			this.configurationInterface.getElement('.priceDisplay')
				.setStyle('cursor', this.getAmount() ? 'pointer' : 'default')
				.set('html', MooTools.lang.get('Tx_Mevshop_Product', (this.getAmount() ? 'requestPriceLabel' : 'fillInQuantitiyLabel')));

			if (this.fetchingProductData) {
				this.configurationInterface.addClass('fetchingProductData')
			} else {
				this.configurationInterface.removeClass('fetchingProductData')
			}
		}


	},



	/**
	 * Gibt die Cart-Optionen zurück
	 * @return Object
	 */
	getCartAddOptions: function(){
		var ret = this.parent();

		ret['configuration'] = this.getConfigurationOptions().toQueryString();

		return ret;
	},


	/**
	 * Gibt die Optionen für die Online-Preisanfrage zurück
	 * @return Object
	 */
	getProductPriceOptions: function() {
		var ret = this.parent();

		ret['configuration'] = this.getConfigurationOptions().toQueryString();

		return ret;
	}
});
