CatClientResult = function(config){
  this.url = config.url;
  this.serviceName = config.serviceName;
  this.defaultTitle = config.title;
  this.detailPanel= config.detailPanel;

  this.search = function(query, params){
  	this.catStore.baseParams.query = query;
    if(params){
      this.catStore.baseParams.sortBy = params.sortBy;
    }
    this.catStore.load({params:{'start':0}});
    this.setTitle(this.defaultTitle, 'loading-icon');
    this.catStore.on({
      'load':{
         fn: function(store, records, options){
           this.setTitle(this.defaultTitle+' ('+store.getTotalCount()+')','loading-space');
         },
         scope: this
      },
			'loadexception':{
				fn: function(store, records, options){
					this.setTitle(this.defaultTitle, 'loading-error');
//				alert('Error at '+config.title);
//      	if(recs.length==0){} //TODO uselect
				},
				scope: this
			}
    })
  }

  this.showDetail = function(sm, idx, r){
    this.detailPanel.showDetail({
      id: r.id, 
      serviceName: this.serviceName, 
      url: this.url 
      //proxy: this.proxy
    });
  }

  this.showDetailByQuery = function(q){
    this.detailPanel.showDetail({
      query: q, 
      serviceName: this.serviceName, 
      url: this.url 
      //proxy: this.proxy
    });
}

  //uložiště metadat
  this.catStore = new Ext.data.JsonStore({
    url: config.url,
    //proxy: this.proxy,
    baseParams: {project:this.project,serviceName:this.serviceName,format:'json',standard:config.standard,query:'',session:'save',sortBy:'',limit: 25}, 
    autoLoad: false,
    root: 'records',
    totalProperty: 'matched',
    id:'id',
    fields:['id', 'title', 'abstract', 'trida', 'link', 'format']
  }); 

  // nastaveni parametrů úložiště
  this.setParams = function(params){
    for(param in params){
      this.catStore.baseParams[param] = params[param];
    }
  }

  // zobrazení řádku levé tabulky
  this.recordRenderer = function(value, p, record){
    var s = '<span class="rec-title cat-'+record.data.trida.toLowerCase()+'">'+value+"</span>";
    if(record.data.link.toUpperCase().indexOf('WMS')>0)  s += "<a class='mapa' href='#' onclick=\"return app.showMap('"+record.data.link+"','wms');\">map</a>";
    else if(record.data.link.toUpperCase().indexOf('WFS')>0)  s += "<a class='mapa' href=\"javascript:app.showMap('"+record.data.link+"','wfs');\">map</a>";
    else if(record.data.format.toUpperCase().indexOf('MAPMAN')>0) s += "<a class='run' href='"+record.data.link+"' target='_blank'>map</a>";
    else if(record.data.link!='') s += " <a class='run' href='"+record.data.link+"' target='_blank'>"+HS.i18n("open")+"</a>";
    return s;
  }

  this.reset = function(){
    this.catStore.removeAll();
    this.detailPanel.body.update();
  }
 

  // --- seznam nalezenych - levý panel
    this.recordList = new Ext.grid.GridPanel({
      itemSelector: 'div.thumb-wrap',
      multiSelect: false,
      region: 'center',
      layout: 'fit',
      title: this.defaultTitle,
      iconCls: 'loading-space',
      store: this.catStore,
      hideHeaders: true,
      closable: true,
      columns: [
        {id:'title', width: 150, sortable: false, dataIndex: 'title', renderer: this.recordRenderer}
      ],
      loadMask: true,
      autoExpandColumn: 'title',
      sm: new Ext.grid.RowSelectionModel({
        singleSelect: true,
        listeners: {'rowselect': this.showDetail, scope:this}
      }),
      viewConfig: {
        forceFit: true,
        enableRowBody:true,
        showPreview:true,
        getRowClass : function(record, rowIndex, p, store){
          if(this.showPreview){
            p.body = '<div style="margin:3px; color:gray">'+record.data.abstract.substring(0,255)+' ...</div>';
            return 'x-grid3-row-expanded';
          }
          return 'x-grid3-row-collapsed';
        }
      },
      bbar: new Ext.PagingToolbar({
        pageSize: 25,
        store: this.catStore,
        paramNames: {start:'start'},
        displayInfo: true,
        displayMsg: '{0} - {1} / {2}',
        emptyMsg: HS.i18n("Not found"),
        items:[{
          pressed: true,
          enableToggle:true,
          text: HS.i18n('Detailed'),
          icon: 'img/document-extended.gif',
          cls: 'x-btn-text-icon details',
          scope: this,
          toggleHandler: function(btn, pressed){
            this.getView().showPreview = pressed;
            this.getView().refresh();
          }
        }]
      })
    });
  CatClientResult.superclass.constructor.call(this,this.recordList);
}

Ext.extend(CatClientResult, Ext.grid.GridPanel, {});

HS.Lang["cze"]["Detailed"] = "Podrobnější";
HS.Lang["cze"]["Not found"] = "Nic nenalezeno";
HS.Lang["cze"]["open"] = "otevřít";

