/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.onReady(function(){

    var ds = new Ext.data.Store({
        proxy: new Ext.data.ScriptTagProxy({
            url: 'include/lib/xmler/jsoner_professor.php'
        }),
		reader: new Ext.data.JsonReader({
            root: 'results',
            totalProperty: 'number_records',
			id: 'id'
			
        }, [
            {name: 'id'},
            {name: 'database'},
            {name: 'record_id'},
            {name: 'description'}
        ])
    });

    // Custom rendering Template
    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="search-item">',
            '<table><tr><td  class="searchKey">{database} {record_id}</td><td>',
            '{description}',
        '</td></tr></table></div></tpl>'
    );
    
    var search = new Ext.form.ComboBox({
        store: ds,
        displayField: 'id',
        typeAhead: false,
        loadingText: 'Searching...',
        pageSize:0,
        hideTrigger:true,
        tpl: resultTpl,
        applyTo: 'quickquery',
        minChars: 3,
		itemSelector: 'div.search-item',
        onSelect: function(record){ // override default onSelect to do redirect
            var currentQuery = Ext.getDom('quickquery').value;
			if(currentQuery.lastIndexOf("[")>-1) {
				currentKey = currentQuery.substring(currentQuery.lastIndexOf("[")+1,currentQuery.lastIndexOf("]"));
				currentQuery = currentQuery.substring(0,currentQuery.lastIndexOf("["));
				
				if(currentKey.toUpperCase()=='OR')
					record.id = "[OR]" + record.id.substring(record.id.indexOf("]")+1);  
			 } else
				currentQuery = '';
				
			Ext.getDom('quickquery').value = currentQuery + record.id;
        }
    });
	

});