/**
 * Creates a view of the record list that has search filters and a record grid
 *
 * @param id	The id of the search grid
 */
subclass(RecordListViewSearchGrid, RecordListView);
function RecordListViewSearchGrid(id)
{
  RecordListView.call(this, id);

  this.searchPanel = new RecordListPanel(id + "_searchPanel", "Search", "style_"+id);
  this.searchPanel.setChildContent(this.search);
  this.searchPanel.setWidth(250);
  this.searchPanel.setCollapseDirection(RecordListPanel.statics.collapseDirection.horizontal);

  this.dataGridPanel.setLeftPanel(this.searchPanel);
}

/**
 * Called during setup to allow the content to initialize itself.  Eg register events on the parent
 *
 * @param parent	The parent of the content, the record list this view is added to
 */
RecordListViewSearchGrid.prototype.init = function(parent)
{
  RecordListView.prototype.init.call(this, parent);
  
  this.searchPanel.init(this);

  this.refreshItems = true;

  this.parent.onRecordModify.addEventListener(new REventListener(this.onRecordModifyListener, this));
  this.parent.onReLayout.addEventListener(new REventListener(this.onReLayoutListener, this));
  this.onShow.addEventListener(new REventListener(this.onShowListener, this));
  this.search.onSearch.addEventListener(new REventListener(this.onSearchListener, this));
  this.search.onReset.addEventListener(new REventListener(this.onSearchListener, this));
  this.searchPanel.onExpandCollapse.addEventListener(new REventListener(this.onReLayoutListener, this));
}

/**
 * Attaches the search filters to a DOM node.
 *
 * @param node	The the DOM node to put the search filters in
 */
RecordListViewSearchGrid.prototype.attach = function(node)
{
  this.searchPanel.attach(node);
  this.dataGridPanel.attach(node);
}

/**
 * Handles relaying out the content of this view
 */
RecordListViewSearchGrid.prototype.onReLayoutListener = function()
{
  this.searchPanel.resize();
  this.dataGridPanel.resize();
}

/**
 * Handles updating the items listed when they are changed
 */
RecordListViewSearchGrid.prototype.onRecordModifyListener = function()
{
  if (this.parent.getCurrentView() == this)
    this.requestSearchedItems();
  else
    this.refreshItems = true;
}

/**
 * Handles getting itself the data needed to show itself
 */
RecordListViewSearchGrid.prototype.onShowListener = function()
{
  if (this.refreshItems)
  {
    this.requestSearchedItems();
    this.refreshItems = false;
  }
  else
  {
    this.onDataChange.trigger(this.totalRows, this.getStartIndex());
  }
}

/**
 * Handles updating the records listed when a search returns the results
 
 * @param jsObject	The javascript object of results 
 */
RecordListViewSearchGrid.prototype.onSearchItemsLoad = function(jsObject)
{
  this.onItemsLoad(jsObject);
}

/**
 * Set if this search grid should allow its search to be in generic mode
 *
 * @param e	If general search should be allowed
 */
RecordListViewSearchGrid.prototype.setAllowGeneralSearch = function(e)
{
    this.search.setAllowGeneralSearch(e);
}