/**
 * The base class fo rhte record list views.  contains code common between them
 *
 * @param id	The id of the view
 */
subclass(RecordListView, IRecordListPage, ITabPage);
function RecordListView(id)
{
  ITabPage.call(this, id);
  IRecordListPage.call(this, id);

  this.id = id;
  this.tabText = id;
  this.parent = null;
  this.startIndex = 0;
  this.totalRows = 0;
  this.buttonList = new Array();
  
  this.dropInProgress = false;
  
  this.hierarchyColumnField = null;
  this.hierarchyColumnText = null;
  this.hierarchyColumnKey = null;
  this.hierarchyColumnPosition = 1;
    
  this.search = new RecordListSearch(id + "_search");

  this.dataGrid = new RecordListDataGrid(id + "_dataGrid");
  this.dataGridPanel = new RecordListPanel(id + "_dataGridPanel", "Data Grid", "style_"+id);
  this.dataGridPanel.setChildContent(this.dataGrid);
  
  this.gridFilter = new RecordListGridFilter(id + "_gridFilter");
  this.gridFilter.onChange.addEventListener(new REventListener(this.onGridFilterChangeListener, this));
  this.dataGridPanel.addHeaderContent(this.gridFilter);  
  
  this.dataGrid.onOrderByChange.addEventListener(new REventListener(this.sortItems, this));
  this.dataGrid.onColumnOrderChange.addEventListener(new REventListener(this.updateHierarchyColumnPosition, this));
  this.dataGrid.onColumnOrderChange.addEventListener(new REventListener(this.saveFieldSequence, this));
  
  this.gridDataCommand = null;
  this.sortGridDataCommand = null;
  this.saveFieldSequenceCommand = null;
  this.updateSearchFilterCommand = null;
  this.updateGridFilterCommand = null;
}

/**
 * Gets the id of the view
 *
 * @returns	The id of the view
 */
RecordListView.prototype.getId = function()
{
  return this.id;
}

/**
 * Attached the view to a dom node
 *  to be overridden in the views that inherit from this
 *
 * @param node	The node to attach the view to
 */
RecordListView.prototype.attach = function(node)
{
}

/**
 * Inititializes the view and calls init on any children it has
 *
 * @param parent	The parent object of this view
 */
RecordListView.prototype.init = function(parent)
{
  this.parent = parent;

  this.dataGridPanel.init(this);

  for (var i in this.buttonList)
    this.buttonList[i].init(this.parent);
}

/**
 * Gets the text to display on the tab for this view
 *
 * @returns	The text to display on the tab for this view
 */
RecordListView.prototype.getTabText = function()
{
  return this.tabText;
}
RecordListView.prototype.setTabText = function(text)
{
  this.tabText = text;
}

RecordListView.prototype.setTitle = function(title)
{
  this.dataGridPanel.setTitle(title);
}

RecordListView.prototype.getStartIndex = function()
{
  return this.startIndex;
}
RecordListView.prototype.setInitialStartIndex = function(startIndex)
{
  this.startIndex = startIndex;
}
RecordListView.prototype.setStartIndex = function(startIndex)
{
  this.startIndex = startIndex;
  this.requestItems();
}

RecordListView.prototype.addButton = function(button)
{
  this.buttonList.push(button);
  if (this.parent)
    button.init(this.parent);
}

RecordListView.prototype.getButtonList = function()
{
  return this.buttonList;
}

RecordListView.prototype.setGridDataCommand = function(command)
{
  this.gridDataCommand = command;
}

RecordListView.prototype.setSortGridDataCommand = function(command)
{
  this.sortGridDataCommand = command;
}

RecordListView.prototype.setSaveFieldSequenceCommand = function(command)
{
  this.saveFieldSequenceCommand = command;
}

RecordListView.prototype.requestItems = function()
{
  this.dataGrid.displayWaiting("Loading...");
  var command = this.gridDataCommand + "," + this.getStartIndex();
  this.postCommand(command, new REventListener(this.onItemsLoad, this));
}

RecordListView.prototype.sortItems = function(orderByList)
{
  this.dataGrid.displayWaiting("Loading...");
  var command = this.sortGridDataCommand + "," + orderByList.join(',');
  this.postCommand(command, new REventListener(this.onItemsLoad, this));
}

RecordListView.prototype.saveFieldSequence = function()
{
  var command = this.saveFieldSequenceCommand + ',' + (this.hierarchyColumnPosition?this.hierarchyColumnPosition:'') + ',' + this.dataGrid.getFieldSequence().join(',');
  this.postCommand(command, null);
}

RecordListView.prototype.updateHierarchyColumnPosition = function()
{
  var fieldSequence = this.dataGrid.getFieldSequence();
  for (var i=0; i<fieldSequence.length; i++)
  {
    if (this.hierarchyColumnField == fieldSequence[i])
    {
      this.hierarchyColumnPosition = i;
      break;
    }
  }  
}

