function DPYMenu(DivSourceID, VarName)
{
  this.varName = VarName;
  this.divSourceID = DivSourceID;
  
  this.getHTML = getHTML;
  this.addItem = addItem;
  this.addSubItem = addSubItem;
  this.count= count;
  this.showTree = showTree;
  this.sendMenu = sendMenu;
  this.toggle = toggle;
  this.openLink = openLink;
  this.getLink = getLink;
  this.childCompare = childCompare;
  this.removeEmpty = removeEmpty;
  this.getOpen = getOpen;

  this.menuItems = new Array();
  
  function getOpen()
  {
    var n;
    var incookie;
    var openchilds;
    
    incookie="";
    n=0;
    while (n<this.menuItems.length)
    {
      if(!this.menuItems[n].collapsed)
      {
        if (incookie=="")
        {
          incookie=this.menuItems[n].itemID;
        }
        else
        {
          incookie=incookie + "," + this.menuItems[n].itemID;
        }
        
        openchilds=this.menuItems[n].getOpen();
        
        if (openchilds!="")
        {
          if (incookie=="")
          {
            incookie=openchilds;
          }
          else
          {
            incookie=incookie + "," + openchilds;
          }
        }      
      }
      n++;
    }
    document.cookie="DPYMenu=" + incookie;
  }
  
  function removeEmpty()
  {
    var i;
    for (i=0;i<this.menuItems.length;i++)
    {
      if (this.menuItems[i].menuItems.length==0)
      {
        this.menuItems.splice(i, 1);
        i=0;
      }
    }
  }
  
  function getLink(linkItemID)
  {
    var i, myLink;
    i=0;
    myLink=0;
    while ((i<this.menuItems.length) && (myLink==0))
    {
      myLink = this.menuItems[i].getLink(linkItemID);
      i++;
    }
    return myLink;
  }

  function toggle(toggleItemID)
  {
    var i;
    for (i=0; i<this.menuItems.length;i++)
    {
      this.menuItems[i].toggle(toggleItemID);
    }
    this.sendMenu();
  }

  function openLink(e)
  {
    bron = checkBrowser()=="IE" ? bron=e.srcElement : bron=e.target;
    if (bron.id.indexOf("icon_")!=-1)
    {
      myLink=this.getLink(bron.id.substring(5, bron.id.length));
      if (myLink=="-")
      {
        this.toggle(bron.id.substring(5, bron.id.length));
      }
      else
      {
        location.href=myLink;
      }
    }
    else
    {
      myLink = this.getLink(bron.id);
      if (myLink=="-")
      {
        this.toggle(bron.id);
      }
      else
      {
        location.href=myLink;
      }
    }
  }
    
  function showTree()
  {
    var i;
    for (i=0; i<this.menuItems.length;i++)
    {
      this.menuItems[i].showTree("");
    }
  } 
  
  function addItem(ItemName, ItemID, ItemLink, Collapsed, StyleClass, Position)
  {
    var newItem = new DPYMenuItem(ItemName, ItemID, ItemLink, Collapsed, StyleClass, this.varName, Position);
    this.menuItems.push(newItem);
  }

  function addSubItem(parentNode, ItemName, ItemID, ItemLink, Collapsed, StyleClass, Position)
  {
    var i;
    for (i=0; i<this.menuItems.length;i++)
    {
      this.menuItems[i].addSubItem(parentNode, ItemName, ItemID, ItemLink, Collapsed, StyleClass, this.varName, Position)
    }
  }

  function count()
  {
    return menuItems.length;
  }

  function childCompare(a,b)
  {
    return a.position - b.position;
  }

  function sortChild()
  {
    a = this.itemID + ":\n";
    for (i=0; i<this.menuItems.length;i++)
    {
      a += this.menuItems[i].itemID + ": " + this.menuItems[i].position + "\n";
    }
    alert(a);
  }

  function getHTML()
  {
    var a, i;
    a = "";
    this.menuItems.sort(this.childCompare);
    if (this.menuItems.length>0)
    {
      a = "<TABLE width='162'>\n";
      for (i=0; i< this.menuItems.length; i++)
      {
        a = a + this.menuItems[i].getHTML();
      }
      a = a + "</TABLE>\n";
    }
    return a;
  }

  function sendMenu()
  {
    this.getOpen();
    document.getElementById(this.divSourceID).innerHTML = this.getHTML();
  }
};

