Ext.namespace('cmv.links');

cmv.links.categoriStore = new Ext.data.JsonStore({
  url: 'json/linkscat.php',
  root: 'data',
  fields:[
  'id','Categori',{
    name:'status',
    type:'boolean'
  },{
    name:'antal',
    type:'int'
  }
  ],
  listeners:{
    load: {
      fn: function(store, record, operation){
        var tbar = operation.scope.getTopToolbar();
        store.each(function(record){
          tbar.add([{
            xtype:'tbseparator'
          },record.get('Categori')+" ("+record.get('antal')+")",{
            xtype:'checkbox',
            checked:record.get('status'),
            id:'links_'+record.get('id'),
            listeners:{
              check:{
                fn:function(box,check){
                  var idx=box.getId().split('_');
                  var catrec=cmv.links.categoriStore.getById(idx[1]);
                  catrec.set('status',check);
                  cmv.links.Store.filterBy(function(rec,id){
                    return cmv.links.categoriStore.getById(rec.get('Categori_id')).get('status')
                  })
                  cmv.links.panel.removeAll(true);
                  cmv.links.panel.add({
                    id:'cmvlinksinner',
                    layout:'table',
                    layoutConfig: {
                      columns:7,
                      tableAttrs: {
                        cellspacing: 5
                      }
                    }
                  });
                  cmv.links.Store.each(cmv.links.build)
                  cmv.links.panel.doLayout();
                }
              }
            }
          }])
        })
        cmv.links.Store.load({
          scope:operation.scope
        });
      }
    }
  }
})

cmv.links.Store = new Ext.data.JsonStore({
  url: 'json/links.php',
  root: 'data',
  fields:[
  'id', 'Categori_id', 'Description', 'Hyperlink',{
    name:'Grafik',
    type:'boolean'
  }
  ],
  listeners:{
    load: {
      fn: function(store, record, operation){
        //Collapse opslagstavlen
        cmv.opslag.Panel.collapse();
        cmv.links.Store.filterBy(function(rec,id){
          return cmv.links.categoriStore.getById(rec.get('Categori_id')).get('status')
        })
        store.each(cmv.links.build)
        operation.scope.doLayout();
      }
    }
  }
})

cmv.links.panel = new Ext.Panel({
  displayed:false,
  title:'Information > Links',
  id:'cmvmenuitem11-panel',
  iconCls: 'icon-grid',
  margins: '0 5 0 5',
  collapsible: false,
  autoScroll: true,
  cmargins: '0 5 0 5', // adjust top margin when collapse
  border: false,
  frame:true,
  items:{
    id:'cmvlinksinner',
    layout:'table',
    layoutConfig: {
      columns:7,
      tableAttrs: {
        cellspacing: 5
      }
    }
  },
  tbar  : [
  'Vælg kategorier:', ' '
  ],
  listeners:{
    activate: {
      fn: function(panel){
        if(!panel.displayed)
        {
          cmv.links.categoriStore.load({
            scope:this
          });
          panel.displayed=true
        }
      }
    }
  }
});

cmv.links.build=function(record){
  if(record.get('Grafik'))
    cmv.links.pic='<img src="php/display_linklogo.php?idx='+record.get('id')+'">';
  else
    cmv.links.pic='<img src="images/male_silh.jpg">';
  cmv.links.panel.get(0).add(
  {
    title:record.get('Description'),
    frame: true,
    html:'<ul><li>'+"<a href='http://"+record.get('Hyperlink')+"' target='_blank'>"+cmv.links.pic+"</a>"+'</li></ul>'
  }
  )
}

cmv.menu.wrc.add(cmv.links.panel);
