subclass(RecordListTree, IDropTarget, IRecordListPanelChildContent, HierarchyTreeSingleSelect);
function RecordListTree( id )
{
  HierarchyTreeSingleSelect.call( this, id );
  
  this.parent = null;
  this.enableViewDown = false;
  this.viewDown = false;
 
  this.onDragOver = new REvent();
  this.onDragOut  = new REvent(); 
  this.onDrop     = new REvent();
  this.onViewDownChange = new REvent();
  
  this.firstResize = true;
  
  this.onMouseOver.addEventListener(new REventListener(RecordListTree.prototype.onMouseOverEventListener, this));
  this.onMouseOut.addEventListener(new REventListener(RecordListTree.prototype.onMouseOutEventListener, this));
}
RecordListTree.prototype.onMouseOverEventListener = function(node)
{
  DragMonitor.statics.getInstance().setDropTarget(this, node); 
}
RecordListTree.prototype.onMouseOutEventListener = function()
{
  DragMonitor.statics.getInstance().removeDropTarget();
}

RecordListTree.prototype.setEnableViewDown = function(enableViewDown)
{
  this.enableViewDown = enableViewDown;
}
RecordListTree.prototype.setViewDown = function(viewDown)
{
  this.viewDown = viewDown;
  this.setHighlightSubNodes(this.viewDown);
}
RecordListTree.prototype.getViewDown = function()
{
  return this.viewDown;
}

RecordListTree.prototype.attach = function( e )
{
  if (this.enableViewDown)
  {
    var buttonBar = document.createElement("div");
    buttonBar.id = RegisterControl.statics.encodeId(this, "buttonBar");
    buttonBar.style.paddingBottom = '3px';
    e.appendChild(buttonBar);

    var checkbox = document.createElement("input");
    checkbox.type = 'checkbox';
    checkbox.id = RegisterControl.statics.encodeId(this, "viewDownCheckbox");
    checkbox.onclick = RecordListTree.prototype.onViewDownChange;
    buttonBar.appendChild(checkbox);
    checkbox.checked = this.getViewDown();  // after the appendChild so ie will keep the checked value
    var label = document.createElement("label");
    label.htmlFor = checkbox.id;
    label.appendChild(document.createTextNode('Include sub-departments'));
    buttonBar.appendChild(label);

    var treeArea = document.createElement("div");
    treeArea.id = RegisterControl.statics.encodeId(this, "treeArea");
    treeArea.style.overflow = "auto";
    e.appendChild(treeArea);
      
    HierarchyTree.prototype.attach.call(this, treeArea);
  }
  else
  {
    HierarchyTree.prototype.attach.call(this, e);
  }
}

RecordListTree.prototype.init = function(parent)
{
  this.parent = parent;
  
  if(this.enableViewDown && this.parent.onResize)
    this.parent.onResize.addEventListener(new REventListener(RecordListTree.prototype.onResizeListener, this));
}

RecordListTree.prototype.handlesScrolling = function()
{
  return !(this.enableViewDown);
}

RecordListTree.prototype.onResizeListener = function()
{
  var treeArea = document.getElementById(RegisterControl.statics.encodeId(this, "treeArea"));
  var buttonBar = document.getElementById(RegisterControl.statics.encodeId(this, "buttonBar"));
  
  var remainingHeight = parseInt(treeArea.parentNode.offsetHeight) - buttonBar.offsetHeight - 2;
  var remainingWidth = parseInt(treeArea.parentNode.offsetWidth) - 2;

  // work around the scrollbars not appearing initially in ie7
  if (this.firstResize)
    remainingHeight -= 1;

  treeArea.style.height = ((0<remainingHeight)?remainingHeight:1) + "px";
  treeArea.style.width = ((0<remainingWidth)?remainingWidth:1) + "px";
  
  this.firstResize = false;
}

RecordListTree.prototype.onViewDownChange = function()
{
	//Click handler for the check box 'Include sub departments'
  var params = new Array();
  var instance = RegisterControl.statics.decodeId(this.id, params);
  var viewDownCheckbox = document.getElementById(RegisterControl.statics.encodeId(instance, "viewDownCheckbox"));
  instance.setViewDown(viewDownCheckbox.checked);
  instance.onViewDownChange.trigger();
}