/*
 * File version  $Revision: 17 $
 * Last modified $Date    :  $

 * Papirfly extensions to the xTREE Cross Browser Tree Widget by WebFX stored in the file xtree.js
 * Please avoid making changes to the original file; make your changes here and make sure that
 * this file gets included after the original.
 *
 * NB! I hope you remember to branch this file in SourceSafe before making your changes!
 */


// Override: new default file names.
// New option (autoSlam) to automatically close nodes as they loose focus to other nodes.
// New option (showRoot) to display root node or not.
var webFXTreeConfig = {
  rootIcon        : 'xtree/images/folder.png',
  openRootIcon    : 'xtree/images/folderopen.png',
  folderIcon      : 'xtree/images/folder.png',
  openFolderIcon  : 'xtree/images/folderopen.png',
  fileIcon        : 'xtree/images/document.png',
  iIcon           : 'xtree/images/I.png',
  lIcon           : 'xtree/images/L.png',
  lMinusIcon      : 'xtree/images/Lminus.png',
  lPlusIcon       : 'xtree/images/Lplus.png',
  tIcon           : 'xtree/images/T.png',
  tMinusIcon      : 'xtree/images/Tminus.png',
  tPlusIcon       : 'xtree/images/Tplus.png',
  blankIcon       : 'xtree/images/blank.png',
  defaultText     : 'Tree Item',
  defaultAction   : 'javascript:void(0);',
  defaultBehavior : 'classic',
  usePersistence  : true,
  autoSlam        : true,
  showRoot        : false
};



/*
 * WebFXTreeAbstractNode class
 */

// New: collapse all siblings for this node
WebFXTreeAbstractNode.prototype.collapseSiblings = function() {
  if (!this.parentNode) return;
  for (var i = 0; i < this.parentNode.childNodes.length; i++) {
    if (this != this.parentNode.childNodes[i] && this.parentNode.childNodes[i].childNodes.length) {
      this.parentNode.childNodes[i].collapse(true);
    }
  }
}

/*
// New: collapse all nodes in the same generation as this node
WebFXTreeAbstractNode.prototype.collapseMyGeneration = function() {
  if (!this.parentNode) return;
  for (var i = 0; i < webFXTreeHandler.idCounter; i++) {
    var that = webFXTreeHandler.all[xxxxx];
    if (this != this.parentNode.childNodes[i] && this.parentNode.childNodes[i].childNodes.length) {
      this.parentNode.childNodes[i].collapse(true);
    }
  }
}
*/

// Override: collapse open node(s) after toggling this one if autoSlam options are on.
WebFXTreeAbstractNode.prototype.toggle = function() {
  if (this.folder) {
    if (this.open) { this.collapse(); }
    else { this.expand(); }
  }
  if (webFXTreeConfig.autoSlam)
    this.collapseSiblings();
    //this.collapseMyGeneration();
}

// Override: don't collapse pre-expanded nodes.
WebFXTreeAbstractNode.prototype.doCollapse = function() {
  if (this.jammed) return false;
  if (webFXTreeHandler.behavior == 'classic') { document.getElementById(this.id + '-icon').src = this.icon; }
  if (this.childNodes.length) { document.getElementById(this.id + '-cont').style.display = 'none'; }
  this.open = false;
  if (webFXTreeConfig.usePersistence)
    webFXTreeHandler.cookies.setCookie(this.id.substr(18,this.id.length - 18), '0');
}



/*
 * WebFXTree class
 */

// Override: generate an additional <div> for use in our custom css file (xtree_papirfly.css).
// Override: replace "ondblclick" with onclick".
// Override: Don't produce HTML for root folder - keep it invisible.
WebFXTree.prototype.toString = function() {
  var str = "<div class=\"ptn-tree-menu\">";
  if (webFXTreeConfig.showRoot) {
	str += "<div id=\"" + this.id + "\" onclick=\"webFXTreeHandler.select(this);\" class=\"";
    str += (this.className)?this.className:"webfx-tree-item";
	str += "\" onkeydown=\"return webFXTreeHandler.keydown(this, event)\">";
	str += "<img id=\"" + this.id + "-icon\" class=\"webfx-tree-icon\" src=\"" + ((webFXTreeHandler.behavior == 'classic' && this.open)?this.openIcon:this.icon) + "\" onclick=\"webFXTreeHandler.select(this);\">";
	str += "<a href=\"" + this.action + "\" id=\"" + this.id + "-anchor\" onfocus=\"webFXTreeHandler.focus(this);\" onblur=\"webFXTreeHandler.blur(this);\">" + this.text + "</a></div>";
  } else {
    str += "<div id=\"" + this.id + "\" class=\"webfx-tree-item\" style=\"display: none\">";
    str += "<img id=\"" + this.id + "-icon\" class=\"webfx-tree-icon\" src=\"xyz\">";
    str += "<a href=\"#\" id=\"" + this.id + "-anchor\">" + this.text + "</a></div>";
  }
  str += "<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
  for (var i = 0; i < this.childNodes.length; i++) {
    str += this.childNodes[i].toString(i, this.childNodes.length);
  }
  str += "</div></div>";
  this.rendered = true;
  return str;
};

