

Top10.widget.Grid = Ext.extend(Ext.grid.GridPanel, {
	
	widget: '',
    context: '',
    context_id: null,
	
    initComponent: function() {
        var s = new Ext.data.GroupingStore({
            url: Top10.APP_URL + 'widgets/grid',
            reader: new Ext.data.JsonReader(),
            remoteSort: true,
            remoteGroup: true,
            baseParams: {
                start: 0,
                limit: 10,
                widget: this.widget,
                context: this.context,
                context_id: this.context_id
            }
        });
        
        var config = {
        	ref: 'grid',
            view: new Ext.grid.GroupingView({
                forceFit: true,
                groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "'+ I18n.Items +'" : "'+ I18n.Item +'"]})'
            }),
            plugins: {
                ptype: 'ux-grid-metagrid',
                maskEmpty: false,
                paging: {perPage: 10}
            },
            enableColLock: false,
            loadMask: true,
            border: false,
            stripeRows: true,
            store: s,
            bbar: {
            	xtype: 'paging',
                store: s,
                displayInfo: true,
                pageSize: 10,
                plugins: {
                    ptype: 'perpagefield',
                    beforeText: '',
                    afterText: ''
                }
            }
        };
        
        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);
        
        Top10.widget.Grid.superclass.initComponent.apply(this, arguments);
    },
    
    onRender: function(ct, position) {
        Top10.widget.Grid.superclass.onRender.call(this, ct, position);
        
        this.getEl().mask(I18n.Loading);
        this.getStore().on('load', function(s, r, o) {
            this.getEl().unmask();
            this.getBottomToolbar().store.baseParams = o.params; //to fix TOPTEN-99
        }, this);
        
        this.on('rowdblclick', function(grid, row) {
            var record = grid.getStore().getAt(row);
            
            new Top10.window.Validation({
            	nickname: record.data.nickname,
            	record_id: record.data.record_id
            }).show();
        });
        
        this.on('rowcontextmenu', function(grid, rowIndex, e) {
            e.stopEvent();
            
            grid.getSelectionModel().selectRow(rowIndex);
            var item = grid.getStore().getAt(rowIndex);
            
            new Ext.menu.Menu({
                items: [{
                    text: item.data.nickname,
                    iconCls: Top10.Icon.css('user_suit')
                }, '-', {
                    text: I18n.Validation,
                    iconCls: Top10.Icon.css('tick'),
                    handler: function() {
                        new Top10.window.Validation({
                           nickname: item.data.nickname,
                           record_id: item.data.record_id
                        }).show();
                    }
                }, {
                    text: I18n.RankingLogChart,
                    iconCls: Top10.Icon.css('chart_line'),
                    scope: this,
                    handler: function() {
                        new Top10.window.UserRankingLog({
                            widget: this.widget,
                            context: this.context,
                            context_id: this.context_id,
                            account_id: item.data.account_id,
                            nickname: item.data.nickname
                        }).show();
                    }
                }]
            }).showAt(e.getXY());
        }, this);
    },
    
    showContextColumn: function(r, o, s) {
    	if (!s)
    	   return;
        
    	var context = o.params.context;
        var cm = this.getColumnModel();
        
        cm.setHidden(cm.findColumnIndex('continent_name'), true);
        cm.setHidden(cm.findColumnIndex('country_name'), true);
        cm.setHidden(cm.findColumnIndex('city_name'), true);
        
        switch(context) {
        	case 'World':
                cm.setHidden(cm.findColumnIndex('country_name'), false);
                break;
            case 'Continent':
                cm.setHidden(cm.findColumnIndex('country_name'), false);
                break;
            case 'Country':
                cm.setHidden(cm.findColumnIndex('city_name'), false);
                break;
            case 'City':
                break;
        }
    }
});

Ext.reg('widgetgrid', Top10.widget.Grid);