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

Ext.onReady(function(){

    // create the Data Store
    var store = new Ext.data.Store({
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        //ScriptTagProxy
        proxy: new Ext.data.HttpProxy({
            url: 'remote/truckload_remote.php?state=CA'
        }),

        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'loads',
            totalProperty: 'totalCount',
            id: 'load id',
            fields: [
                'orig', 'dest', 'adate', 'weight', 'type', 'backhaul', 'contact1'
            ]
        }),

        // turn on remote sorting
        remoteSort: true
    });
    store.setDefaultSort('adate', 'desc');
    
    function renderContact(value, p, record){
        return String.format(
                '<b><a href="http://www.rightnowloads.com/full-truckload-details.php?agentid={1}" target="_blank">{0}</a></b>',
                value, record.id);
    }
    
    // the column model has information about grid columns
    // dataIndex maps the column to the specific data field in
    // the data store
    var cm = new Ext.grid.ColumnModel([{
           id: 'topic', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
           header: "Origin",
           dataIndex: 'orig',
           width: 150
        },{
           header: "Destination City",
           dataIndex: 'dest',
           width: 150
        },{
           header: "Available",
           dataIndex: 'adate',
           width: 75,
           align: 'right'
        },{
           header: "Weight",
           dataIndex: 'weight',
           width: 75,
           align: 'right'
        },{
           header: "Trailer",
           dataIndex: 'type',
           width: 75,
           align: 'right'
        },{
           header: "Backhauls",
           dataIndex: 'backhaul',
           width: 70,
           align: 'right'
        },{
           id: 'Contact',
           header: "Contact",
           dataIndex: 'contact1',
           width: 100,
           align: 'right',
           renderer: renderContact
        }]);

    // by default columns are sortable
    cm.defaultSortable = true;

    var grid = new Ext.grid.GridPanel({
        el:'topic-grid',
        width:700,
        height:500,
        title:'Available Truck Loads In Nevada, NV',
        store: store,
        cm: cm,
        trackMouseOver:true,
        sm: new Ext.grid.RowSelectionModel({selectRow:Ext.emptyFn}),
        loadMask: true,
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            showPreview:false,
            getRowClass : function(record, rowIndex, p, store){
                if(this.showPreview){
                    p.body = '<p>'+record.data.excerpt+'</p>';
                    return 'x-grid3-row-expanded';
                }
                return 'x-grid3-row-collapsed';
            }
        },
        bbar: new Ext.PagingToolbar({
            pageSize: 25,
            store: store,
            displayInfo: true,
            displayMsg: 'Displaying loads {0} - {1} of {2}',
            emptyMsg: "No topics to display",
            items:[
                '-', {
                pressed: true,
                enableToggle:false,
                text: 'Show Preview',
                cls: 'x-btn-text-icon details',
                toggleHandler: toggleDetails
            }]
        })
    });

    // render it
    grid.render();

    // trigger the data store load
    store.load({params:{start:0, limit:25}});

    function toggleDetails(btn, pressed){
        var view = grid.getView();
        view.showPreview = pressed;
        view.refresh();
    }
});

