subclass(DataGridSelect, DataGrid);
function DataGridSelect( id )
{
  this.DataGrid( id );
  this.keyField = "Id";
  this.selectedRows = new Object();
  this.rowClickId = null;

  this.onRowSelect = new REvent();
  this.onRowUnselect = new REvent();
  this.onRowDoubleClick = new REvent();
  this.onRowHighlight = new REvent();
  this.onRowUnhighlight = new REvent();
  this.onRowMouseDown = new REvent();
  this.onRowMouseUp = new REvent();
  this.onTitleClick = new REvent();
  
  this.onRowSelect.addEventListener(new REventListener(DataGridSelect.prototype.onRowSelectListener, this));
  this.onRowUnselect.addEventListener(new REventListener(DataGridSelect.prototype.onRowUnselectListener, this));
  this.onRowMouseDown.addEventListener(new REventListener(DataGridSelect.prototype.onRowMouseDownListener, this));
  this.onRowMouseUp.addEventListener(new REventListener(DataGridSelect.prototype.onRowMouseUpListener, this));
  
  var documentProperties = DocumentProperties.statics.getInstance();
  documentProperties.onKeyDown.addEventListener(new REventListener(DataGridSelect.prototype.onKeyDownListener, this));
  documentProperties.onKeyPress.addEventListener(new REventListener(DataGridSelect.prototype.onKeyPressListener, this));

  RegisterControl.statics.getInstance().add(this);
}

DataGridSelect.statics = new Object();

DataGridSelect.prototype.DataGridSelect = DataGridSelect;

DataGridSelect.prototype.onRowSelectListener = function( id )
{
  this.selectedRows[id] = true;
}
DataGridSelect.prototype.onRowUnselectListener = function( id )
{
  delete this.selectedRows[id];
}
DataGridSelect.prototype.isRowSelected = function( id )
{
  return this.selectedRows[id];
}
DataGridSelect.prototype.onRowMouseDownListener = function( id, ctrlPressed, shiftPressed )
{
  if (ctrlPressed)
  {
  }
  else if (shiftPressed)
  {
  }
  else
  {
    if (!this.isRowSelected(id))
    {
      this.unselectAll();
      this.onRowSelect.trigger(id);
    }
  }

  this.mouseDownId = id;
}
DataGridSelect.prototype.onRowMouseUpListener = function( id, ctrlPressed, shiftPressed )
{
  if (this.mouseDownId == id)
  {
    if (ctrlPressed)
    {
      if (this.isRowSelected(id))
        this.onRowUnselect.trigger(id);
      else
        this.onRowSelect.trigger(id);
      this.rowClickId = id;
    }
    else if (shiftPressed)
    {
      var startId = parseInt(id);
      var endId = parseInt(this.rowClickId);
      if (parseInt(this.rowClickId) < parseInt(id))
      {
        startId = parseInt(this.rowClickId);
        endId = parseInt(id);
      }

      var length = this.getLength();
      for (var i=0; i<length; i++)
      {
        if (startId<=i && i<=endId)
        {
          if (!this.isRowSelected(i))
            this.onRowSelect.trigger(i);
        }
        else
        {
          if (this.isRowSelected(i))
            this.onRowUnselect.trigger(i);
        }
      }
    }
    else
    {
      for (var i in this.selectedRows)
        if (i!=id)
          this.onRowUnselect.trigger( i );

      this.rowClickId = id;
    }
  }
  this.mouseDownId = null;
}


DataGridSelect.prototype.selectAll = function()
{
  var id = null;
  var length = this.getLength();
  for (var i=0; i<length; i++)
  {
    if (!this.selectedRows[i])
      this.onRowSelect.trigger( i );
  }
}

DataGridSelect.prototype.unselectAll = function()
{
  for (var id in this.selectedRows)
    this.onRowUnselect.trigger( id );
}

DataGridSelect.prototype.getSelectedItems = function()
{
  var list = new Array();
  var length = this.getLength();
  for (var i=0; i<length; i++)
    if (this.isRowSelected(i))
      list.push(this.getContentItem(i));
  return list;
}

DataGridSelect.prototype.setKeyField = function(key)
{
  this.keyField = key;
}
DataGridSelect.prototype.getKeyField = function()
{
  return this.keyField;
}

DataGridSelect.prototype.getContentKey = function(id)
{
  var item = this.contentList[id];
  if (item)
    return item[this.keyField];
  return null;
}

DataGridSelect.prototype.getSelectedKeys = function(actionFilter)
{
  var list = new Array();
  var length = this.getLength();
  var item = null;
  for (var i=0; i<length; i++)
  {
    if (this.isRowSelected(i))
    {
      item = this.getContentItem(i);
      if (!actionFilter || actionFilter.call(this, item))
        list.push(item[this.keyField]);
    }
  }
  return list;
}

DataGridSelect.prototype.renderRow = function( tr, id )
{
  tr.id = this.getNodeDomElementId(id);
  tr.onmouseover = DataGridSelect.statics.onMouseOver;
  tr.onmouseout  = DataGridSelect.statics.onMouseOut;
  tr.onmousedown = DataGridSelect.statics.onMouseDown;
  tr.onmouseup   = DataGridSelect.statics.onMouseUp;
  tr.ondblclick  = DataGridSelect.statics.onDblClick;
}

DataGridSelect.prototype.renderTitleCell = function( cell, field, content, index )
{
  cell.id = RegisterControl.statics.encodeId( this, field );
  cell.onclick = DataGridSelect.statics.onTitleClick;
  DataGrid.prototype.renderTitleCell.call(this, cell, field, content, index );
}

DataGridSelect.prototype.getNodeDomElementId = function(id)
{
  return RegisterControl.statics.encodeId( this, id );
}

DataGridSelect.prototype.onKeyDownListener = function(key, e)
{
  if (e.ctrlKey && (97==key || 65==key))
  {
    this.selectAll();
    return false; // stop all the text from getting selected in IE
  }
}
DataGridSelect.prototype.onKeyPressListener = function(key, e)
{
  if (e.ctrlKey && (97==key || 65==key))
    return false; // stop all the text from getting selected in Firefox
}

DataGridSelect.statics.onMouseOver  = function(e) { DataGridSelect.statics.eventDispatch(this.id, "onRowHighlight", e); }
DataGridSelect.statics.onMouseOut   = function(e) { DataGridSelect.statics.eventDispatch(this.id, "onRowUnhighlight", e); }
DataGridSelect.statics.onDblClick   = function(e) { DataGridSelect.statics.eventDispatch(this.id, "onRowDoubleClick", e); }
DataGridSelect.statics.onMouseDown  = function(e) { DataGridSelect.statics.eventDispatch(this.id, "onRowMouseDown", e); return false; }  // false to prevent the text from selecting if this is used to start a drag
DataGridSelect.statics.onMouseUp    = function(e) { DataGridSelect.statics.eventDispatch(this.id, "onRowMouseUp", e); }
DataGridSelect.statics.onTitleClick = function(e) { DataGridSelect.statics.eventDispatch(this.id, "onTitleClick", e); }

DataGridSelect.statics.eventDispatch = function(id, event, e)
{
  if (!e)
    e = window.event;
  var params = new Array();
  var instance = RegisterControl.statics.decodeId(id, params);
  var recordId = params[0];
  instance[event].trigger( recordId, e.ctrlKey, e.shiftKey, e );
}