// New prototype property - class name
WebFXTree.prototype.setClass = function (sClass) {
  this.className = sClass;
}



/*
 * WebFXTreeItem class
 */

// Override: generate a custom class (if requested) for use in our custom css file (xtree_papirfly.css).
// Override: replace "ondblclick" with onclick".
// Override: pre-expand jammed nodes.
// Removed: deleted onclick from plus-img.
// New: added indent class to plus-img.
WebFXTreeItem.prototype.toString = function (nItem, nItemCount) {
  var foo = this.parentNode;
  var indent = '';
  if (nItem + 1 == nItemCount) { this.parentNode._last = true; }
  var i = 0;
  while (foo.parentNode) {
    foo = foo.parentNode;
    indent = "<img id=\"" + this.id + "-indent-" + i + "\" class=\"indent\" src=\"" + ((foo._last)?webFXTreeConfig.blankIcon:webFXTreeConfig.iIcon) + "\">" + indent;
    i++;
  }
  this._level = i;
  if (this.childNodes.length) { this.folder = 1; }
  else { this.open = false; }
  if (this.jammed) { this.open = true; }
  if ((this.folder) || (webFXTreeHandler.behavior != 'classic')) {
    if (!this.icon) { this.icon = webFXTreeConfig.folderIcon; }
    if (!this.openIcon) { this.openIcon = webFXTreeConfig.openFolderIcon; }
  }
  else if (!this.icon) { this.icon = webFXTreeConfig.fileIcon; }
  var label = this.text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  var str = "<div id=\"" + this.id + "\" " + ((this.jammed)?"":"onclick=\"webFXTreeHandler.toggle(this);\" ") + "class=\"";
  str += (this.className)?this.className:"webfx-tree-item";
  str += "\" onkeydown=\"return webFXTreeHandler.keydown(this, event)\">";
  str += indent;
  str += "<img id=\"" + this.id + "-plus\" class=\"plus\" src=\"" + ((this.folder)?((this.open)?((this.parentNode._last)?(this.jammed?webFXTreeConfig.lIcon:webFXTreeConfig.lMinusIcon):(this.jammed?webFXTreeConfig.tIcon:webFXTreeConfig.tMinusIcon)):((this.parentNode._last)?webFXTreeConfig.lPlusIcon:webFXTreeConfig.tPlusIcon)):((this.parentNode._last)?webFXTreeConfig.lIcon:webFXTreeConfig.tIcon)) + "\">"
  str += "<img id=\"" + this.id + "-icon\" class=\"webfx-tree-icon\" src=\"" + ((webFXTreeHandler.behavior == 'classic' && this.open)?this.openIcon:this.icon) + "\" onclick=\"webFXTreeHandler.select(this);\"><a href=\"" + this.action + "\" id=\"" + this.id + "-anchor\" onfocus=\"webFXTreeHandler.focus(this);\" onblur=\"webFXTreeHandler.blur(this);\">" + label + "</a></div>";
  str += "<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
  for (var i = 0; i < this.childNodes.length; i++) {
    str += this.childNodes[i].toString(i,this.childNodes.length);
  }
  str += "</div>";
  this.plusIcon = ((this.parentNode._last)?webFXTreeConfig.lPlusIcon:webFXTreeConfig.tPlusIcon);
  this.minusIcon = ((this.parentNode._last)?webFXTreeConfig.lMinusIcon:webFXTreeConfig.tMinusIcon);
  return str;
}

// New prototype property - class name
WebFXTreeItem.prototype.setClass = function (sClass) {
  this.className = sClass;
}

// New prototype property - whether node can be opened/closed (default) or permanently jammed open.
WebFXTreeItem.prototype.jam = function () {
  this.jammed = true;
}

// Override: we need to stop navigation to the parent of top level nodes as they do not have a visible/selectable parent.
WebFXTreeItem.prototype.keydown = function(key) {
  if ((key == 39) && (this.folder)) {
    if (!this.open) { this.expand(); }
    else { this.getFirst().select(); }
    return false;
  }
  else if (key == 37) {
    if (this.open) { this.collapse(); }
    //else { this.parentNode.select(); }    // OK it's a hack but it works (TimT).
    return false;
  }
  else if (key == 40) {
    if (this.open) { this.getFirst().select(); }
    else {
      var sib = this.getNextSibling();
      if (sib) { sib.select(); }
    }
    return false;
  }
  else if (key == 38) { this.getPreviousSibling().select(); return false; }
  return true;
}

