// running in config.js or in iframe
// difference is pqrent in 
// Node.prototype.nodeselect = function ()

var folderImage     = 'images/_system/menu_depliant/default/TreeDirClosed.gif';
var openFolderImage = 'images/_system/menu_depliant/default/TreeDirOpen.gif';
var fileImage       = 'images/_system/menu_depliant/default/new.gif';
var lineImage       = 'images/_system/menu_depliant/default/TreeLine.gif';
var lImage          = 'images/_system/menu_depliant/default/TreeLine.gif';
var lMinusImage     = 'images/_system/menu_depliant/default/TreeMinus2.gif';
var lPlusImage      = 'images/_system/menu_depliant/default/TreePlus2.gif';
var tImage          = 'images/_system/menu_depliant/default/TreeBranch1.gif';
var tMinusImage     = 'images/_system/menu_depliant/default/TreeMinus1.gif';
var tPlusImage      = 'images/_system/menu_depliant/default/TreePlus1.gif';
var blankImage      = 'images/_system/menu_depliant/default/blank.gif';

var	objTree         = new Tree();

var node             = new Node("__ROOT");
node.isMaster        = true;
node.open            = true;
node.tree            = objTree;
objTree.nodes[objTree.nodes.length] = node;
objTree.rootnode     = node;

// preload images  
var img1 = new Image();
img1.src = folderImage;
var img2 = new Image();
img2.src = openFolderImage;
var img3 = new Image();
img3.src = fileImage;
var img4 = new Image();
img4.src = lineImage;
var img5 = new Image();
img5.src = lImage;
var img6 = new Image();
img6.src = lMinusImage;
var img7 = new Image();
img7.src = lPlusImage;
var img8 = new Image();
img8.src = tImage;
var img9 = new Image();
img9.src = tMinusImage;
var img10 = new Image();
img10.src = tPlusImage;
var img11 = new Image();
img11.src = blankImage;

var version = "1.0.1127";


function hi(object)
{
  //border-width: 1px; border-style: solid; border-color:" + this.tree.bordercolor + 
  object.style.backgroundColor  = objTree.hibackcolor;
/*  if ((objTree.hibordercolor == "transparent") && (objTree.bordercolor != "transparent")) {
    object.style.borderWidth      = "0px";
  } else {
    object.style.borderWidth      = "1px";
  }
*/  object.style.borderColor      = objTree.hibordercolor;
}

function lo(object)
{
  object.style.backgroundColor  = objTree.backcolor;
/*  if ((objTree.borderColor == "transparent") && (objTree.hibordercolor != "transparent")) {
    object.style.borderWidth      = "0px";
  } else {
    object.style.borderWidth      = "1px";
  }
*/  object.style.borderColor      = objTree.bordercolor;
}

function Tree()
{
  this.divname         = "menu";
  this.source          = "";
  this.mode            = 0;
  this.adapterURL      = "";
  this.id              = -1;
  this.nodes           = new Array();
  this.marging         = "0";
  ////this.tablebackcolor  = "white";
  //this.backcolor       = "white";
  //this.backcolor       = "transparent";
  this.backcolor       = "";
  //this.bordercolor     = "white";
  //this.bordercolor     = "transparent";
  this.bordercolor     = "";
  //this.background      = "white";
  this.font            = "Verdana";
  this.fontsize        = "10pt";
  this.fontcolor       = "black";
  //this.fontbackcolor   = "transparent";
  this.fontbackcolor   = "";
  this.fontbold        = false;
  this.fontitalic      = false;
  this.selectfontcolor = "black";
  //this.selectfontbackcolor = "transparent";
  this.selectfontbackcolor = "";

  this.hibackcolor     = "white";
  //this.hibackcolor     = "transparent";
  //this.hibordercolor   = "white";
  //this.hibordercolor   = "transparent";
  this.hibordercolor   = "";
  this.hifontcolor     = "black";

  this.folderIcon      = folderImage;
  this.openFolderIcon  = openFolderImage;
  this.icons           = true;
  this.highlight       = false;
  this._selNode        = null;
  this._scrollY        = 0;
  this.callback_select = null;
  this.callback_click  = null;
  this.isFolderOpenedWhenTextClicked = false;
  this.onNodeClick     = "";

  this.createNode      = __tree_createNode;
  this.setIconsBlank   = __tree_setIconsBlank;
  this._getID          = __tree_getID;
  this.draw            = __tree_create;
  this.create          = __tree_create;
};

