/**
 * @author George Miller
 */
Ext.onReady(function(){
	Ext.QuickTips.init();

    // create the Data Store
    var ds = new Ext.data.Store({
        // load using HTTP
        proxy: new Ext.data.HttpProxy({url: '../includes/xml_news.php'}),

        // the return will be XML, so lets set up a reader
        reader: new Ext.data.XmlReader({
               // records will have an "Item" tag
               record: 'item'
           }, [
               // set up the fields mapping into the xml doc
               // The first needs mapping, the others are very basic
               {name: 'title', mapping: 'title'},
               {name: 'description', mapping: 'description'},
			   {name: 'link', mapping: 'link'}
           ])
    });

 // pluggable renders
    function renderTopic(value, p, record){
        return String.format('<b><a href="{1}" target="_blank">{0}</a></b>', value, record.data['link']);
    }
		
	
	 var cm = new Ext.grid.ColumnModel([{
		   header: 'Recent Horse Racing Articles',
           dataIndex: 'title',
           renderer: renderTopic,
		   tooltip: 'Click to sort news items',
		   resizable: false,
		   menuDisabled: true,
		   width: 490,
		   hideable: false,
		   sortable:true
        }]);
  
  // create the grid
    var grid = new Ext.grid.GridPanel({
		renderTo: 'myGrid',
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		width: 530,
		height: 325,
        ds: ds,
        cm: cm,
		bbar: ['Total Items: <span id="numItems"></span>','->', '&copy; BBC News']
    });
	

    // trigger the data store load
    ds.load({params:{url:'includes/xml_news.php', start: 0, limit:25}});
	
	ds.on('load', function(){
		Ext.get('numItems').update(this.getTotalCount());
	}, ds);
	
});
