/*
 * 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';
	Ext.util.CSS.createStyleSheet('.x-grid3-cell-inner {margin-right: 2px}', '.x-grid3-cell-inner');
	
	/* Make sure you run qstoarr.js */
	/* Define the strView to be used by Ektron ID */
	var strView = "EMTX_";
	if (qsParm['id'] == '5218') {
		strView = "EMTX_";
	} else if (qsParm['id'] == '5220') {
		strView = "EMTXn_";
	} else if (qsParm['id'] == '5216') {
		strView = "EMTX_AG_";
	} else if (qsParm['id'] == '5264') {
		strView = "EMTX_FR_";
	} else if (qsParm['id'] == '5262') {
		strView = "EMTX_DE_";
	} else if (qsParm['id'] == '5226') {
		strView = "EMTX_IT_";
	} else if (qsParm['id'] == '5260') {
		strView = "EMTXC_";
	} else if (qsParm['id'] == '8382') {
		strView = "EMTXC_DE_";
	} else if (qsParm['id'] == '5222') {
		strView = "EMTXC_FR_";
	} else if (qsParm['id'] == '5266') {
		strView = "EMTXC_UK_";
	} else if (qsParm['id'] == '5268') {
		strView = "EMTXC_ES_";
	} else if (qsParm['id'] == '5270') {
		strView = "EMTXC_IE_";
	}
	
	// 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;
    }

	var eireader = new Ext.data.JsonReader({
		root: 'data',
		totalProperty: 'recordcount',
		id: 'intrecordid',
		fields: ['intrecordid', 'strindex', 'thisdatetime', 'strlabel', 'indexvalue', 'duration', 'modduration', 'convexity', 'avgcouponrate', 'avgmaturity', 'avgyield', 'dailypricechange', 'dailyyieldchange']
		});
	
	var homecolumns = new Ext.grid.ColumnModel([{
			header: "Index",
			dataIndex: 'strlabel', 
			width: 50
		},{
           header: "Value",
           dataIndex: 'indexvalue',
           align: 'right',
		   width: 60
        },{
           header: "Duration",
           dataIndex: 'duration',
           align: 'right',
		   width: 60
        },{
           header: "Mod Duration",
           dataIndex: 'modduration',
           align: 'right',
		   width: 75
        },{
           header: "Convexity",
           dataIndex: 'convexity',
           align: 'right',
		   width: 60
        },{
           header: "Coup Rate",
           dataIndex: 'avgcouponrate',
           align: 'right',
		   width: 60
        },{
           header: "Maturity",
           dataIndex: 'avgmaturity',
           align: 'right',
		   width: 60
        },{
           header: "Yield",
           dataIndex: 'avgyield',
           align: 'right',
		   width: 60
        },{
           header: "Price Chg",
           dataIndex: 'dailypricechange',
           align: 'right',
		   renderer: change,
		   width: 60
        },{
           header: "Yield Chg",
           dataIndex: 'dailyyieldchange',
           align: 'right',
		   renderer: change,
		   width: 60
        }]);
		
	homecolumns.defaultSortable = true;
	
	var eiviewconfig = {forceFit:false, enableRowBody:true};
	
	//BEGIN
	var proxy1 = new Ext.data.ScriptTagProxy({
		url: 'http://data.investinginbondseurope.org/iibeu-site/actGetMTSNextAsJSON.cfm',
		method: 'GET'
	});
	
	proxy1.on('beforeload', function(p, params) {
		params.strView = strView;
	});
	
	var store1 = new Ext.data.Store({
		proxy: proxy1,
        reader: eireader
    });

	// 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') + ' CET';
		var tb = new Ext.Toolbar({
			items: [{id: 'lastUpdated', xtype: 'tbtext', text: lu}]
			});
			
		var grid1 = new Ext.grid.GridPanel({
	        el: 'see_data',
	        autoWidth: true,
			autoHeight: true,
	        store: store1,
	        cm: homecolumns,
	        loadMask: true,
	        viewConfig: eiviewconfig,
			bbar: tb
	    });
	
	    // render it
	    grid1.render();
	});
	//END
	
});