function __tree_createNode(name,action,key,icon)
{
	var node = new Node(name,action,key, icon);
	node.tree = objTree;
	return node;
};

function __tree_setIconsBlank()
{
  lineImage       = blankImage;
  lImage          = blankImage;
  lMinusImage     = blankImage;
  lPlusImage      = blankImage;
  tImage          = blankImage;
  tMinusImage     = blankImage;
  tPlusImage      = blankImage;

  return false;
};

function __tree_getID()
{
  // get next id
  this.id++;
  return this.id;
}

Tree.prototype._nodeclick = function(id,y)
{
  this._scrollY = y;
  this.nodes[id].nodeclick();
}

Tree.prototype._nodeselect = function(id)
{
  this.nodes[id].nodeselect();
}

Tree.prototype.setMode = function (mode) {
  this.mode =  mode;
  if(this.mode == 1) {
    if(this.adapterURL != "") {
      __comm = new Comm(this.adapterURL);
    }
  }
};

Tree.prototype.getMode = function ()
{
  return this.mode;
};

Tree.prototype.setAdapterURL = function (url)
{
  this.adapterURL =  url;
};

Tree.prototype.add = function (node)
{
  // add the new node to the internal root node
  this.rootnode.add(node);
};

Tree.prototype.createHTML = function ()
{
  var html = "";
  var crlf = "\r";

  html +="<table width='100%' bgcolor=\"" + this.backcolor + "\" border='0' cellspacing='0'>" + crlf;
  html +="<tr>" + crlf;
  html +="<td nowrap>" + crlf;
  for (var i = 0; i < this.rootnode._subNodes.length; i++) {
    html += this.rootnode._subNodes[i].createHTML(i,this.rootnode._subNodes.length) + crlf;
  }

  html +="</td>" + crlf;
  html +="</tr>" + crlf;
  html +="</table>" + crlf;

  return html;
};

Tree.prototype.getSelectedNode = function ()
{
  return this._selNode;
}

function __tree_create()
{
  document.getElementById(this.divname).style.marginLeft = this.marging+"px";
  document.getElementById(this.divname).style.marginRight = this.marging+"px";
  document.getElementById(this.divname).style.marginTop = this.marging+"px";
  document.getElementById(this.divname).style.marginBottom = this.marging+"px";
  document.getElementById(this.divname).style.backgroundColor = this.backcolor;
  document.getElementById(this.divname).innerHTML = this.createHTML();
}

function Node(name, action, key, icon)
{
  this._subNodes  = [];
  this._lastNode  = false;
  this.name       = name;
  this.action     = action || "javascript:void(0);";
  this.id         = objTree._getID();
  this.expanded   = false;
  this.key        = key;
  this.isRoot     = false;
  this.icon       = icon;
  this.parent     = null;
	this.tree       = objTree;
};

Node.prototype.add = function (node)
{
  node.parent = this;
  this._subNodes[this._subNodes.length] = node;
  // hold the tree in array, necessary for identifying node after call back
  this.tree.nodes[this.tree.nodes.length] = node;
  if(this.name == "__ROOT")
    // make this node to root node, because it is the first visible node
    node.isRoot = true;
};

Node.prototype.nodeclick = function ()
{
  if (this.expanded) {
    this.collapse();
  } else {
    this.expand();
  }
}

