Ext.namespace('cmv.champs');

cmv.champs.info = new Ext.data.JsonStore({
  autoDestroy: false,
  url: 'json/champrules.php',
  // reader configs
  idProperty: 'id',
  root: 'data',
  totalProperty: 'total',
  fields: [{
    name:'id',
    type:'int'
  }, {
    name:'Tekst'
  }],
  listeners:{
    load: {
      fn: function(store, record, operation){
        var rec=store.getAt(0);
        operation.scope.add({
          html:rec.get('Tekst')
        });
      }
    }
  }
});

cmv.champs.Panel = Ext.extend(Ext.Panel,{
  constructor: function(config,title,actid,year,idx){
    config = Ext.apply({
      title: 'Om klubben > Mesterskaber '+year,
      id:'champscmvmenuitem'+idx+'-panel',
      border:false,
      layout:'fit',
      items: new Ext.TabPanel({
        activeTab: 0,
        layoutOnTabChange: true,
        defaults:{
          frame:false
        },
        items: [{
          displayed:false,
          title: 'Regelsæt',
          id:'cmvchamprules'+year,
          frame: true,
          height:400,
          listeners:{
            activate:{
              fn: function(panel){
                if(!panel.displayed)
                {
                  cmv.champs.info.load({
                    scope:panel
                  });
                  panel.displayed=true;
                }
              },afterlayout:{
              fn: function(panel){
                Ext.Msg.alert('layout');
              }
            }
            }
          }
        },{
          title: title[0],
          items: new cmv.champs.GridPanel({},actid[0],year,false)
        },{
          title: title[1],
          items: new cmv.champs.GridPanel({},actid[1],year,false)
        },{
          title: title[2],
          items: new cmv.champs.GridPanel({},actid[2],year,false)
        },{
          title: 'Samlet stilling',
          items: new cmv.champs.GridPanel({},'',year,true)
        }]
      })
    }, config);
    cmv.champs.Panel.superclass.constructor.call(this, config);
  }
});

cmv.champs.GridPanel = Ext.extend(Ext.grid.GridPanel, {
  constructor: function(config,actid,year,timehidden) {
    var colModel=new Ext.grid.ColumnModel({
      columns: [{
        dataIndex: 'place',
        header: 'Placering'
      }, {
        dataIndex: 'name',
        header: 'Navn',
        width: 150
      }, {
        dataIndex: 'time',
        header: 'Tid',
        hidden: timehidden
      }, {
        dataIndex: 'point',
        header: 'Point'
      }],
      defaults: {
        sortable: true,
        filterable: false,
        width:100
      }
    });
    var store = new Ext.data.JsonStore({
      proxy: new Ext.data.HttpProxy({
        api: {
          read : 'json/champresults.php'
        }
      }),
      baseParams :{
        actid:actid,
        year: year
      },
      autoDestroy: true,
      root: 'data',
      totalProperty: 'total',
      fields: [{
        name: 'place',
        type: 'int'
      }, {
        name: 'name'
      }, {
        name: 'time'
      },{
        name: 'point'
      }],
  listeners:{
    load: {
      fn: function(store, record, operation){
        var hgt=operation.scope.ownerCt.ownerCt.ownerCt.getHeight()-55;
        operation.scope.setHeight(hgt);
      }
    }
  }
    });
    config = Ext.apply({
      store: store,
      colModel: colModel,
      height: 400,
      autoscroll: true,
      enableColumnHide:false
    }, config);

    cmv.champs.GridPanel.superclass.constructor.call(this, config);
  },
  onRender:function() {    
    this.store.load({scope:this});
    cmv.champs.GridPanel.superclass.onRender.apply(this, arguments);
  }
});

Ext.reg('cmvchampsgrid', cmv.champs.GridPanel);
Ext.reg('cmvchampspanel', cmv.champs.Panel);
