/**
 *
 * Paul Streise
 * http://www.allbase.de/
 *
 * Created on 19.02.2009
 * @copyright 2008
 *
 **/

Autocompleter.IATA = new Class({

	Extends: Autocompleter.Request.JSON,

	options: {
		forceSelect: true,
		selectMode: false,
		selectFirst: true
	},

	markQueryValue: function(str) {
		str = '<strong>'+str.name+'</strong><br />'+str.code+', '+str.country;
		
		return (!this.options.markQuery || !this.queryValue) ? str
			: str.replace(new RegExp('(' + ((this.options.filterSubset) ? '' : '^') + this.queryValue.escapeRegExp() + ')', (this.options.filterCase) ? '' : 'i'), '<span class="autocompleter-queried">$1</span>');
	},

	
	setSelection: function(finish) {
		var input = this.selected.inputValue, value = input;
		var start = this.queryValue.length, end = input.length;
		if (input.name.substr(0, start).toLowerCase() != this.queryValue.toLowerCase()) start = 0;
		var selectedVal = value.airport;
		this.observer.setValue(selectedVal);
		this.opted = value.code;
		if (finish || this.selectMode == 'pick') start = end;
		this.element.selectRange(start, end);
		this.fireEvent('onSelection', [this.element, this.selected, selectedVal, selectedVal]);
	},
	
	hideChoices: function(clear) {
		this.observer.resume();
		if (clear) {
			var value = this.element.value;	
			if (this.options.autoTrim) {
				value = value.split(this.options.separatorSplit).filter($arguments(0)).join(this.options.separator);
			}
			
			if(!value)
			{
				this.observer.clear();
				return;
			}
			this.observer.setValue(value);
			
		}
		if (!this.visible) return;
		this.visible = false;
		if (this.selected) this.selected.removeClass('autocompleter-selected');
		this.observer.clear();
		var hide = function(){
			this.choices.setStyle('display', 'none');
			this.fix.hide();
		}.bind(this);
		if (this.fx) this.fx.start(0).chain(hide);
		else hide();
		this.fireEvent('onHide', [this.element, this.choices]);
	}

});
