var folderImage     = 'design/image/TreeDirClosed.gif';
var openFolderImage = 'design/image/TreeDirOpen.gif';
var lineImage       = 'design/image/TreeLine.gif';
var lImage          = 'design/image/TreeBranch2.gif';
var lMinusImage     = 'design/image/TreeMinus2.gif';
var lPlusImage      = 'design/image/TreePlus2.gif';
var tImage          = 'design/image/TreeBranch1.gif';
var tMinusImage     = 'design/image/TreeMinus1.gif';
var tPlusImage      = 'design/image/TreePlus1.gif';
var alfanodes       = [];

//var selborder = "green";	//SELECT BORDERFARVE
//var selback = "cyan";		//SELECT BGFARVE
var __tree;

function hi(object)
{

//  object.style.backgroundColor  = selback // "#ffb6c1";
//  object.style.borderColor =  selborder  //"rgb(176,8,95)";  

    object.className = "highitem"

  
}

function lo(object)
{

  object.className = "menuitem"

//  if (( menubackground != "" )) 
//     { object.style.backgroundColor = "" }
//  else 
//     { object.style.backgroundColor  = __tree.backcolor;  }
//  object.style.borderColor      = __tree.backcolor;


}

function Tree()
{
  this.source          = "";
  this.mode            = 0;    //0=egen frame   1=direktelink?
  this.adapterURL      = "";
  __tree               = this;
  this.id              = -1;
  this.nodes           = [];
  this.backcolor       = "white";  //BENYTTES IKKE
  this.tablebackcolor  = "white";  //BENYTTES IKKE
  this.background      = "url(images/closed.gif)";  //BENYTTES IKKE
  this.font            = "Arial";  //BENYTTES IKKE
  this.fontsize        = "10px";   //BENYTTES IKKE
//  this.fontcolor       = menucolor //"black";  //MENUENS STANDARDFARVE
  this.fontcolor       = "";
  this.fontbackcolor   = "";	   //MENUITEMS BGFARVE
  this.fontbold        = false;
  this.fontitalic      = false;
  this.selectfontcolor = "green";  //BENYTTES IKKE
  this.selectfontbackcolor = "";
  this.folderIcon      = folderImage;
  this.openFolderIcon  = openFolderImage;
  this.icons           = true;
  this.highlight       = false;
  var node             = new Node("__ROOT");
  node.isMaster        = true;
  node.open            = true;
  node.tree            = this;
  this.nodes[this.nodes.length] = node;
  this.rootnode        = node;
  this._selNode        = null;
  this._glNode	       = null;
  this._scrollY        = 0;
  this.callback_select = null;
  this.callback_click  = null;
  this.isFolderOpenedWhenTextClicked = false;
  this.onNodeClick     = "";
  this.marginTop       = "0";
  this.marginLeft      = "10";
  this.marginRight     = "5";
}

Tree.prototype.setIconsBlank = function()
{
  lineImage       = blankimage;
  lImage          = blankimage;
  lMinusImage     = blankimage;
  lPlusImage      = blankimage;
  tImage          = blankimage;
  tMinusImage     = blankimage;
  tPlusImage      = blankimage;
  return false;
}

Tree.prototype._getID = function()
{
  // 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)
{  
  parent.setcurrent(this.nodes[id].key)
  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 +="<table width='100%' 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;
}

Tree.prototype.draw = function ()
{
//  document.body.style.marginLeft = this.marginLeft;
//  document.body.style.marginRight = this.marginRight;
//  document.body.style.marginTop = this.marginTop;
//  document.body.style.backgroundColor = this.backcolor;
//  if (( menubackground != "" ))
//     { document.body.style.background=menubackground; }
  document.body.innerHTML = "<span id=menutitel></span>"
  var tekst = "<div class=menuover>" + menutitel + "</div><hr>" 
  if (( men.length == 0 ))
     { if (( administrator == true ))
        { tekst += "<div class=menuover>" + gettekst(2) + "</div><hr>" +
         "<input class=admknap type=button value='" + gettekst(3) +"' onclick=NewItem()>"
        }
     }
  
  document.body.innerHTML = tekst + this.createHTML()

    



//  document.body.innerHTML = "<table height=100% cellpadding=0 cellspacing=0><tr><td valign=top>" +
//     this.createHTML() +
//     "</td></tr><tr><td valign=bottom><input class=knap type=button value='Edit Page'></td></tr>" +
//     "<tr><td></td></tr></table>"
}

function Node(name, action, key, hidn, icon)
{
  this._subNodes  = [];
  this._lastNode  = false;
  this.name       = name;
  this.action     = action || "javascript:void(0);";
  this.id         = __tree._getID();
  this.expanded   = false;
  this.key        = key;
  this.hidn 	  = hidn;
  this.tree       = null;
  this.isRoot     = false;
  this.icon       = icon;
  this.parent     = null;
};

Node.prototype.add = function (node)
{
  node.parent = this;
  node.tree   = this.tree;
  this._subNodes[this._subNodes.length] = node;
  alfanodes[node.key] = 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 ()
{  
  parent.setcurrent(0)
  if (this.expanded) {
    this.collapse();
    if ((administrator == true)) {admNode(this,"C");}
  } else {
    this.expand();
    if ((administrator == true)) {admNode(this,"O");}    
  }
}

Node.prototype.nodeselect = function ()
{
  if (( dokurettet == true ))
    {
     if (( !window.confirm(gettekst(1)) ))
       { return }
     parent.menu.dokurettet=false
    } 


  if(this.tree._selNode) {
    this.tree._glNode = 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;
  document.getElementById(this.id + '.0').className = "selitemtext"

//  document.getElementById(this.id + '.0').style.textDecoration = "underline";
//  document.getElementById(this.id + '.0').style.fontWeight = "bold";
  if(this.tree.onNodeClick != "")
    eval(this.tree.onNodeClick);
}

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.className = "itemtext";
//    obj.style.backgroundColor = backcolor;
//    obj.style.color = color;
//    obj.style.textDecoration = "";
//    obj.style.fontWeight = "normal";
  }
}

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.draw();
  }
}

