/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

var kbsearchds = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: 'index.php?fuse=knowledgebase&view=KB_AutoSuggestion_JSON'
    }),
    baseParams:{category: "0",ispublic:"1",timesent:"0"},
    reader: new Ext.data.JsonReader({
        root: 'topics',
        totalProperty: 'totalCount',
        id: 'post_id'
    }, [
        {name: 'title', mapping: 'topic_title'},
        {name: 'topicId', mapping: 'topic_id'},
        {name: 'author', mapping: 'author'},
        {name: 'lastPost', mapping: 'post_time'},
        {name: 'excerpt', mapping: 'post_text'}
    ])
});

function UpdateStoreParam(obj,activeCategoryId){
    
    //set default to the category we are in
    var searchcategoryid=activeCategoryId;
    if(obj.checked) searchcategoryid=0;

    var combo = Ext.getCmp('kbarticlesearch');

    var tQuery = $('search').value;
    var currentTime = new Date()
    kbsearchds.removeAll();
    kbsearchds.baseParams = {category: searchcategoryid,ispublic:"1",timesent:currentTime.getTime()};
    kbsearchds.load({params:{start:0, limit:10, query:combo.getValue()}});
    //combo.setValue("");
    combo.focus();

}


Ext.onReady(function(){
    // Custom rendering Template
    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="search-item">',
            '<h3><span>{lastPost}</span>{title}</h3>',
            '{excerpt}',
        '</div></tpl>'
    );
    
    var search = new Ext.form.ComboBox({
        store: kbsearchds,
        displayField:'title',
        typeAhead: false,
        loadingText: 'Searching...',
        width: 540,
        id: "kbarticlesearch",
        pageSize:10,
        hideTrigger:true,
        tpl: resultTpl,
        applyTo: 'search',
        itemSelector: 'div.search-item',
        onSelect: function(record){ // override default onSelect to do redirect
            var combo = Ext.getCmp('kbarticlesearch');
            var searchstring = combo.getValue();
            window.location =
                String.format('index.php?fuse=knowledgebase&view=KB_viewArticle&articleId={0}&recordid={1}&public=1&searchstring={2}', record.id, record.data.topicId, searchstring);
        }
    });
});