RecordListView.prototype.onItemsLoad = function(jsObject)
{
  this.totalRows = jsObject.totalRows;
  this.createGrid(jsObject.rows);
  this.onDataChange.trigger(this.totalRows, this.getStartIndex());
}

RecordListView.prototype.returnJSON = function(json, ctx)
{
  var jsObject = new Object();
  if (json && json.length)
  {
    jsObject = eval(json);
  }
    
  if (ctx && ctx.listener)
    ctx.listener.trigger(jsObject);
}

RecordListView.prototype.returnError = function(message, ctx)
{
  alert(message);

  if (ctx && ctx.view)
    ctx.view.dropInProgress = false;
}

RecordListView.prototype.createGrid = function(rows)
{
  this.dataGrid.setContent(rows);
  this.dataGrid.unselectAll();
  this.dataGrid.refresh();
  this.dataGrid.hideWaiting();
  this.dropInProgress = false;
}
RecordListView.prototype.addColumn = function(field, text)
{
  this.dataGrid.addColumn(field, text);
}

RecordListView.prototype.setHierarchyColumn = function(field, text, key, pos)
{
  this.hierarchyColumnField = field;
  this.hierarchyColumnText = text;
  this.hierarchyColumnKey = key;
  this.hierarchyColumnPosition = pos;
}

RecordListView.prototype.addAction = function(redirectPage, iconName, tooltip, actionFilter, defaultAction)
{
  this.dataGrid.addAction(new RecordListAction(redirectPage, iconName, tooltip, actionFilter), defaultAction);
}

RecordListView.prototype.addSearchFilter = function(filter)
{
  this.search.addSearchFilter(filter);
}

RecordListView.prototype.addFilterValueList = function(filterValueList, selectedValue)
{
  this.gridFilter.addFilterValueList(filterValueList, selectedValue);
}

RecordListView.prototype.setKeyField = function(key)
{
  this.dataGrid.setKeyField(key);
}

RecordListView.prototype.setQueryCallback = function(callback)
{
  this.queryCallback = callback;
}

RecordListView.prototype.postCommand = function(command, listener)
{
  var ctx = new Object();
  ctx.listener = listener;
  ctx.view = this;
  this.queryCallback(command, ctx);
}

RecordListView.prototype.getSelectedKeys = function(actionFilter)
{
  return this.dataGrid.getSelectedKeys(actionFilter);
}

RecordListView.prototype.addSortField = function(field, direction)
{
  this.dataGrid.addSortField(field, direction);
}

RecordListView.prototype.setUpdateSearchFilterCommand = function(command)
{
  this.updateSearchFilterCommand = command;
}

RecordListView.prototype.setUpdateGridFilterCommand = function(command)
{
  this.updateGridFilterCommand = command;
}

RecordListView.prototype.setGeneralSearchEnabled = function(e)
{
  this.search.setGeneralSearchEnabled(e);
}

RecordListView.prototype.setGeneralSearchValue = function(v)
{
  this.search.setGeneralSearchValue(v);
}

RecordListView.prototype.setDragEnabled = function(dragEnabled)
{
  this.dataGrid.setDragEnabled(dragEnabled);
}

RecordListView.prototype.getDragEnabled = function()
{
  return this.dataGrid.getDragEnabled();
}

RecordListView.prototype.getDropInProgress = function()
{
  return this.dropInProgress;
}

RecordListView.prototype.moveColumnToStart = function(field)
{
  this.dataGrid.moveColumnToStart(field);
}

RecordListView.prototype.getSaveSearchCommandText = function()
{
  var commandText = this.updateSearchFilterCommand;
  if (this.search.IsGeneralSearchInUse())
  {
    commandText += ',generalSearch,'
    commandText += escape(this.search.getGeneralSearchValue());
  }
  else
  {
    var searchValues = this.search.getSearchValueList();
    commandText += ',advancedSearch,'
    if (0<searchValues.length)
      commandText += EncodingUtil.statics.arrayToCommaList(searchValues);
  }
  return commandText;
}

RecordListView.prototype.requestSearchedItems = function()
{
  this.dataGrid.displayWaiting("Loading...");
  var commandText = this.getSaveSearchCommandText();
  this.postCommand(commandText, new REventListener(this.onSearchItemsLoad, this));
}

RecordListView.prototype.onSearchListener = function()
{
  this.requestSearchedItems();
}

RecordListView.prototype.onSearchResetListener = function()
{
  this.dataGrid.displayWaiting("Loading...");
  var commandText = this.getSaveSearchCommandText();
  this.postCommand(commandText, new REventListener(this.onSearchResetItemsLoad, this));
}

RecordListView.prototype.onGridFilterChangeListener = function()
{
  this.dataGrid.displayWaiting("Loading...");
  
  var commandText = this.updateGridFilterCommand + ',';
  var gridFilterValues = this.gridFilter.getValueList();
  if (0<gridFilterValues.length)
    commandText += EncodingUtil.statics.arrayToCommaList(gridFilterValues);
  this.postCommand(commandText, new REventListener(this.onItemsLoad, this));
}
