// Transforms am input element into a combo box with autocompletion
//
// TODO: - Will be finished later on. For now it symply presents a popup button. So it is not possible to enter free text.
//
var PWComboBox = new Class({
	
	Implements: [Options],
	
	options: {
        predefinedValues:[],
        selectedValue:null,
        inputWidth:0
	},
	    
	initialize: function(input, options, inputWidth){
		this.setOptions(options);
		this.input = input;
        var selectedValue = this.options.selectedValue;
        if(selectedValue)
            input.value = selectedValue;
//        input.setStyle('position', 'absolute');
//        input.setStyle('width', (this.options.inputWidth-15)+'px');
//        input.setStyle('left', '0px');
        var values = this.options.predefinedValues;
        if(values.length > 0){
//            select = $(SELECT({'style':'width:'+(this.options.inputWidth+6)+'px;'}));
            select = $(SELECT({'style':'width:100%;'}));
            values.each(function(value){
                var option = $(OPTION({'value':value}, value));
                if(value == selectedValue)
                    option.setProperty('selected', 'true');
                select.appendChild(option);
            }, this);

            // For now we only show the popup button
            select.inject(input, 'before');
            input.setStyle('display','none');
            
            this.select = select;
            this.connectInputWithSelect();
        }
	},
    
    connectInputWithSelect: function(){
        this.select.addEvent('change', function(){
            this.input.value = this.select.value;
        }.bind(this));
    },
    
    setDisabled: function(disabled){
        if(this.select)
            this.select.disabled = disabled;
    }
});
