subclass(RecordListDataGrid, IDragSource, IRecordListPanelChildContent, DataGridSelect);
function RecordListDataGrid( id )
{
  this.DataGridSelect( id );
  
  this.parent = null;
  
  this.mouseDownTitleField = null;
  this.mouseDownTitleX = null;
  this.dragTitleField = null;
  this.overTitleField = null;
  this.rowClickPos = null

  this.actions     = new Array();
  this.defaultAction = null;
  this.sortOrder   = new Object();
  this.sortFields  = new Array();
  this.dragEnabled = true;

  this.onDragStart = new REvent();
  this.onDragMove  = new REvent();
  this.onDragEnd   = new REvent();
  this.onOrderByChange = new REvent();
  this.onGridBodyScroll = new REvent();
  this.onTitleMouseDown = new REvent();
  this.onColumnOrderChange = new REvent();

  this.onRowSelect.addEventListener(new REventListener(RecordListDataGrid.prototype.onRowSelectListener, this));
  this.onRowUnselect.addEventListener(new REventListener(RecordListDataGrid.prototype.onRowUnselectListener, this));
  this.onRowHighlight.addEventListener(new REventListener(RecordListDataGrid.prototype.onRowHighlightListener, this));
  this.onRowUnhighlight.addEventListener(new REventListener(RecordListDataGrid.prototype.onRowUnhighlightListener, this));
  this.onRowMouseDown.addEventListener(new REventListener(RecordListDataGrid.prototype.onRowMouseDownListener, this));
  this.onTitleClick.addEventListener(new REventListener(RecordListDataGrid.prototype.onTitleClickListener, this));
  this.onGridBodyScroll.addEventListener(new REventListener(RecordListDataGrid.prototype.updateGridHeaderScroll, this));
  this.onTitleMouseDown.addEventListener(new REventListener(RecordListDataGrid.prototype.onTitleMouseDownListener, this));
  this.onRowDoubleClick.addEventListener(new REventListener(RecordListDataGrid.prototype.onRowDoubleClickListener, this));
}

RecordListDataGrid.statics = new Object();
RecordListDataGrid.statics.ASCENDING = 'asc';
RecordListDataGrid.statics.DESCENDING = 'desc';

RecordListDataGrid.prototype.setDragEnabled = function(dragEnabled)
{
  this.dragEnabled = dragEnabled;
}
RecordListDataGrid.prototype.getDragEnabled = function()
{
  return this.dragEnabled;
}

RecordListDataGrid.prototype.addAction = function(action, defaultAction)
{
  action.onClick.addEventListener(new REventListener(RecordListDataGrid.prototype.onActionClickListener, this));
  this.actions[this.actions.length] = action;
  if (defaultAction)
    this.defaultAction = action;
}

RecordListDataGrid.prototype.changeClass = function(id, className)
{
  var domRow = document.getElementById(this.getNodeDomElementId(id));
  if(domRow)
    domRow.className = className;
}

RecordListDataGrid.prototype.getTitleClass = function(field)
{
  var className = 'sortableColumn';
  if(this.sortFields[0] == field)
  {
    // assume sorting is always done on load
    if(RecordListDataGrid.statics.ASCENDING == this.sortOrder[field])
      className += ' descending';
    else if(RecordListDataGrid.statics.DESCENDING == this.sortOrder[field])
      className += ' ascending';
  }
  return className;
}

RecordListDataGrid.prototype.init = function(parent)
{
  this.parent = parent;
  if(this.parent.onResize)
  {
    this.parent.onResize.addEventListener(new REventListener(RecordListDataGrid.prototype.onResizeListener, this));
    this.parent.onResize.addEventListener(new REventListener(RecordListDataGrid.prototype.updateGridHeaderScroll, this));
  }
}

