/**
 * @author M�ller Michael
 */

var selectedSearchItem = false;

Ext.onReady(function(){

	var store = new Ext.data.JsonStore({
	    url: 'ajax.php',
	    root: 'topics',  // the root of the array you'll send down
	    idProperty: 'id',
	    fields: ['title', 'id']
	});
	
	var combo = new Ext.form.ComboBox({
	    store: store,
	    id: 'id_search',
	    displayField:'title',
	    typeAhead: false,
	    mode: 'remote',
	    loadingText: 'Suche...',
	    queryParam: 'query',  //contents of the field sent to server.
	    hideTrigger: true,    //hide trigger so it doesn't look like a combobox.
	    triggerAction:'query',
	    selectOnFocus:true,
	    minChars:2,
	    x: 0,
	    pageX: 0,
	    width: 732,
	    valueField:'id',
	    applyTo: 'search'  //the id of the html element to render to.
	                              //Not necessary if this is in an Ext formPanel.
	});
	
	var keyMap = new Ext.KeyMap(Ext.get('search'), [
	    {
	    	key: [Ext.EventObject.ENTER], fn: function(){
	    		//Ext.getCmp('id_search').setDisplayField('Hallo');
	    		if(Ext.getCmp('id_search').getValue()!="") {
	    			if(!selectedSearchItem) {
	    				window.location.href = Ext.getCmp('id_search').getValue();
	    			}else {
	    				window.location.href = selectedSearchItem;
	    			}
	    		}
	         }
	     }
	]);
	
	Ext.getCmp('id_search').on('select', function() {
		var value = Ext.getCmp('id_search').getRawValue().replace(/<\/b>/g, "");
		Ext.getCmp('id_search').setRawValue(value.replace(/<b>/g, ""));
		selectedSearchItem = Ext.getCmp('id_search').getValue();
	});

});
