
// SUCH FELD AUTOVERVOLLSTÄNDIGUNG
Ext.namespace('Ext.gs');

Ext.gs.SimpleSearchfield = function(config){
	if(typeof config == 'undefined'){
		config = {};
	}

	/* call parent constructor */
	Ext.gs.SimpleSearchfield.superclass.constructor.call(this, config);

	this.node 				= config.node				|| '';
	this.valueField 		= config.valueField 		|| 'value';
	this.displayField 		= config.displayField 		|| 'value';
	this.totalproperty 		= config.totalproperty 		|| 'totalcount';
	this.root 				= config.root 				|| 'items';
	this.fields 			= config.fields 			|| ['key', 'value'];
	this.url 				= config.url 				|| "/index.cfm" + '?event=ajax.getSearchSuggestions';
	this.events				= ['searchtermchanged'];
	this.searchterm			= '';
	this.hideTrigger 		= config.hideTrigger 		|| false;
	this.queryDelay 		= config.queryDelay 		|| 150;
	this.minChars			= config.minChars 			|| 3;
	this.width 				= config.width 				|| 300;
    this.typeAhead			= false;
//    this.loadingText		= getLanguageText('Suggestions.Message.Wait');
    this.loadingText		= 'Generierung der Vorschl&auml;ge...';

	this.enableKeyEvents 	= config.enableKeyEvents 	|| true;
	this.editable 			= true;
	this.rawValue			= config.rawValue || '';

	/* Store der ComboBox */
	this.store = new Ext.data.JsonStore({
		url: this.url,
		root: this.root,
		remoteSort: true,
		totalProperty: this.totalproperty,
		autoLoad: false,
		fields: this.fields
	})

	this.handleTriggerClass = function(){
		if(this.searchterm == ''){
			this.trigger.removeClass('x-form-search-trigger');
			this.trigger.removeClass('x-form-clear-trigger');

			this.trigger.addClass('x-form-search-trigger');

		}else{
			this.trigger.removeClass('x-form-search-trigger');
			this.trigger.removeClass('x-form-clear-trigger');

			this.trigger.addClass('x-form-clear-trigger');
		}
	}

	this.setAndFire = function(){
			
		this.rawValue = this.el.dom.value;
		this.searchterm = this.rawValue;
		this.value = this.searchterm;

		this.handleTriggerClass();
		this.fireEvent('searchtermchanged');
		this.store.removeAll();
		this.collapse();
	}

	/* Rueckgabe des Suchbegriffs */
	this.getSearchTerm = function(){
		return(this.searchterm);
	}

	/* Key-Handler */
	this.on('keydown', function(cb, e){
		if (e.keyCode == 13) {
			this.setAndFire();
			this.store.removeAll();
			this.collapse();
		}
		
		if (e.keyCode == 40){
			var idx = parseInt(cb.selectedIndex+1)
			if(typeof cb.store.data.items[idx] != 'undefined'){
				cb.store.data.items[idx].data.value = cb.store.data.items[idx].data.key;
				var val = cb.store.data.items[idx].data.value;
				cb.setRawValue(val);
				cb.setValue(val);
			}
		}
		
		if (e.keyCode == 38){
			var idx = parseInt(cb.selectedIndex-1)
			if(typeof cb.store.data.items[idx] != 'undefined'){
				cb.store.data.items[idx].data.value = cb.store.data.items[idx].data.key;
				var val = cb.store.data.items[idx].data.value;
				cb.setRawValue(val);
				cb.setValue(val);
			}
		}
		
	}, this);

	this.on('render', function(cb, e){
		this.trigger.addClass('x-form-search-trigger');
	}, this)
	this.on('beforeselect', function(cb, record, index){
		var idx = parseInt(index);
		cb.store.data.items[idx].data.value = cb.store.data.items[idx].data.key;
		var val = cb.store.data.items[idx].data.value;
		cb.setValue(val);		
	}, this);
};

/* Extend */
Ext.extend(Ext.gs.SimpleSearchfield, Ext.form.ComboBox, {
    onLoad: function(){
		if (!this.hasFocus) {
			return;
		}
		if (this.store.getCount() > 0) {
			this.expand();
			this.restrictHeight();
		}
		this.rawValue = this.el.dom.value;
	},
	onTriggerClick: function(){
		if(this.searchterm != ''){
			this.el.dom.value = '';
			this.searchterm = '';
		}
		this.handleTriggerClass
		this.setAndFire();	
	}
});


function doFormPanel(i) {
	
	if (document.getElementById("search_input" + i)) {		
		searchForm = new Ext.FormPanel({
			renderTo: 'search_input' + i,			
			items: [			
			new Ext.gs.SimpleSearchfield(
			{
				id: 'searchterm' + i,
				hideLabel: true,
				value: search_value,
				name: 'searchterm',
				ctCls: 'input_box'
			})
			],
			keys: {
				key: [10, 13],
				fn: function() {
					doSearch(i);
				}
				
			}
		});
	
	};
};

function doSearch(i) {
	if(document.getElementById('searchterm' + i))
		location.href='/?cid='+cid_sitesearch+'&searchterm='+document.getElementById('searchterm' + i).value;
}