RecordListDataGrid.prototype.attach = function( e )
{
  DataGrid.prototype.attach.call(this, e);

  var dataGridHeader = document.getElementById(RegisterControl.statics.encodeId(this, "dataGridHeader"));
  var dataGridBody = document.getElementById(RegisterControl.statics.encodeId(this, "dataGridBody"));
  dataGridHeader.style.overflow = "hidden";
  dataGridBody.style.overflow = "auto";
  dataGridBody.onscroll = RecordListDataGrid.statics.onGridBodyScroll;
  e.onselectstart = RecordListDataGrid.statics.onSelectStart;
  
  var documentProperties = DocumentProperties.statics.getInstance();
  documentProperties.onMouseMove.addEventListener(new REventListener(RecordListDataGrid.prototype.dragTitleMouseMove, this));
  documentProperties.onMouseUp.addEventListener(new REventListener(RecordListDataGrid.prototype.dragTitleMouseUp, this));
  documentProperties.onMouseMove.addEventListener(new REventListener(RecordListDataGrid.prototype.dragRowMouseMove, this));
  documentProperties.onMouseUp.addEventListener(new REventListener(RecordListDataGrid.prototype.dragRowMouseUp, this));
  
  var arrow = document.createElement( "img" );
  arrow.id = RegisterControl.statics.encodeId(this, "arrow");
  arrow.src = Icons['ArrowDown'];
  arrow.style.position = "absolute";
  arrow.style.display = "none";
  document.body.appendChild( arrow );
}

RecordListDataGrid.prototype.getBaseRowClass = function( id )
{
  if (0==(id%2))
    return "row";
  return "row rowOdd";
}
RecordListDataGrid.prototype.onRowSelectListener = function( id )
{
  this.changeClass(id, this.getBaseRowClass(id) + " rowSelect");
}
RecordListDataGrid.prototype.onRowUnselectListener = function( id )
{
  this.changeClass(id, this.getBaseRowClass(id));
}
RecordListDataGrid.prototype.onRowHighlightListener = function( id )
{
  var className = this.getBaseRowClass(id);
  if ( this.isRowSelected( id ) )
    className += " rowSelect";
  className += " rowOver";
  this.changeClass(id, className);
}
RecordListDataGrid.prototype.onRowUnhighlightListener = function( id )
{
  var className = this.getBaseRowClass(id);
  if ( this.isRowSelected( id ) )
    className += " rowSelect";
  this.changeClass(id, className);
}

RecordListDataGrid.prototype.onRowMouseDownListener = function( id, ctrlPressed, shiftPressed, e )
{
	//Handles click events (on mouse down in this case) for grid views with a list grid
  var dropInProgress = false;
  if (this.parent && this.parent.parent && this.parent.parent.getDropInProgress)
    dropInProgress = this.parent.parent.getDropInProgress();
  
  if(this.dragEnabled && !this.showingWaiting && !dropInProgress)
  {
    this.rowClickPos = Coordinate.Statics.findEventPosition(e);
  }
}

RecordListDataGrid.prototype.dragRowMouseMove = function(x, y)
{
  if (this.rowClickPos)
  {
    // don't start the move until the mouse has moved 10px
    var distance = Math.sqrt(Math.pow(this.rowClickPos.x - x, 2) + Math.pow(this.rowClickPos.y - y, 2))
    if (10<distance)
    {
      this.mouseDownId = null;
      this.rowClickPos = null
      var list = this.getSelectedItems();
      DragMonitor.statics.getInstance().setDragSource(this, list); 
    }
  }
}
RecordListDataGrid.prototype.dragRowMouseUp = function()
{
  this.rowClickPos = null
}

RecordListDataGrid.prototype.createIcon = function( image )
{
   var img = document.createElement("img");
   img.src = image;
   img.style.width = '16px';
   img.style.width = '16px';
   return img;
}

RecordListDataGrid.prototype.renderTitleRow = function( tr )
{
  var th = document.createElement( "th" );
  th.className = 'menuCell';

  var a = document.createElement("o");
  a.selectAllState = true;
  a.style.cursor = 'pointer';
  a.appendChild(document.createTextNode('Select All'));
  a.instanceId = this.getId();
  a.onclick = RecordListDataGrid.statics.onSelectAllNone;
  th.appendChild(a);

  tr.appendChild( th );
}
RecordListDataGrid.prototype.renderTitleCell = function( cell, field, content, index )
{
  cell.onmousedown = RecordListDataGrid.statics.onTitleMouseDown;
  cell.className = this.getTitleClass(field);
  if (this.fieldSequence.length-1 == index)
  {
    // add some space to the end of the header to make room for the scrollbar
    var td = document.createElement( "td" );
    td.className = 'lastCell';
    td.width = '30px';
    cell.parentNode.appendChild( td );
  }
  DataGridSelect.prototype.renderTitleCell.call(this, cell, field, content, index );
}