Node.prototype.nodeselect = function ()
{

  if(this.tree._selNode) {
    this.tree._selNode.deselect();
  }
  this.tree._selNode = this;

  document.getElementById(this.id + '.0').style.backgroundColor = this.tree.selectfontbackcolor;
  document.getElementById(this.id + '.0').style.color = this.tree.selectfontcolor;

  if(this.tree.onNodeClick != "") {
    //eval("parent." + this.tree.onNodeClick);
    eval(this.tree.onNodeClick);
  } else {
    document.location.href = this.action;
  }
}

Node.prototype.deselect = function ()
{
  var color = "";
  var backcolor = "";

  if(!this.fontcolor)
    color = this.tree.fontcolor;

  if(this.fontbackcolor)
    backcolor = this.fontbackcolor;
  else if(!this.fontbackcolor && this.tree.fontbackcolor)
    backcolor = this.tree.fontbackcolor;
  else
    backcolor = "";

  var obj = document.getElementById(this.id + '.0');
  if(obj != null) {
    obj.style.backgroundColor = backcolor;
    obj.style.color = color;
  }
}

Node.prototype.expand = function ()
{
  this.expanded = true;

  if(this.tree.mode == 1) {
    // delete all sub items
    this._subNodes = [];
    this._lastNode  = false;
    // save current tree
    this.tree.currentNode = this;
    // get nodes from server
    this.tree.getSubTree();
  } else {
    this.tree.create();
  }
}

Node.prototype.collapse = function ()
{
  this.expanded = false;
  this.tree.create();
}

Node.prototype.activate = function ()
{
  this.nodeselect();
  eval(this.action);
}

