var ProductConfigurable = Class.create();

ProductConfigurable.prototype = {
    initialize: function(form, productId, url, selects){
        this.form = $(form);
		this.productId = productId;
		this.url = url;
		this.selects = selects;
		this.choices = $H();
		this.nextElementId = "";
		this.validator = new Validation(form, {immediate : false });
		
		Event.observe(form, 'submit', this.submit.bindAsEventListener(this));

		this.selects.each(function(select) {
			Event.observe(select.key, 'change', this.change.bind(this));
		}.bind(this));

		this.complete = this.setValues.bindAsEventListener(this);
    },

    submit : function(event){
		oEvent = window.event || event;
		
		if(!this.validator.validate() || this.selects.size() != this.choices.size()) {
			if (window.event) 
        		oEvent.returnValue = false;
    		else 
       			oEvent.preventDefault();
		} else {
			document.getElementById("product_add_to_basket").submit();
		}
    },
	
	change : function(event) {
		var element = Event.element(event);
		elementId = element.readAttribute('id');
		
		if(this.validatorRun)
			this.validator.validate();
		
		this.disableAfterElement(elementId);
		this.nextElementId = this.findNext(elementId);
		this.setChoice(elementId.match(/parameter([0-9]+)\S*/)[1], $F(elementId));
		
		if(!$F(elementId).empty() && this.nextElementId != null) {
			
			var sParams = new String();
			sParams = addPostParam(sParams, "product_id", this.productId);
			sParams = addPostParam(sParams, "parameters_name", this.choices.keys().toJSON());
			sParams = addPostParam(sParams, "parameters_value", this.choices.values().toJSON());
			sParams = addPostParam(sParams, "parameter_id", this.nextElementId.match(/parameter([0-9]+)\S*/)[1]);
		
			var request = new Ajax.Request(
				this.url,
				{
					method:'post',
					postBody: sParams,
					onComplete: this.complete
				}
			);
		}
	},
	
	setValues: function(transport) {
		
		if (transport && transport.responseText){
            try{
                response = eval('(' + transport.responseText + ')');
            }
            catch (e) {
                response = {};
            }
        }
		
		$(this.nextElementId).childElements().each(function(child) {
			child.remove();
		});
		
		if(response) {
			response = $A(response);
			
			$(this.nextElementId).insert({bottom: "<option> </option>"});
			
			response.each(function(value) {
				$(this.nextElementId).insert({bottom: "<option value='" + value + "'>" + value + "</option>"})
			}.bind(this));
			
			$(this.nextElementId).selectedIndex = 0;
		}
		
		$(this.nextElementId).enable();
	},
	
	disableAfterElement: function(elementId) {
		var disable = false;
		this.selects.each(function(select) {
			if(disable)
			$(select.key).disable();
			
			if(select.key == elementId)
			disable = true;
		});
	},
	
	setChoice: function(elementId, value) {
		var choices = $H();
		
		var add = true;
		this.choices.each(function(select) {
			if(select.key == elementId)
			add = false;
			
			if(add)
			choices.set(select.key, select.value);
		});
		
		if(!value.empty())
			choices.set(elementId, value);
		
		this.choices = choices;
	},
	
	findNext: function(elementId) {
		var keys = this.selects.keys();
		
		for(var i = 0; i < keys.length; i++) {
			if(keys[i] == elementId && typeof keys[i + 1] != undefined)
				return keys[i + 1];
		}
		
		return null;
	}
}
