function Element(name)
{
  this.name=name;
  this.id=-1;
  this.level=0;
  this.children=new Array;
  this.childcount=0;
  
  this.expanded=false;
  this.greyed=false;
  this.selected=false;
  this.selectimg=0;
  this.nodeimg=0;
  this.div=0;
  this.endnode=false;  

  this.parent=null;
  this.addChild=addChild;
  this.draw=drawfolder;
  this.createID=createID;
  this.expand=expandfolder;
  this.collapse=collapsefolder;
  this.select=switchselectfolder;
  this.grey=switchgreyfolder;
  this.selection=selection;
}

function addChild(child)
{
  this.children[this.childcount]=child;
  this.childcount++;
  child.parent=this;
}

function drawfolder(level,endnode,precode)
{
  var i;
  this.createID();
  this.level=level;
  this.endnode=endnode;
  if(level==0)
  {
    precode="<table border='0' cellspacing='0' cellpadding='0'><tr>";
    this.expanded=true;
    document.write("<div id='selecttree' style='display:block; margin-top:5px; margin-left:75px; margin-bottom:5px'>");
  }
  document.write("<div id='folder");
  document.write(this.id);
  if(level>1)
    document.write("' style='display:none'>");
  else
    document.write("' style='display:block'>");
  document.write(precode);
  if(level>0)
  {
    document.write("<td><img src='");
    document.write(imgpath);
    if(endnode)
    {
      if(this.childcount==0)
      {
        document.write("stendnode.gif'");
      }
      else
      {
        document.write("stcolendnode.gif'");
        document.write("onClick='switchfolder("+this.id+")'"); 
        document.write("id='nodeimg"+this.id+"'");
      }
    }
    else
    {
      if(this.childcount==0)
      {
        document.write("stnode.gif'");
      }
      else
      {
        document.write("stcolnode.gif'");
        document.write("onClick='switchfolder("+this.id+")'");
        document.write("id='nodeimg"+this.id+"'");
      }
    }
    document.write("><td><img src='");
    document.write(imgpath);
    document.write("stunsel.gif'");
    document.write("onClick='switchselect("+this.id+")'");
    document.write("id='selectimg"+this.id+"'");
    document.write("></td>");
  }
  document.write("<td>");
  document.write(this.name);
  document.write("</td></tr></table></div>");
  if(this.childcount>0)
  {
    //childs!!
    
    if(level>0)
    {
      if(endnode)
      {
        precode+="<td><img src='"+imgpath+"stempty.gif'></td>";
      }
      else
      {
        precode+="<td><img src='"+imgpath+"stline.gif'></td>";
      }
    }

    for(i=0;i<this.childcount;i++)
    {
      if(i==this.childcount-1)
        this.children[i].draw(level+1,true,precode);
      else
        this.children[i].draw(level+1,false,precode);
    }  
  }
  if(level==0)
    document.write("</div>");
  //zum schluss selectimg und nodeimg setzen
  if(level>0)
  {
    this.div=document.getElementById("folder"+this.id);
    this.nodeimg=document.getElementById("nodeimg"+this.id);
    this.selectimg=document.getElementById("selectimg"+this.id);
  }
}

function expandfolder()
{
  var i;
  for(i=0;i<this.childcount;i++)
    this.children[i].div.style.display="block";
  if(this.endnode)
    this.nodeimg.src=imgpath+"stexpendnode.gif";
  else
    this.nodeimg.src=imgpath+"stexpnode.gif";
}

function collapsefolder()
{
  var i;
  for(i=0;i<this.childcount;i++)
    this.children[i].collapse();
  this.div.style.display="none";
  if(this.childcount>0)
  {
    if(this.endnode)
      this.nodeimg.src=imgpath+"stcolendnode.gif";
    else
      this.nodeimg.src=imgpath+"stcolnode.gif";
  }
  this.expanded=false;
}

function switchselectfolder()
{
  var i;
  if(!this.greyed)
  {
    this.selected=!this.selected;
    if(this.selected)
      this.selectimg.src=imgpath+"stsel.gif";
    else
      this.selectimg.src=imgpath+"stunsel.gif";
    for(i=0;i<this.childcount;i++)
      this.children[i].grey();
  } 
}

function switchgreyfolder()
{
  var i;
  this.greyed=!this.greyed;
  if(this.selected)
  {
    if(this.greyed)
      this.selectimg.src=imgpath+"stselgrey.gif";
    else
      this.selectimg.src=imgpath+"stsel.gif";
  }
  else
  {
    if(this.greyed)
      this.selectimg.src=imgpath+"stselgrey.gif";
    else
      this.selectimg.src=imgpath+"stunsel.gif";
    for(i=0;i<this.childcount;i++)
      this.children[i].grey();
  }
}

function preloadimgs()
{
  imgsrcs = new Array (
    "stempty.gif",
    "stline.gif",
    "stnode.gif",
    "stendnode.gif",
    "stcolnode.gif",
    "stcolendnode.gif",
    "stexpnode.gif",
    "stexpendnode.gif",
    "stunsel.gif",
    "stsel.gif",
    "stunsel.gif")
  plimgs = new Array ();
  for (counter in imgsrcs) 
  {
    plimgs[counter] = new Image();
    plimgs[counter].src = imgpath + imgsrcs[counter];
  }
}

function createID()
{
  this.id=itemcount;
  items[itemcount]=this;
  itemcount++;
}

function switchfolder(folderID)
{
  folder=items[folderID];
  if(folder.expanded==false)
  {
    folder.expand();
    folder.expanded=true;
  }
  else
  {
    folder.collapse();
    folder.div.style.display="block";
  }
}

function selection()
{
  var i;
  var selection="";
  if(this.selected)
    selection+=this.name+"\n";
  else
    if(this.childcount>0)
      for(i=0;i<this.childcount;i++)
        selection+=this.children[i].selection();
  return selection;
}

function updateselection()
{
  var inputfield=document.getElementById(inputfieldid);
  var selection=selecttree.selection();
  inputfield.value=selection;
}

function switchselect(itemID)
{
  var item;
  item=items[itemID];
  item.select();
  updateselection();
}

var imgpath='fileadmin/user_upload/selecttree/';
var selecttree;
var inputfieldid="dokuformauswahl"

var items=new Array;
var itemcount=0;

function initselecttree()
{
  preloadimgs();
  
  selecttree.draw(0,1,true);
}