Node.prototype.createHTML = function (node,nodeCount)
{
  var parentNode = this.parent;
  var indent     = '';
  var icon       = "";
  var crlf       = "\r";
  var font       = "";
  var size       = "";
  var color      = "";
  var backcolor  = "";
  var bold       = false;
  var italic     = false;
  var id         = "";
  var scroll     = "";

  if(!this.font)
    font = "font-family:" + this.tree.font + ";";
  else
    font = "font-family:" + this.font + ";";
  if(!this.fontsize)
    size = "font-size:" + this.tree.fontsize + ";";
  else
    size = "font-size:" + this.fontsize + ";";
  if(!this.fontcolor)
    color = "color:" + this.tree.fontcolor + ";";
  else
    color = "color:" + this.fontcolor + ";";
  if(!this.fontbackcolor && this.tree.fontbackcolor != "")
    backcolor = "background-color:" + this.tree.fontbackcolor + ";";
  else if(this.fontbackcolor)
    backcolor = "background-color:" + this.fontbackcolor + ";";
  else
    backcolor = "";
  if(this.fontbold)
    bold = "font-weight:bold;";
  else
    bold = "font-weight:normal;";

  if (node + 1 == nodeCount) {
    // determine last node
    this.parent._lastNode = true;
  }

  while (parentNode.parent && !parentNode.parent.isMaster) {
    parentNode = parentNode.parent;
    icon = parentNode._lastNode ? blankImage : lineImage;
    indent = "<td width=\"1%\"><img border=0 src=\"" + icon + "\"></td>" + indent;
  }
  parentNode = this.parent;
  if(this.tree.mode == 1) {
    this.folder = 1;
  } else {
    if (this._subNodes.length) {
      this.folder = 1;
    }
  }
  if(this.tree.highlight && (browser.ie || browser.ns)) {
    if ((this.tree.bordercolor == "transparent") || (this.tree.bordercolor == "") || (this.tree.hibordercolor == "transparent") || (this.tree.hibordercolor == "")) {
      var html = "<table border=0 onmouseover=\"javascript:hi(this);\" onmouseout=\"javascript:lo(this);\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">"
    } else {
      var html = "<table style=\"border-width: 1px; border-style: solid; border-color:" + this.tree.bordercolor + ";\" onmouseover=\"javascript:hi(this);\" onmouseout=\"javascript:lo(this);\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">"
    }
  } else {
    var html = "<table cellspacing=\"0\" cellpadding=\"0\">"
  }
  if (this.folder) {
    if (!this.icon) {
      this.icon = this.tree.folderIcon;
    }
    if (!this.openIcon) { this.openIcon = this.tree.openFolderIcon; }
    if(this.expanded)
      this.icon = this.tree.openFolderIcon;
    else
      this.icon = this.tree.folderIcon;

    html += "<tr>" + crlf;
    html += indent + crlf;
    icon = ((this.expanded)?((this.parent._lastNode)?lMinusImage:tMinusImage):((this.parent._lastNode)?lPlusImage:tPlusImage));
    if(browser.ie)
      scroll = ",document.body.scrollTop";
    else
      scroll = ",window.pageYOffset";
    if(!parentNode.isMaster) {
      html += "<td width=\"1%\"><a  style=\"text-decoration: none;\" href=\"javascript:" + "objTree._nodeclick(" + this.id + scroll + ");\"><img id=\"" + this.id + ".1\" border=0 src=\"" + icon + "\" ></a></td>" + crlf;
    }
    if(this.tree.icons)
      html += "<td width=\"1%\"><a style=\"text-decoration: none;\"  href=\"javascript:" + "objTree._nodeclick(" + this.id + scroll + ");\"><img id=\"" + this.id + ".2\" border=0 src=\"" + this.icon + "\"  ></a></td>" + crlf;
    id = "id=\"" + this.id + ".0\"";

    if(this.tree.isFolderOpenedWhenTextClicked)
      html += "<td nowrap><a style=\"text-decoration: none;\"  href=\"javascript:" + "objTree._nodeclick(" + this.id + scroll + ");\" href=\"" + this.action + "\" ><div " + id + " style=\"text-decoration: none;" + bold + font + size + color + backcolor + "\">" + this.name + "</div></a></td>" + crlf;
    else
      html += "<td nowrap><a style=\"text-decoration: none;\"  href=\"javascript:" + "objTree._nodeselect(" + this.id + ");\" href=\"" + this.action + "\"><div " + id + " style=\"text-decoration: none;" + bold + font + size + color + backcolor + "\">" + this.name + "</div></a></td>" + crlf;
    html += "</tr>" + crlf;
    html += "</table>" + crlf;

    if(this.expanded) {
      for (var i = 0; i < this._subNodes.length; i++) {
        html += this._subNodes[i].createHTML(i,this._subNodes.length) + crlf;
      }
      this._lastNode = false;
    }

  } else {
    if (!this.icon) {
      this.icon = fileImage;
    }
    html += "<tr>" + crlf;
    html += indent + crlf;
    icon = this.parent._lastNode ? lImage : tImage;
    if(!parentNode.isMaster) {
      html += "<td width=\"1%\"><img id=\"" + this.id + ".1\" border=0 src=\"" + icon + "\"></td>" + crlf;
    }
    if(this.tree.icons)
      html += "<td width=\"1%\"><a href=\"javascript:" + "objTree._nodeselect(" + this.id + ");\"><img id=\"" + this.id + ".2\" border=0 src=\"" + this.icon + "\" ></a></td>" + crlf;
//    if(!browser.ns4) {
      id = "id=\"" + this.id + ".0\"";
//    }
    html += "<td nowrap><a style=\"text-decoration: none;\" href=\"javascript:objTree._nodeselect(" + this.id + ");\"><div " + id + " style=\"" + bold + font + size + color + backcolor + "\">" + this.name + "</div></a></td>" + crlf;

    html += "</tr>" + crlf;
    html += "</table>" + crlf;
  }
  this.plusIcon = ((this.parent._lastNode) ? lPlusImage : tPlusImage);
  this.minusIcon = ((this.parent._lastNode) ? lMinusImage : tMinusImage);
  //alert(html);
  return html;
}

