var openNodes = "";

function toogleDisplay(parent, child)
{
  if ( child.style.display != 'none' )
  {
    child.style.display = 'none';
    parent.style.listStyleImage = "url('img/folder.gif')";
    rmvNode(parent.id);
  }
  else
  {
    child.style.display = '';
    parent.style.listStyleImage = "url('img/folderopen.gif')";
    addNode(parent.id);
  }
}

function makeMenu(parent, child)
{
  parent.tabIndex='0';
  parent.onmouseover = function () { this.style.cursor='pointer'; };
  parent.onclick = function () { toogleDisplay(parent, child); };
  parent.onkeypress = function (e)
  {
    e = e || window.event;
    var key = e.keyCode || e.which;
    if (key == 13) { toogleDisplay(parent, child); }
  };
  child.onclick = function(e)
  { // stop event bubbling, see http://www.quirksmode.org/js/events_order.html
    e = e || window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) { e.stopPropagation(); }
  };
}

function collapseTree(el)
{
  //accept either a node or its ID
  var menu = el.nodeType==1? el : document.getElementById(el);
  var j=0; //iterator that will serve as id to the collapsible nodes
  openNodes=getCookie('tree'); //get open folders from cookie
  for (var i in menu.childNodes)
  {
    var menuItem = menu.childNodes[i];
    if(menuItem.tagName == "LI")
    {
      menuItem.id = 'node' + j++ + '_';
      var subMenu = menuItem.getElementsByTagName('ul')[0];
      if(subMenu)
      {
        if(!isNodeOpen(menuItem.id))
        {
          subMenu.style.display = 'none';
          menuItem.style.listStyleImage = "url('img/folder.gif')";
        }
        else menuItem.style.listStyleImage = "url('img/folderopen.gif')";
        makeMenu(menuItem, subMenu);

        //make this recursive, to support several levels (NOT TESTED)
        //collapseTree(subMenu);
      }
      else { menuItem.style.listStyleImage = "url('img/page.gif')"; }
    }
  }
  window.onunload = function()
  { //clear cookie when leaving the page
    if(window.closed)
    {
      setCookie('tree',"",1); //clear the cookie, since they left the page
    }
    //set a cookie for 30 seconds, in case they're just moving to another page
    setCookie('tree',openNodes,30*1000);
  }
}

/******************************************************************************/
/*                        Tree State Update Functions                         */
/******************************************************************************/

function isNodeOpen(nodeID) //returns true if the node is open
{
  //openNodes=getCookie('tree');
  if (openNodes!=null && openNodes!="")
  {
    return !(openNodes.indexOf(nodeID) == -1);
  }
}

function addNode(nodeID)
{
  if(!isNodeOpen(nodeID)) //do not add twice (just in case)
  {
    openNodes+=nodeID;
  }
}

function rmvNode(nodeID)
{
  if (openNodes!=null && openNodes!="")
  {
    openNodes = openNodes.replace(nodeID,"");
  }
}

/******************************************************************************/
/*                        Generic Cookie Functions                            */
/******************************************************************************/

function getCookie(cookieName)
{
  if (document.cookie.length>0)
  {
    cookieStart=document.cookie.indexOf(cookieName + "=");
    if (cookieStart != -1)
    {
      cookieStart = cookieStart + cookieName.length+1;
      cookieEnd = document.cookie.indexOf(";",cookieStart);
      if (cookieEnd == -1) cookieEnd = document.cookie.length;
      return unescape(document.cookie.substring(cookieStart,cookieEnd));
    }
  }
  return "";
}

function setCookie(cookieName, value, expireDays)
{
  //http://www.mapbender.org/JavaScript_pitfalls:_null,_false,_undefined,_NaN
  if(!isNaN(expireDays) && expireDays!=null && expireDays!="") expireDays=30000;
  var expDate=new Date(); //initialized with the current time
  expDate.setTime(expDate.getTime()+expireDays);
  document.cookie = cookieName + "=" + escape(value)
                    + ";expires="+expDate.toGMTString();
}