/*
 * Ext JS Library 2.0 RC 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
var ext_search_obj = null;
Ext.onReady(function(){

    var ds = new Ext.data.Store({
        proxy: new Ext.data.ScriptTagProxy({
            url: 'http://localhost:8000/webservice/searchpages/json/'
            //url: 'http://www.pagechat.com/webservice/searchpages/json/'
        }),
        reader: new Ext.data.JsonReader({
            root: 'pages',
            totalProperty: 'total_count',
            id: 'page_id'
        }, [
            {name: 'username', mapping: 'username'},
            {name: 'title', mapping: 'title'},
            {name: 'content', mapping: 'content'},
            {name: 'created_date', mapping: 'created_date'},
            {name: 'permalink', mapping: 'permalink'}
        ])
    });

    // Custom rendering Template
    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="search-item">',
            '<h3><span style="width:auto;">by {username} ({created_date})</span>{title}</h3>', //
            '{content}',
        '</div></tpl>'
    );
    
    var search = new Ext.form.ComboBox({
        store: ds,
        displayField:'title',
        typeAhead: false,
        loadingText: 'Searching...',
        width: 450,
        pageSize:5,
        hideTrigger:true,
        tpl: resultTpl,
        applyTo: 'id_name',
        itemSelector: 'div.search-item',
        onSelect: function(record){ // override default onSelect to do redirect
			document.getElementById('id_name').value = record.data.title;
            window.location =
                String.format('http://localhost:8000/p/{0}', record.data.permalink);
                //String.format('http://www.pagechat.com/p/{0}', record.data.permalink);
        }
    });

	ext_search_obj = search;
});