Node.prototype.collapse = function ()
{
  this.expanded = false;
  this.tree.draw();

}

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     = "";
  var dyt        = this.action;

  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 && (is.ie || is.ns6))
    {    
//    var html = "<table style=\"border-width: 1px; border-style: solid; border-color:" + this.tree.backcolor + ";\" 
    var html = "<table class=menuitem " +
    " onmouseover=\"javascript:hi(this);\" onmouseout=\"javascript:lo(this);\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\""
    if (!this.folder) 
       {  html += " onclick=\"javascript:" + "__tree._nodeselect(" + this.id + ")\"" }
    else
       {  html += " onclick=\"javascript:" + "__tree._nodeclick(" + this.id + scroll +  ")\"" }
    html += ">"
    }
  else
    {
    var html = "<table cellspacing=\"0\" cellpadding=\"0\">"
    }


  if (this.folder) {
    if (!this.icon) {
      if ( this.hidn == "H" )
      { this.icon = hopenimage }
      else 
      { this.icon = this.tree.folderIcon;}
      
    }
    if (!this.openIcon)
       { if ( this.hidn == "H" )  
         { this.openIcon = hopenimage }
         else 
         { this.openIcon = this.tree.openFolderIcon; }
       }
    if(this.expanded)
      { if ( this.hidn == "H" )  
         { this.icon = hopenimage }
         else 
         { this.icon = this.tree.openFolderIcon; }
       }
//      {  this.icon = this.tree.openFolderIcon; }

    else
      { if ( this.hidn == "H" )  
         { this.icon = hclosedimage }
         else 
         { this.icon = this.tree.folderIcon; }
       } 
//      { 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(is.ie)
      scroll = ",document.body.scrollTop";
    else
      scroll = ",window.pageYOffset";
    if(!parentNode.isMaster) {
      html += "<td width=\"1%\"><a  style=\"text-decoration: none;\" href=\"javascript:void(0);\"" +
//      " onclick=\"javascript:" + "__tree._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:void(0);\"" +
//      " onclick=\"javascript:" + "__tree._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=\"" + this.action + "\" target=\"" +
      this.tree.target + "\"" +
//    " onclick=\"" + "__tree._nodeclick(" + this.id + scroll + ");\"" +
//    "><div " + id + " style=\"text-decoration: none;" + bold + font + size + color + backcolor + "\">F" +
      "><div " + id + " class=foldertext>" +
     unescape(this.name) + "</div></a></td>" + crlf;

    else

      html += "<td nowrap><a style=\"text-decoration: none;\"  href=\"" + this.action + "\" target=\"" +
      this.tree.target + "\"" +
//   " onclick=\"" + "__tree._nodeselect(" + this.id + ");\"" +
      "><div " + id + " style=\"text-decoration: none;" + bold + font + size + color + backcolor + "\">FF" +
      unescape(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) {
      if ( this.hidn == "H" )
      { this.icon = hiddenimage }
      else
      { 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:void(0);\"" +
//     " onclick=\"" + "__tree._nodeselect(" + this.id + ");\"" +
"><img id=\"" + this.id + ".2\" border=0 src=\"" + this.icon + "\" ></a></td>" + crlf;
    if(!is.ns4) {
      id = "id=\"" + this.id + ".0\"";
    }
    html += "<td nowrap><a style=\"text-decoration: none;\" href=\"javascript:void(0);\"" +
//  " onclick=\"javascript:__tree._nodeselect(" + this.id + ");\"" +
//  "><div " + id + " style=\"" + bold + font + size + color + backcolor + "\">D" +
    "><div " + id + " class=itemtext>" +
    unescape(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);
  return html;
}


function gettekst(id)
{
  if(language=="EN") {
    if(id=='1') return "Page has been modified - Continue without saving ?";
    if(id=='2') return "No menu items...";
    if(id=='3') return "Add Start Page";
  }
  if(language=="DA") {
    if(id=='1') return "Siden er ændret - vil du fortsætte uden at gemme ?";
    if(id=='2') return "Ingen sider i denne menu...";
    if(id=='3') return "Tilføj Startside";
  }

}
