/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){
	
	Ext.BLANK_IMAGE_URL = '/ext/resources/images/default/s.gif';
	
	var eiviewconfig = {forceFit:false, enableRowBody:true, showPreview: false};
	
	// example of custom renderer function
    function change(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '</span>';
        }
        return val;
    }

    // example of custom renderer function
    function pctChange(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '%</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '%</span>';
        }
        return val;
    }

	
	//BEGIN govt_iib_usty
	var eireader1 = new Ext.data.JsonReader({
            root: 'data',
            totalProperty: 'recordcount',
            id: 'intrecordid',
            fields: ['thisdatetime', 'strissue', 'strmat', 'strbid', 'stroffer', 'stryields', 'strbidchg']
        });
	
	var homecolumns1 = new Ext.grid.ColumnModel([{
           id: 'name', 
           header: "Issue",
           dataIndex: 'strissue',
		   width: 83
        },{
           header: "Maturity",
           dataIndex: 'strmat',
           align: 'right',
		   width: 80
        },{
           header: "Bid",
           dataIndex: 'strbid',
           align: 'right',
		   width: 80
        },{
           header: "Offer",
           dataIndex: 'stroffer',
           align: 'right',
		   width: 80
        },{
           header: "Yields",
           dataIndex: 'stryields',
           align: 'right',
		   width: 80
        },{
           id: 'fldbidchg',
		   header: "Bid Chg",
		   renderer: change,
		   dataIndex: 'strbidchg',
           align: 'right',
		   width: 80
        }]);
		
	homecolumns1.defaultSortable = false;
	
	var proxy1 = new Ext.data.ScriptTagProxy({
		url: 'http://data.investinginbondseurope.org/iibeu-site/actGetUSTYAsJSON.cfm',
		method: 'GET'
	});   

    var store1 = new Ext.data.Store({
		proxy: proxy1,
        reader: eireader1
    });
	
	// trigger the data store load
    store1.load();	
	
	store1.on('load', function(){
		var dt = new Date(store1.getAt(0).data.thisdatetime);
	    var lu = 'Last Updated: ' + dt.format('d/m/Y H:i TO');
		var tb = new Ext.Toolbar({
			items: [{id: 'lastUpdated', xtype: 'tbtext', text: lu}]
			});

		var grid1 = new Ext.grid.GridPanel({
			getRowClass : function (row, index) {
				var data = row.data;
				alert(data.strbidchg);
			},            
	        el: 'govt_iib_usty',
	        autoWidth: true,
			autoHeight: true,
	        store: store1,
	        cm: homecolumns1,
	        loadMask: true,
	        viewConfig: eiviewconfig,
			bbar: tb
	    });
		
		grid1.render();

    });
	//END govt_iib_usty
	
	//BEGIN govt_iib_ustips
	var eireader2 = new Ext.data.JsonReader({
            root: 'data',
            totalProperty: 'recordcount',
            id: 'intrecordid',
            fields: ['name', 'coupon', 'maturity', 'bid', 'ask', 'bid_yld', 'ask_yld', 'thisdatetime']
        });
	
	var homecolumns2 = new Ext.grid.ColumnModel([{
           id: 'name', 
           header: "Name",
           dataIndex: 'name',
		   width: 80
        },{
           header: "Coupon",
           dataIndex: 'coupon',
           align: 'right',
		   width: 63
        },{
           header: "Maturity",
           dataIndex: 'maturity',
           align: 'right',
		   width: 100
        },{
           header: "Bid",
           dataIndex: 'bid',
           align: 'right',
		   width: 60
        },{
           header: "Ask",
           dataIndex: 'ask',
           align: 'right',
		   width: 60
        },{
           header: "Bid Yield",
           dataIndex: 'bid_yld',
		   renderer: change,
           align: 'right',
		   width: 60
        },{
           header: "Ask Yield",
           dataIndex: 'ask_yld',
		   renderer: change,
           align: 'right',
		   width: 60
        }]);
		
	homecolumns2.defaultSortable = false;

	var proxy2 = new Ext.data.ScriptTagProxy({
		url: 'http://data.investinginbondseurope.org/iibeu-site/actGetUSTIPSAsJSON.cfm',
		method: 'GET'
	});  
	
    var store2 = new Ext.data.Store({
		proxy: proxy2,
        reader: eireader2
    });
	
	// trigger the data store load
    store2.load();	
	
	store2.on('load', function(){
		var dt2 = new Date(store2.getAt(0).data.thisdatetime);
	    var lu2 = 'Last Updated: ' + dt2.format('d/m/Y H:i TO');
		var tb2 = new Ext.Toolbar({
			items: [{id: 'lastUpdated2', xtype: 'tbtext', text: lu2}]
			});

		var grid2 = new Ext.grid.GridPanel({
	        el: 'govt_iib_ustips',
	        autoWidth: true,
			autoHeight: true,
	        store: store2,
	        cm: homecolumns2,
	        loadMask: true,
	        viewConfig: eiviewconfig,
			bbar: tb2
	    });
		
		grid2.render();

    });
	
	//END govt_iib_ustips
	
});