RecordListDataGrid.prototype.renderRow = function( tr, id )
{
  tr.className = this.getBaseRowClass(id);
  
  var td = document.createElement( "td" );
  tr.appendChild( td );
  if (0<this.actions.length)
  {
    for (var i=0; i<this.actions.length; i++)
    {
      if (!this.actions[i].actionFilter || this.actions[i].actionFilter.call(this, this.contentList[id]))
      {
        var img = this.createIcon(this.actions[i].image);
        img.id = this.getActionDomElementId( i, id );
        if (0<i)
          img.style.paddingLeft = '3px';
        img.onclick = RecordListDataGrid.statics.onClick;
        img.title = this.actions[i].tooltip;
        td.appendChild(img);
      }
      else {
          var img = this.createIcon(Icons['Blank']);
          if (0<i)
              img.style.paddingLeft = '3px';
          td.appendChild(img);
      }
    }
  }
  else
    td.innerHTML = '&nbsp;';

  DataGridSelect.prototype.renderRow.call(this, tr, id );
}

RecordListDataGrid.prototype.adjustHeaderRowWidth = function( w )
{
  this.minHeaderRowWidth = w + 36;
  return this.minHeaderRowWidth;
}

RecordListDataGrid.prototype.adjustDataRowWidth = function( w )
{
  this.minDataRowWidth = w;
  return this.minDataRowWidth;
}

RecordListDataGrid.prototype.getActionDomElementId = function(actionIndex, id)
{
  return RegisterControl.statics.encodeId( this, actionIndex, id );
}

RecordListDataGrid.prototype.onActionClickListener = function(action, recordId )
{
  window.location = action.getRedirectPage() + '&assign=RecordIdList,' + this.getContentItem(recordId)[this.keyField];
}

RecordListDataGrid.prototype.onResizeListener = function()
{
  var dataGridHeader = document.getElementById(RegisterControl.statics.encodeId(this, "dataGridHeader"));
  var dataGridBody = document.getElementById(RegisterControl.statics.encodeId(this, "dataGridBody"));
  var thead = document.getElementById(RegisterControl.statics.encodeId(this, "thead"));
  var tbody = document.getElementById(RegisterControl.statics.encodeId(this, "tbody"));

  var remainingWidth = parseInt(dataGridBody.parentNode.offsetWidth) - 2;
  remainingWidth = ((0<remainingWidth)?remainingWidth:1);

  dataGridBody.style.width = remainingWidth + "px";
  dataGridHeader.style.width = remainingWidth + "px";
  
  var remainingHeight = dataGridBody.parentNode.offsetHeight - dataGridHeader.offsetHeight - 2;
  dataGridBody.style.height = ((0<remainingHeight)?remainingHeight:1) + "px";
}

RecordListDataGrid.prototype.addSortField = function(field, direction)
{
  // remove the field from the sort fields if it is already there
  for (var i=0; i<this.sortFields.length; i++)
  {
    if (this.sortFields[i] == field)
    {
      this.sortFields.splice(i, 1);
      break;
    }
  }
  
  // add the field to the begining for the sort fields
  this.sortFields.unshift(field);
  
  // set the sort direction
  if(RecordListDataGrid.statics.ASCENDING == this.sortOrder[field]  || RecordListDataGrid.statics.DESCENDING == direction)
    this.sortOrder[field] = RecordListDataGrid.statics.DESCENDING;
  else
    this.sortOrder[field] = RecordListDataGrid.statics.ASCENDING;
}

RecordListDataGrid.prototype.onTitleClickListener = function(field)
{
  this.addSortField(field);
  this.onOrderByChange.trigger(this.createOrderByList());
}

RecordListDataGrid.prototype.createOrderByList = function()
{
  var orderBy = new Array();
  for (var i=0; i<this.sortFields.length; i++)
  {
    orderBy[orderBy.length] = this.sortFields[i] +  ' ' + this.sortOrder[this.sortFields[i]];
  }
  return orderBy;
}

RecordListDataGrid.prototype.updateGridHeaderScroll = function()
{
  var dataGridHeader = document.getElementById(RegisterControl.statics.encodeId(this, "dataGridHeader"));
  var dataGridBody = document.getElementById(RegisterControl.statics.encodeId(this, "dataGridBody"));
		
  dataGridHeader.scrollLeft = dataGridBody.scrollLeft;
}

RecordListDataGrid.prototype.handlesScrolling = function()
{
  return true;
}

