
/***************************************************************************
*   Copyright (C) 2006, Paul Lutus                                        *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

addEvent(window,"load",readCookie);
addEvent(window,"unload",writeCookie);
addEvent(document,"click",on_click);

// function to add an event listener

function addEvent(o,e,f) {
   if (o.addEventListener) {
      o.addEventListener(e,f,false);
      return true;
   }
   else if (o.attachEvent) {
      return o.attachEvent("on"+e,f);
   }
   else {
      return false;
   }
}

// cookie handling code

function getCookie(path)
{
   var result = "";
   var a = document.cookie.indexOf(path);
   if(a != -1) {
      a += path.length + 1;
      var b = document.cookie.indexOf(";",a);
      if(b == -1) {
         b = document.cookie.length;
      }
      result = unescape(document.cookie.substring(a,b));
   }
   return result;
}

function readCookie()
{
   if (!document.createElement) { return; }
      var arr = new Array();
   var cookie = getCookie(window.location.pathname);
   if (cookie != '') {
      for (var i=0 ; i < cookie.length ; i++) {
         arr.push((cookie.substring(i,i+1) == '1'));
      }
      var alldivs = document.getElementsByTagName('div');
      if (alldivs == null) { return; }
         var alen = alldivs.length;
      var arrlen = arr.length;
      var j = 0;
      for(var i = 0;i < alen;i++) {
         var cn = alldivs[i].className;
         if((/\bexpf[o|c]\b/i).test(cn)) {
            set_state(alldivs[i],arr[j]);
            j++;
         }
      }
   }
}

function writeCookie() {
   var nodestr = '';
   var alldivs = document.getElementsByTagName('div');
   var alen = alldivs.length;
   for(var i = 0;i < alen;i++) {
      var cn = alldivs[i].className;
      if((/\bexpf[o|c]\b/i).test(cn)) {
         nodestr += (/\bexpfo\b/i.test(cn))?'1':'0';
      }
   }
   var duration = new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000);
   document.cookie=window.location.pathname+"="+nodestr+"; expires=" + duration.toGMTString();
}

function expandCollapseTree(state)
{
   var nodes = document.getElementsByTagName('div');
   var nlen = nodes.length;
   for(var i = 0;i < nlen;i++) {
      var cn = nodes[i].className;
      if((/\bexpf[o|c]\b/i).test(cn)) {
         set_state(nodes[i],state);
      }
   }
   return false;
}

// event handler

function set_state(target,state) {
   var item = target.childNodes;
   var node = item[1];
   ncn = (state)?'expco':'expcc';
   tcn = (state)?'expfo':'expfc';
   if(node.className != ncn) {
      node.className = ncn;
   }
   if(target.className != tcn) {
      target.className = tcn;
   }
}

function on_click(e) {
   var evt=e?e:event;
   var target=evt.target?evt.target:evt.srcElement;
   var cn = target.className;
   if(/\bexpf[o|c]\b/i.test(cn)) {
      var state = (/\bexpfo\b/i.test(cn));
      set_state(target,!state);
   }
}
