/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function() {
});

/**
 * @class Ext.grid.TableGrid
 * @extends Ext.grid.Grid
 * A Grid which creates itself from an existing HTML table element.
 * @constructor
 * @param {String/HTMLElement/Ext.Element} table The table element from which this grid will be created - 
 * The table MUST have some type of size defined for the grid to fill. The container will be 
 * automatically set to position relative if it isn't already.
 * @param {Object} config A config object that sets properties on this grid and has two additional (optional)
 * properties: fields and columns which allow for customizing data fields and columns for this grid.
 * @history
 * 2007-03-01 Original version by Nige "Animal" White
 * 2007-03-10 jvs Slightly refactored to reuse existing classes
 */
Ext.grid.TableGrid = function(table, config) {
  config = config || {};
  Ext.apply(this, config);
  var cf = config.fields || [], ch = config.columns || [];
  table = Ext.get(table);

  var ct = table.insertSibling();

  var fields = [], cols = [];
  var headers = table.query("thead th");
  for (var i = 0, h; h = headers[i]; i++) {
    var text = h.innerHTML;
    var name = 'tcol-'+i;

    fields.push(Ext.applyIf(cf[i] || {}, {
      name: name,
      mapping: 'td:nth('+(i+1)+')/@innerHTML'
    }));

    cols.push(Ext.applyIf(ch[i] || {}, {
      'header': text,
      'dataIndex': name,
      //'width': h.offsetWidth,
      //'width': 'auto',
      'css': 'white-space:normal;',
      'tooltip': h.title,
      'sortable': true
    }));
  }
  var ds  = new Ext.data.Store({
    reader: new Ext.data.XmlReader({
      record:'tbody tr'
    }, fields)
  });

  ds.loadData(table.dom);

  var cm = new Ext.grid.ColumnModel(cols);

  if (config.remove !== false) {
    table.remove();
  }

  var tableWidth = Ext.isIE6 ? 600 : 'auto';

  Ext.applyIf(this, {
    'ds': ds,
    'cm': cm,
    'sm': new Ext.grid.RowSelectionModel(),
    'maxHeight': config.maxHeight||300,
    'autoHeight': true,
    width: tableWidth
  });
  Ext.grid.TableGrid.superclass.constructor.call(this, ct, {});

};

Ext.extend(Ext.grid.TableGrid, Ext.grid.GridPanel);


Ext.grid.GridView.override({
  syncScroll : function(){
    /*this.innerHd.scrollLeft = mb.scrollLeft;*/
      var mb = this.scroller.dom;
      this.innerHd.style.width='10000px';
      var leftOffset = '-' + mb.scrollLeft + 'px';
      this.innerHd.style.left = leftOffset;
      this.grid.fireEvent("bodyscroll", mb.scrollLeft, mb.scrollTop);
    },
    getRowClass: function(row,index){
      if ( /Cancelled on/.test(row.data['tcol-0']) ) {
        return 'cancelled-policy-row';
      }
    },
    layout : function(){
        if(!this.mainBody){
            return; // not rendered
        }
        var g = this.grid;
        var c = g.getGridEl();
        var csize = c.getSize(true);
        var vw = csize.width;

        if(vw < 20 || csize.height < 20){ // display: none?
            return;
        }

        if(g.autoHeight){        
            if (g.maxHeight) {
                var hdHeight = this.mainHd.getHeight();
                var vh = Math.min(csize.height, g.maxHeight);
                this.el.setSize(csize.width, vh);    
                this.scroller.setSize(vw, vh - hdHeight);            
            } else {
                this.scroller.dom.style.overflow = 'visible';                
            }
        }else{
            this.el.setSize(csize.width, csize.height);

            var hdHeight = this.mainHd.getHeight();
            var vh = csize.height - (hdHeight);

            this.scroller.setSize(vw, vh);
            if(this.innerHd){
                this.innerHd.style.width = (vw)+'px';
            }
        }
        if(this.forceFit){
            if(this.lastViewWidth != vw){
                this.fitColumns(false, false);
                this.lastViewWidth = vw;
            }
        }else {
            this.autoExpand();
            this.syncHeaderScroll();
        }
        this.onLayout(vw, vh);
    }
});