function DPYMenuItem(ItemName, ItemID, ItemLink, Collapsed, StyleClass, VarName, Position)
{
  this.menuItems = new Array();

  this.varName = VarName;
  this.itemName = ItemName;
  this.itemLink = ItemLink;
  this.styleClass = StyleClass;
  this.collapsed = Collapsed;
  this.itemID = ItemID;
  this.position = Position;

  this.getHTML = getHTML;
  this.getLink = getLink;
  this.addSubItem = addSubItem;
  this.showTree = showTree;
  this.toggle = toggle;
  this.getOpen = getOpen;
  this.childCompare = childCompare;

  this.menuItems = new Array();


  function getOpen()
  {
    var n;
    var incookie;
    var openchilds;
    
    incookie="";
    n=0;
    while (n<this.menuItems.length)
    {
      if(!this.menuItems[n].collapsed)
      {
        if (incookie=="")
        {
          incookie=this.menuItems[n].itemID;
        }
        else
        {
          incookie=incookie + "," + this.menuItems[n].itemID;
        }
        
        openchilds=this.menuItems[n].getOpen();
        
        if (openchilds!="")
        {
          if (incookie=="")
          {
            incookie=openchilds;
          }
          else
          {
            incookie=incookie + "," + openchilds;
          }
        }      
      }
      n++;
    }
    return incookie;
  }

  function toggle(toggleItemID)
  {
    if (toggleItemID==this.itemID)
    {
      this.collapsed=!this.collapsed;
    }
    else
    {
      if (this.menuItems.length>0)
      {
        var i;
        for (i=0; i<this.menuItems.length;i++)
        {
          this.menuItems[i].toggle(toggleItemID);
        }
      }
    }
  }

  function childCompare(a,b)
  {
    return a.position - b.position;
  }

  function getHTML()
  {
    var i, a;
    a="";
    this.menuItems.sort(this.childCompare);
    if (this.menuItems.length>0 || this.itemLink != "-")
    {
      a = "<TR><TD"
      a = a + " id='" + this.itemID + "'";
      a = a + " class='" + this.styleClass + "'";
      a = a + " OnMouseOver='setMenuStyle(event, \"" + this.styleClass + "MouseOver\")'";
      a = a + " OnMouseOut='setMenuStyle(event, \"" + this.styleClass + "\")'";
      a = a + " OnClick='" + this.varName + ".openLink(event)'";
      a = a + ">";
      if (this.itemLink == "-")
      {
         if (this.collapsed)
         {
            a = a + "<IMG id='icon_" + this.itemID + "' SRC='img/plus.png' width=9 height=9>&nbsp;" + this.itemName;
         }
         else
         {
           a = a + "<IMG id='icon_" + this.itemID + "' SRC='img/min.png' width=9 height=9>&nbsp;" + this.itemName;
         }
      }
      else
      {
        a = a + this.itemName;
      }
      a = a + "</TD></TR>\n";
  
  
      if (this.menuItems.length>0)
      {
        for (i=0; i<this.menuItems.length;i++)
        {
        	if (!this.collapsed)
        	{
            a = a + this.menuItems[i].getHTML();
          }
        }
      }
    }
    return a;
  }
  
  function showTree(voorloop)
  {
    var i;
    if (this.menuItems.length>0)
    {
      for (i=0; i<this.menuItems.length;i++)
      {
        this.menuItems[i].showTree(voorloop + this.itemName + "|");
      }
    }
    else
    {
      alert(voorloop + this.itemName);
    }
  }

  function getLink(linkItemID)
  {
    var i, myLink;
    if (linkItemID == this.itemID)
    {
      return this.itemLink;
    }
    else
    {
      i=0;
      myLink=0;
      while ((i<this.menuItems.length) && (myLink==0))
      {
        myLink = this.menuItems[i].getLink(linkItemID);
        i++;
      }
      return myLink;
    }
  }

  function addSubItem(parentNode, ItemName, ItemID, ItemLink, Collapsed, StyleClass, VarName, Position)
  {
    var i;
    if (parentNode == this.itemID)
    {
      var newItem = new DPYMenuItem(ItemName, ItemID, ItemLink, Collapsed, StyleClass, VarName, Position);
      this.menuItems.push(newItem);
      return 1;
    }
    else
    {
      for (i=0; i<this.menuItems.length;i++)
      {
        this.menuItems[i].addSubItem(parentNode, ItemName, ItemID, ItemLink, Collapsed, StyleClass, VarName, Position)
      }
      return 0;
    }
  }
}

function setMenuStyle(e, stl)
{
  bron=checkBrowser()=="IE" ? bron=e.srcElement : bron=e.target;
  if (bron.id.indexOf("icon_")!=-1)
  {
    parentBron="header_" + document.getElementById(bron.id.substring(5, bron.id.length));
    parentBron.className=stl;
  }
  else
  {
    bron.className=stl;
  }
};

