/*
 * Ext JS Library 1.1 RC 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://www.extjs.com/license
 */

Ext.onReady(function(){

    var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: './lib/ajax/ajax_artikel.php'
        }),
        reader: new Ext.data.JsonReader({
            root: 'artikel',
            totalProperty: 'totalCount',
            searchtext: 'search',		
            id: 'artikel_id'
        }, [
            {name: 'title', mapping: 'titel'},
            {name: 'artikelId', mapping: 'artikel_id'},
            {name: 'kategorie', mapping: 'kategorie'},
            {name: 'titelhtml', mapping: 'titelhtml'},
            {name: 'kategoriehtml', mapping: 'kategoriehtml'}
        ])
    });

    // Custom rendering Template
    var resultTpl = new Ext.Template(
        '<div class="search-item">',
            '<b><span>{title}</span></b>({kategorie})',
        '</div>'
    );
    
    var search = new Ext.form.ComboBox({
        store: ds,
        displayField:'title',
        typeAhead: false,
        loadingText: 'Suche...',
        width: 260,
        pageSize:10,
        hideTrigger:false,
        tpl: resultTpl,
        onSelect: function(record){ // override default onSelect to do redirect
            window.location =
                String.format('http://www.diabetesindex.de/diabetesinfo.d/k_/s_{0}/{1}.html', record.data.kategoriehtml,record.data.titelhtml);
        }
    });
    // apply it to the exsting input element
    search.applyTo('search');
});
