Ext.onReady(function(){
    var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'index.php?eID=json_response'
        }),
        reader: new Ext.data.JsonReader({
            root: 'rows',
            totalProperty: 'totalCount',
            id: 'i',
            resultTpl: 'tpl'
        }, [
            {name: 'detail_page_link', mapping: 'detail_page_link'},
            {name: 'title', mapping: 'title'},
            {name: 'subtitle', mapping: 'subtitle'},
            {name: 'image', mapping: 'image'},
            {name: 'uid', mapping: 'uid'}
        ])
    });
    // ds.setDefaultSort('title', 'asc');


    var resultTpl = new Ext.XTemplate(
        '<tpl for=".">',
        '<div class="search-item">',
        	'<div class="search-img">',
            '{image}',
            '</div>',
            '<div class="search-txt">',
            '{title}<p>{subtitle}</p>',
            '</div>',
            '<div style="clear:both"></div>',
        '</div>',
        '</tpl>'
    );

    var search = new Ext.form.ComboBox({
    	id:'searchinput',
        store: ds,
        minChars:2,
        displayField:'title',
        typeAhead: false,
        loadingText: 'Suche...',
        width: 450,
        pageSize:10,
		autoSelect:false,
        hideTrigger:true,
        tpl: resultTpl,
        applyTo: 'searchbox',
        itemSelector: 'div.search-item',
        onSelect: function(record){
    			window.location = String.format('/index.php{0}&uid={1}', record.data.detail_page_link, record.data.uid);
        }
    });  
    
    var addbasket = Ext.get('add_to_basket');
    if (addbasket) {
	    addbasket.on('click', function(){
	        Ext.MessageBox.alert('Warenkorb', 'Der Artikel wurde Ihrem Warenkorb hinzugefügt.');
	    });
    }
});
