var FrequentFearPanel = Class.create(Panel, {
    initialize: function ($super, passportReference, tridionPanelId, panelPosition) {
        $super(passportReference, tridionPanelId);
        this.panelId = 'FrequentFear';
        this.panelPosition = panelPosition;
        this.strParams = [];
		this.classPrefix = 'ffear';
    },
    doWork: function () {	
		var adultChildInput = this.panelHTML.select('[name=inputAdultChildQuantity]')[0];		
		this.__setupInput(adultChildInput);
		var ffpItem = this._getItemByType('Frequent Fear Pass');
		var ffppItem = this._getItemByType('Frequent Fear Plus Pass (Includes Fridays)');
		var ffwepItem = this._getItemByType('Frequent Fear with Express Pass');
		var ffpwepItem = this._getItemByType('Frequent Fear Plus with Express Pass');
		
		this.panelHTML.select('[id=ffp_price]')[0].update('$' + parseFloat(ffpItem.Price).toFixed(2));
		this.panelHTML.select('[id=ffpp_price]')[0].update('$' + parseFloat(ffppItem.Price).toFixed(2));
		this.panelHTML.select('[id=ffwep_price]')[0].update('$' + parseFloat(ffwepItem.Price).toFixed(2));
		this.panelHTML.select('[id=ffpwep_price]')[0].update('$' + parseFloat(ffpwepItem.Price).toFixed(2));
		
		this.panelHTML.select('[name=productVariantCatalog]').each(function (inputRadio) {
			inputRadio.onClickWithContext(this, function (event) {
				var value = inputRadio.readAttribute('value');
				var item =  this._getItemByShortType(value);
				this.panelHTML.select('[id=aepParkSelectedLabel]')[0].update(item.FrequentFearType);
				this.panelHTML.select('[id=aepPricePerPassLabel]')[0].update('$' + parseFloat(item.Price).toFixed(2));
				var totalCost = parseInt($F(adultChildInput)) * parseFloat(item.Price);
				this.panelHTML.select('[id=aepTotalCostLabel]')[0].update('$' + parseFloat(totalCost).toFixed(2));
			
				this.__validateSubmit();
				var formData = this.panelHTML.select('form')[0].serialize(true);	
				this.saveValueChanged('radio/' + this.classPrefix  + 'productVariantCatalog', formData['productVariantCatalog']);
			});
		}.bind(this));
		
		adultChildInput.observe('keyup', function (event) {
			var formData = this.panelHTML.select('form')[0].serialize(true);	
			var value = formData['productVariantCatalog'];
				var item =  this._getItemByShortType(value);
				if (item != null) {
				this.panelHTML.select('[id=aepParkSelectedLabel]')[0].update(item.FrequentFearType);
				this.panelHTML.select('[id=aepPricePerPassLabel]')[0].update('$' + parseFloat(item.Price).toFixed(2));
				var totalCost = parseInt(formData.inputAdultChildQuantity) * parseFloat(item.Price);
				this.panelHTML.select('[id=aepTotalCostLabel]')[0].update('$' + parseFloat(totalCost).toFixed(2));
				}
		} .bind(this));
		
		this.__setupAddToCart();
		this.__loadValues(['text/inputAdultChildQuantity', 'radio/productVariantCatalog']);
    },
	_getItemByType: function(type) {
		var result = null;
		this.data.each(function (item) {
			if (item.FrequentFearType == type) {
				result = item;
			}
		});
		return result;
	},
	_getItemByShortType: function (shortType) {
		var item =  null;
		switch (shortType) {
			case 'ffp':
			item = this._getItemByType('Frequent Fear Pass');
			break;
			case 'ffpp':
			item = this._getItemByType('Frequent Fear Plus Pass (Includes Fridays)');
			break;
			case 'ffwep':
			item = this._getItemByType('Frequent Fear with Express Pass');
			break;
			case 'ffpwep':
			item = this._getItemByType('Frequent Fear Plus with Express Pass');
			break;
		}
		return item;
	},
	_formatDataAndSend: function (data) {
		var item =  this._getItemByShortType(data.productVariantCatalog);
		var dataFormatted = {
			productID: item.ProductId,
			productVariantID: item.ProductVariantId,
			qty: data.inputAdultChildQuantity,
			catalogName: item.VirtualCatalog
		};
		this.passportReference.addToCart(dataFormatted);
    },
	_validateInputData: function (data) {
		var q1 = jsHelper.toInt(data.inputAdultChildQuantity);
		return (data.productVariantCatalog != undefined && (q1 > 0));
    }
});
