function RecordList(id)
{
  this.id = id;
  this.pageSize = 20;
  this.tabStrip = new TabStrip(id + "tabs");
  this.viewList = new Array();
  this.defaultViewId = null;
  this.saveSelectedViewNeeded = true;
  this.buttonList = new Array();
  this.attachedButtonDivIdMap = new Array();
  
  this.onRecordModify = new REvent();
  this.onReLayout = new REvent();
}

RecordList.prototype.attach = function(node)
{
  document.body.className='listPage';
  
  var tabRow = document.createElement("div");
  tabRow.id = RegisterControl.statics.encodeId(this, "tabRow");
  tabRow.className = "tabRow";
  node.appendChild(tabRow);
  this.tabStrip.attach(tabRow);

  // add views
  for (var i in this.viewList)
  {
    var view = this.viewList[i];
    var viewRow = document.createElement("div");
    viewRow.id = RegisterControl.statics.encodeId(this, view.getId(), "view");
    viewRow.className = "viewRow clear";
    viewRow.style.display = "none";
    node.appendChild(viewRow);
    view.attach(viewRow);
    view.onDataChange.addEventListener(new REventListener(this.onDataChangeListener, this));
  }

  // controls row
  var controlRow = document.createElement("div");
  controlRow.id = RegisterControl.statics.encodeId(this, "controlRow");
  controlRow.className = 'controlRow';
  node.appendChild(controlRow);
  var clear = document.createElement("div");
  clear.className = 'clear';
  controlRow.appendChild(clear);
  var div = document.createElement("div");
  div.className = "pagesDiv";
  controlRow.appendChild(div);
  this.pageControl = new RecordListPageControl(this.getId()+"_pageControl", this.getPageSize());
  this.pageControl.attach(div);
  this.pageControl.setCurrentRows(0, 0);
  this.pageControl.onPageChange.addEventListener(new REventListener(this.onPageChangeListener, this));
  
  var buttonArea = document.createElement("div");
  buttonArea.id = RegisterControl.statics.encodeId(this, "buttonArea");
  controlRow.appendChild(buttonArea);

  clear = document.createElement("div");
  clear.className = 'clear';
  controlRow.appendChild(clear);

  this.tabStrip.onTabSelect.addEventListener(new REventListener(this.onTabSelectListener, this));
  this.tabStrip.onTabUnselect.addEventListener(new REventListener(this.onTabUnselectListener, this));
  
  var selectedView = null;
  if (this.defaultViewId)
    selectedView = this.tabStrip.getTab(this.defaultViewId);
  if(!selectedView)
    selectedView = this.tabStrip.getTabList()[0];
  else
    this.saveSelectedViewNeeded = false;
  this.setCurrentView(selectedView);
  
  var windowControl = WindowControl.statics.getInstance();
  windowControl.onResize.addEventListener( new REventListener( RecordList.prototype.updateLayout, this ) );

  try
  {
    window.focus();
  }
  catch (error)
  {
    // I don't care if it fails
  }
}

RecordList.prototype.showButton = function(button, buttonArea)
{
  if (this.attachedButtonDivIdMap[button.getDivId()])
  {
    var buttonDiv = document.getElementById(button.getDivId());
    if (buttonDiv)
      buttonDiv.style.display = 'block';
  }
  else
  {
    button.attach(buttonArea);
    this.attachedButtonDivIdMap[button.getDivId()] = true;
  }
}

RecordList.prototype.updateButtons = function(view)
{
  var buttonList = view.getButtonList();
  if (0>=buttonList.length)
    buttonList = this.buttonList;

  // show the buttons we want
  var shownButtonDivIds = new Object();
  var buttonArea = document.getElementById(RegisterControl.statics.encodeId(this, "buttonArea"));
  for (var i=0; i<buttonList.length; i++)
  {
    this.showButton(buttonList[i], buttonArea);
    shownButtonDivIds[buttonList[i].getDivId()] = true;
  }

  // hide other buttons
  for (var buttonDivId in this.attachedButtonDivIdMap)
  {
    if (!shownButtonDivIds[buttonDivId])
    {
      var buttonDiv = document.getElementById(buttonDivId);
      if (buttonDiv)
        buttonDiv.style.display = 'none';
    }
  }
}

RecordList.prototype.setTitle = function(title)
{
  this.tabStrip.setTitle(title);
}