RecordListDataGrid.prototype.onTitleMouseDownListener = function(field, x)
{
  this.mouseDownTitleField = field;
  this.mouseDownTitleX = x;
}

RecordListDataGrid.prototype.positionArrow = function(x, y)
{
  var arrow = document.getElementById(RegisterControl.statics.encodeId(this, 'arrow'));
  if (arrow)
  {
    arrow.style.top = y+'px';
    arrow.style.left = x+'px';
    arrow.style.display = 'block';
  }
}

RecordListDataGrid.prototype.dragTitleMouseMove = function(x, y)
{
  if (this.mouseDownTitleField)
  {
    // don't start the move until the mouse has moved 10px left or right
    if (10<Math.abs(this.mouseDownTitleX - x))
    {
      this.dragTitleField = this.mouseDownTitleField;
      this.mouseDownTitleField = null;
    }
  }
  else if (this.dragTitleField)
  {
    var thead = document.getElementById(RegisterControl.statics.encodeId(this, "thead"));
    var headerRow = thead.rows[0];

    // find the cell for this mouse position
    var cell = null;
    var cellPos = null;
    for (var i=0; i<headerRow.cells.length; i++)
    {
      var headerCell = headerRow.cells[i];
      var pos = Coordinate.Statics.findAbsolutePos(headerCell);
      if(headerCell.id)
      {
        if (!cell)
        {
          cell = headerCell;
          cellPos = pos;
        }
        if (pos.x<x)
        {
          cell = headerCell;
          cellPos = pos;
        }
      }
      else if(i+1==headerRow.cells.length && pos.x<x) // check if past the end
      {
        cell = null;
        cellPos = pos;
      }
    }
    if (cellPos)
      this.positionArrow(cellPos.x-7, cellPos.y-10);

    if (cell)
    {
      var params = new Array();
      var instanceId = RegisterControl.statics.seperateId(cell.id, params);
      this.overTitleField = params[0];
    }
    else
      this.overTitleField = null;
  }
}
RecordListDataGrid.prototype.dragTitleMouseUp = function()
{
  this.mouseDownTitleField = null;
  if (this.dragTitleField)
  {
    if (this.dragTitleField != this.overTitleField)
    {
      this.displayWaiting("Loading...");

      this.moveColumn(this.dragTitleField, this.overTitleField);
      this.dragTitleField = null;

      this.unselectAll();
      this.refresh();
      this.hideWaiting();

      this.onColumnOrderChange.trigger();
    }
    else
    {
      this.dragTitleField = null;
    }
    var arrow = document.getElementById(RegisterControl.statics.encodeId(this, 'arrow'));
    if (arrow)
      arrow.style.display = 'none';
  }
}

RecordListDataGrid.prototype.onRowDoubleClickListener = function( id )
{
  if (this.defaultAction)
    this.defaultAction.onClick.trigger( this.defaultAction, id );
}

RecordListDataGrid.statics.onClick = function()
{
	//Handles on click for items found in main body of the double asp list views
  var params = new Array();
  var instance = RegisterControl.statics.decodeId(this.id, params);
  var actionIndex = params[0];
  var recordId = params[1];
  var action = instance.actions[actionIndex];
  action.onClick.trigger( action, recordId );
}

RecordListDataGrid.statics.onGridBodyScroll = function()
{
  var params = new Array();
  var instance = RegisterControl.statics.decodeId(this.id, params);
  instance.onGridBodyScroll.trigger( );
}

RecordListDataGrid.statics.onSelectStart = function(e)
{
  return false;
}

RecordListDataGrid.statics.onTitleMouseDown = function()
{
  var params = new Array();
  var instance = RegisterControl.statics.decodeId(this.id, params);
  var field = params[0];
  
  var doc = DocumentProperties.statics.getInstance();

  instance.onTitleMouseDown.trigger( field, doc.getMouseX() );
  
  return false;
}

RecordListDataGrid.statics.onSelectAllNone = function()
{
  var params = new Array();
  var instance = RegisterControl.statics.decodeId(this.instanceId, params);
  for(var i=this.childNodes.length-1; i>=0; i--)
    this.removeChild(this.childNodes[i]);
  if (this.selectAllState)
  {
    this.selectAllState = false;
    this.appendChild(document.createTextNode('Deselect all'));
    instance.selectAll();
  }
  else
  {
    this.selectAllState = true;
    this.appendChild(document.createTextNode('Select All'));
    instance.unselectAll();
  }  
}
