/*!
 * Ext JS Library 3.2.1
 * Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.onReady(function(){

    // create the Data Store
    var store = new Ext.data.JsonStore({
        root: 'topics',
        totalProperty: 'totalCount',
        idProperty: 'threadid',
        remoteSort: true,

        fields: [
            'title', 'forumtitle', 'forumid', 'author',
            {name: 'replycount', type: 'int'},
            {name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
            'lastposter', 'excerpt', 'tposte', 'idupload'
        ],

        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        //proxy: new Ext.data.ScriptTagProxy({
            url: 'http://tadejobs.hanploi.com/cvjason.php'
      //  })
    });
    store.setDefaultSort('lastpost', 'desc');


    // pluggable renders
    function renderTopic(value, p, record){
        return String.format(
                '<b><a href="http://www.hanploi.com/cvz/{4}" target="_blank" title="Télécharger ce CV">{0}</a></b>{1}',
                value, record.data.forumtitle, record.id, record.data.forumid, record.data.idupload);
    }
    function renderLast(value, p, r){
        return String.format('<div style="text-align:right;">{0}</div>', value.dateFormat('j/m/Y, H:i'));
    }

    var grid = new Ext.grid.GridPanel({
        width:750,
        height:500,
        title:'Profils issus de la CVthèque Hanploi, reçus via Tadejobs',
        store: store,
        trackMouseOver:true,
        disableSelection:true,
        loadMask: true,

        // grid columns
        columns:[{
            id: 'topic', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: "Liste des CV",
            dataIndex: 'title',
            width: 650,
            renderer: renderTopic,
            sortable: true
        },{
            id: 'last',
            header: "Date de modification",
            dataIndex: 'lastpost',
            width: 100,
            renderer: renderLast,
            sortable: true
        }],

        // customize view config
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            showPreview:false,
            getRowClass : function(record, rowIndex, p, store){
                if(this.showPreview){
                    p.body = '<p style="margin-left:5px;">'+record.data.excerpt+'<br/>'+record.data.tposte+'</p>';
                    return 'x-grid3-row-expanded';
                }
                return 'x-grid3-row-collapsed';
            }
        },

        // paging bar on the bottom
        bbar: new Ext.PagingToolbar({
            pageSize: 25,
            store: store,
            displayInfo: true,
            displayMsg: 'Affichage des CV {0} à {1} sur {2}',
            emptyMsg: "Pas de CV à afficher",
            items:[
                '-', {
                pressed: false,
                enableToggle:true,
                text: 'Afficher le détail des profils',
               // cls: 'x-btn-text-icon details',
                toggleHandler: function(btn, pressed){
                    var view = grid.getView();
                    view.showPreview = pressed;
                    view.refresh();
                }
            }]
        })
    });

    // render it
    grid.render('topic-grid');

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