RecordList.prototype.setCurrentView = function(view)
{
  this.tabStrip.setCurrentTab(view);
}

RecordList.prototype.getCurrentView = function()
{
  return this.tabStrip.getCurrentTab();
}

RecordList.prototype.setDefaultViewId = function(id)
{
  this.defaultViewId = id;
}

RecordList.prototype.setPageSize = function(pageSize)
{
  this.pageSize = pageSize;
}

RecordList.prototype.getPageSize = function()
{
  return this.pageSize;
}

RecordList.prototype.addView = function(view)
{
  this.viewList.push(view);
  this.tabStrip.addTab(view);
  view.init(this);
}

RecordList.prototype.addButton = function(button)
{
  this.buttonList.push(button);
  button.init(this);
}

RecordList.prototype.getId = function()
{
  return this.id;
}

RecordList.prototype.getSelectedKeys = function(actionFilter)
{
  var selectedView = this.getCurrentView();
  if (selectedView)
    return selectedView.getSelectedKeys(actionFilter);
  else
    return "";
}

RecordList.prototype.onTabSelectListener = function(tab)
{
  var tabRow = document.getElementById(RegisterControl.statics.encodeId(this, tab.getId(), "view"));
  if(tabRow)
    tabRow.style.display = 'block';

  this.updateButtons(tab);

  this.updateLayout();
  
  if(this.saveSelectedViewNeeded)
    this.saveSelectedView(tab.getId());
  this.saveSelectedViewNeeded = true;
}

RecordList.prototype.onTabUnselectListener = function(tab)
{
  var tabRow = document.getElementById(RegisterControl.statics.encodeId(this, tab.getId(), "view"));
  if(tabRow)
    tabRow.style.display = "none";

  tab.onHide.trigger();
}

RecordList.prototype.onDataChangeListener = function(rowCount, startIndex)
{
  if (this.pageControl)
    this.pageControl.setCurrentRows(rowCount, startIndex);
  this.updateLayout();
}

RecordList.prototype.onPageChangeListener = function(recordIndex)
{
  var selectedView = this.getCurrentView();
  if (selectedView)
    selectedView.setStartIndex(recordIndex);
}

RecordList.prototype.updateLayout = function()
{
  var tabRow = document.getElementById(RegisterControl.statics.encodeId(this, "tabRow"));

  var controlRow = document.getElementById(RegisterControl.statics.encodeId(this, "controlRow"));
  var selectedView = this.getCurrentView();
  if (selectedView)
  {
    var viewRow = document.getElementById(RegisterControl.statics.encodeId(this, selectedView.getId(), "view"));
    var remainingHeight = parseInt(document.body.offsetHeight) - Coordinate.Statics.findPos(viewRow).y - 2;
    remainingHeight -= controlRow.offsetHeight;
    viewRow.style.height = ((0<remainingHeight)?remainingHeight:1) + "px";
  }
  this.onReLayout.trigger();
}

RecordList.prototype.setSelectedViewCommand = function(command)
{
  this.selectedViewCommand = command;
}

RecordList.prototype.setQueryCallback = function(callback)
{
  this.queryCallback = callback;
}
RecordList.prototype.postCommand = function(command, listener)
{
  this.queryCallback(command, listener);
}
RecordList.prototype.returnJSON = function(json, listener)
{
  var jsObject = new Object();
  if (json && json.length)
    jsObject = eval(json);

  if (listener)
    listener.trigger(jsObject);
}
RecordList.prototype.returnError = function(message)
{
  alert(message);
}

RecordList.prototype.saveSelectedView = function(viewId)
{
  var command = this.selectedViewCommand + "," + viewId;
  this.postCommand(command, null);
}

RecordList.prototype.getSelectedContainerNodeId = function()
{
  var selectedView = this.getCurrentView();
  return selectedView.getSelectedContainerNodeId();
}



RecordList.statics = new Object();



function IRecordListPage()
{
  this.onShow = new REvent();
  this.onHide = new REvent();
  this.onDataChange = new REvent();
}
IRecordListPage.prototype.getId = function()
{
}
IRecordListPage.prototype.attach = function(node)
{
}
IRecordListPage.prototype.getSelectedKeys = function(actionFilter)
{
}
IRecordListPage.prototype.setStartIndex = function(recordIndex)
{
}
IRecordListPage.prototype.getSelectedContainerNodeId = function()
{
  return null;
}