Kelda.Info.BensinGrid = Ext.extend(Ext.grid.GridPanel,{
	initComponent : function() {
	
		this.defaultRenderer = function(val) {
			if(isNaN(val))
				return '-';
			return Kelda.Info.NumberFormat(val,'0.000,00/i');
		};
	
		this.bcolumns=[{
			id: 'station',
			header:keldan_lang_text('station'),
			dataIndex: 'station',
			hidden:false,
			sortable:true,
			width: 30
		},{
            id: 'region', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
        	header:keldan_lang_text('address'),
        	dataIndex: 'address',
        	align:'right',
        	width: 50,
         	hidden:false,
        	sortable:true
        },{
            id: 'priceOkt', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header:keldan_lang_text('okt95'),
            dataIndex: 'priceOkt',
            tooltip:keldan_lang_text('okt95-tt'),
            width: 20,
            hidden:false,
            renderer:function(val) {
				if(isNaN(val))
					return '-';
				return Kelda.Info.NumberFormat(val,'0.000,00/i');
			},
            sortable: true
        },{
            id: 'priceOktChg', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header:'&#916; '+keldan_lang_text('okt95'),
            tooltip:keldan_lang_text('okt95delta-tt'),
            dataIndex: 'lastPriceOkt',
            width: 20,
            hidden:false,
            renderer:function(val,p,rec) {
        		if(isNaN(val) ||  val==0)
        			return '-';
        		var chg = rec.data.priceOkt -val;		
        		if(chg<0)
        			p.attr = 'style="color:red"';
        		else if(chg > 0)
        			p.attr = 'style="color:green"';
        		return Kelda.Info.NumberFormat(chg,'0.000,00/i')+" kr.";
				
			},
            sortable: true
        },{
            id: 'priceDiesel', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            dataIndex: 'priceDiesel',
            tooltip : keldan_lang_text('diesel-tt'),
            header:keldan_lang_text('diesel'),
            align:'right',
            renderer:function(val) {
				if(isNaN(val))
					return '-';
				return Kelda.Info.NumberFormat(val,'0.000,00/i');
			},
            width: 20,
            hidden:false,
            //renderer: this.renderTopic,
            sortable: true
        },{
        	id: 'priceDieselChg', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            dataIndex: 'lastPriceDiesel',
            tooltip:keldan_lang_text('dieseldelta-tt'),
            header:'&#916; '+keldan_lang_text('diesel'),
            align:'right',
            renderer:function(val,p,rec) {
        	
				if(isNaN(val) || val==0)
					return '-';
				var chg = rec.data.priceDiesel - val;
				if(chg<0)
        			p.attr = 'style="color:red"';
        		else if(chg > 0)
        			p.attr = 'style="color:green"';
				return Kelda.Info.NumberFormat(chg,'0.000,00/i')+" kr.";
			},
            width: 20,
            hidden:false,
            //renderer: this.renderTopic,
            sortable: true
        },{
            id: 'bensindate', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header:keldan_lang_text('lastfetch'),
            align:'right',
            dataIndex: 'lastFetched',
            renderer: function(val) {
        		return val.split(" ")[1];
        	},
            //renderer:Ext.util.Format.dateRenderer('hh.mm.ss'),
            width: 30,
            hidden:true,
            //renderer: this.renderTopic,
            sortable: true
        }];
		var bgrid = this;
		this.bstore = new Ext.data.JsonStore({
	        	id:'bensin',
	            fields: [ {name:'station',type:'string'},{name:'lastFetched',type:'string'},{name:'address',type:'string'},
	            	{name:'regionName',type:'string'},{name:'priceOkt',type: 'float'},{name:'priceDiesel',type:'float'},
	            	{name:'lastPriceOkt',type:'float'},{name:'lastPriceDiesel',type:'float'},
	            	{name:'lastChangedOkt',type:'string'},{name:'lastChangeDiesel',type:'string'}
	            ],
	            proxy: new Ext.data.HttpProxy({
	            	//url: window.location.protocol+"//"+window.location.hostname+"/"+basePath+"feedconnect/getFeed"
	            	url: window.location.protocol+"//"+window.location.hostname+"/"+basePath+"connector/getBensin"
	            }),
	            baseParams : {region:bgrid.region}
	            //sortInfo:{field:'category',direction:'ASC'}
            
        });
		this.bview = new Ext.grid.GridView( {
			forceFit : true,
			autoFill:true,
            enableRowBody:true,
            scrollOffset:20,
            showPreview:false,
            getRowClass : function(record, rowIndex, p, store){
				if ((parseInt(rowIndex) % 2) == 0)
					return 'odd-row';
			},
			columnsText:keldan_lang_text('columns'),
			sortAscText:keldan_lang_text('sort ascending'),
			sortDescText:keldan_lang_text('sort descending')
		});
		Ext.apply(this,{
			id:'bensingrid_'+Math.random()*103,
    		store : this.bstore,
    	    columns:this.bcolumns,
    	    view:this.bview
    	});
		
		this.on('mouseover',function(e,t){
			if( (row = this.getView().findRowIndex(t))!== false && (col=this.getView().findCellIndex(t))!==false) {
				var rec = this.getStore().getAt(row);
				var htmlel = this.getView().getCell(row,col);
				var header = this.getColumnModel().getColumnHeader(col);
				var html = '';
				if(header.indexOf('95') >= 0) {
					html = '<pre><b>Síðast breytt : '+rec.data.lastChangedOkt+'. Síðasta verð : '+rec.data.lastPriceOkt+'</b></pre>';
					var tt1 = new Ext.ToolTip({
						target:htmlel,
						html:html,
						//cls:'fx-tooltip',
						//ctCls:'ct-fx-tooltip',
						showDelay:0,
						hideDelay:0,
						trackMouse:true,
						frame:true,
						dismissDelay:0
					});
				}else if(header.indexOf(keldan_lang_text('diesel'))>=0) {
					html = '<pre><b>Síðast breytt : '+rec.data.lastChangeDiesel+'. Síðasta verð : '+rec.data.lastPriceDiesel+'</b></pre>';
					var tt1 = new Ext.ToolTip({
						target:htmlel,
						html:html,
						//cls:'fx-tooltip',
						//ctCls:'ct-fx-tooltip',
						showDelay:0,
						hideDelay:0,
						trackMouse:true,
						frame:true,
						dismissDelay:0
					});
				}
					
				
				
				
			}
		});
    	
    	var s = this.bstore;
    	
    	this.btask = {
	        	run:function() {
    				//s.load({params:{feedName:'rsfaf',type:t}});
    				s.load();
				},
				interval: 600000
		};
    	
		this.bstore.on('load',function(){
			if(Ext.Msg.isVisible()) {
				keldan_store_count--;
				Ext.Msg.updateProgress((keldan_fixed_stcount-keldan_store_count)/keldan_fixed_stcount
						,keldan_fixed_stcount-keldan_store_count+(keldan_lang=='is' ? ' af ' : ' of ')+keldan_fixed_stcount);
				if(keldan_store_count==0) {
					Ext.Msg.hide();
					keldan_store_count=-1;
				}
			}
		});
    	
    	
    	this.on('render',function(comp) {
    		Ext.TaskMgr.start(this.btask);
    	});
    	this.on('destroy',function(){
    		Ext.TaskMgr.stop(this.btask);
    	});
    	
    	Kelda.Info.BensinGrid.superclass.initComponent.apply(this,arguments);
	},
	 stopPoll : function() {
		Ext.TaskMgr.stop(this.btask);
	},
	startPoll : function() {
		Ext.TaskMgr.start(this.btask);
	}
});

Ext.reg('BensinPortlet_Child',Kelda.Info.BensinGrid);

Ext.reg('BensinPortlet_Child',Kelda.Info.BensinGrid);
Kelda.Info.BensinPortlet = Ext.extend(Ext.Panel,{
	title: '',
	layout:'fit',
	plugins: [Ext.ux.PortletPlugin],
    closeable: true,
    maximizable:false,
    settings: false,
    resizeable:true,
    collapsible:true,
    version:1.0001,
    nohelp:true,
    width:400,
    height:290
    
});
Ext.reg('BensinPortlet',Kelda.Info.BensinPortlet);