dojo.provide("parus.gridExtraRow");

dojo.require("dojox.grid.cells.dijit");

dojo.require("dojox.grid.enhanced._Plugin");
dojo.require("dojox.grid.EnhancedGrid");

dojo.declare("dojox.grid.enhanced.plugins.extraRow", dojox.grid.enhanced._Plugin, {

  name: "extraRow",

  visible: true,

  rowCount: 1,

  _oldCreateView: null,

  _afterRowConnect: null,

  _table: null,

  _barRows: null,

  values: null,

  constructor: function(grid, args) {
    this.inherited(arguments);
    dojo.mixin(this, args);

    this._barRows = [];

    var _this = this;
    var g = this.grid;

    var old_func = this._oldCreateView = dojo.hitch(this.grid, this.grid.createView);
    this.grid.createView = function() {
      _this._cleanUp();
      var view = old_func.apply(g, arguments);;
      _this._afterRowConnect = dojo.connect(view, "onAfterRow", _this, "_afterRow");
      return view;
    };

    dojo.mixin(g, {
      showExtraRowBar: dojo.hitch(_this, this.show),
      hideExtraRowBar: dojo.hitch(_this, this.hide),
      getExtraRowVisible: dojo.hitch(_this, function() {
        return this.visible
      }),
      extraRowObject: _this,
      onExtraRowValue: function(column, row) {
        return "";
      }
    });
  },

  show: function() {
    this.visible = true;
    this._render();
    this.grid.setQuery(this.grid.query || {});
  },

  hide: function() {
    this.visible = false;
    this._cleanUpBar();
    this.grid.setQuery(this.grid.query || {});
  },

  _afterRow: function(inRowIndex, cells, inRowNode) {
    if (inRowIndex == -1) dojo.query("table", inRowNode).forEach(dojo.hitch(this, function(table) {
      this._cleanUpBar();
      this._table = table;
      if (this.visible) {
        this._render();
      }
    }));
  },

  _cleanUpEvent: function() {
    this._table = null;
    if (this._afterRowConnect) {
      dojo.disconnect(this._afterRowConnect);
      this._afterRowConnect = null;
    };
  },

  _cleanUpBar: function() {
    if (this._barRows) {
      dojo.forEach(this._barRows, function(row) {
        if (row.parentNode) row.parentNode.removeChild(row);
      });
    };
    this._barRows = [];
  },

  _cleanUp: function() {
    this._cleanUpEvent();
    this._cleanUpBar();
  },

  _render: function() {

    if (!this._table) return;
    for (var r = 0; r < this.rowCount; r++) {

      var row = this._table.insertRow(-1);

      this._barRows.push(row);

      dojo.addClass(row, "dojoxGridStatusBarRow");

      dojo.forEach(this._table.rows[0].cells, dojo.hitch(this, function(sample) {
        var cell = row.insertCell(-1);
        cell.style.padding = "2px 5px 2px 0px";
        cell.style.textAlign = sample.style.textAlign;
        dojo.addClass(cell, "dojoxGridCell");
      }));

      var idx = -1;

      dojo.forEach(this.grid.views.views[0].structure.cells[0], dojo.hitch(this, function(column) {

        if (column.field && !column.hidden) {


          if (idx == -1) idx = column.index
          else idx++;

          var cell = row.cells[idx];
          cell.innerHTML = this.grid.onExtraRowValue(column, r)
        }

      }));

    }

  },

  destroy: function() {
    this._cleanUp();
    this.grid.createView = this._oldCreateView;
    this.inherited(arguments);
  }

});


dojox.grid.EnhancedGrid.registerPlugin(dojox.grid.enhanced.plugins.extraRow);