// toggling event visibility
// v5 Kent Brewster, 7/11/2006
// questions? comments? dirty jokes?
// please leave 'em here:
// http://KENTBREW2ster.com/toggle
// feel free to use or abuse this code
// but please leave this notice intact

// namespace protection: one global variable to rule them all

var KENTBREW2 = window.KENTBREW2 || {};

KENTBREW2.toggle = function() {

   // private bucket for scope-sensitive variables -- thanks, Dustin
   var $ = {};
   $.tog = [];
   return {   
      init : function(selfObj2, toggleClass2, toggleClosed2, toggleHidden2) {
         // first, a brute-force hack to decode the calling function's 
         // name and store it upstairs, safe from scope creep
         $.selfName2 = this.getselfName2(selfObj2);
         
         // we're going to hang all variables that might be confused 
         // by scope changes later onto KENTBREW2.variables, which is aliased to $.  
         // In this case it's three class names, fed in from the init call:

         $.toggleClass2 = toggleClass2;
         $.toggleClosed2 = toggleClosed2;
         $.toggleHidden2 = toggleHidden2;
         
         // crawl through the document, look for toggled elements
         this.crawl(document.body);
      },
            openAll2 : function() {
         for (var i = 0; i < $.tog.length; i++) {
            if($.tog[i].className.match($.toggleClosed2)) {
               $.tog[i].className = $.tog[i].className.replace($.toggleClosed2, '');
               var nextSib = this.getNextSibling($.tog[i]);
               nextSib.className = nextSib.className.replace($.toggleHidden2, '');
            }
         }
      },
      closeAll2 : function() {
         for (var i = 0; i < $.tog.length; i++) {
            if(!$.tog[i].className.match($.toggleClosed2)) {
               $.tog[i].className += ' ' + $.toggleClosed2;
               var nextSib = this.getNextSibling($.tog[i]);
               nextSib.className += ' ' + $.toggleHidden2;
            }
         }
      },
      crawl : function(el) {
      
         if (el.className == 'openAll2') {
            el.onmouseup = function () { 
               eval($.selfName2 + '.openAll2()'); 
            };
         }

         if (el.className == 'closeAll2') {
            el.onmouseup = function () { 
               eval($.selfName2 + '.closeAll2()'); 
            };
         }
      
         // get this element's next sibling
         var nextSib = this.getNextSibling(el);

         // if it has a class name, the class name matches our toggle class, and there's something there to toggle:
         if (el.className && el.className.match($.toggleClass2) && nextSib)
         {
            $.tog[$.tog.length] = el;
            // to avoid scope loss, attach onmouseup to the toggle function with eval and $.selfName2
            el.onmouseup = function () { 
               eval($.selfName2 + '.toggleState(this)'); 
            };
            
            // if the next sib ought to be hidden and it isn't already, hide it
            if (el.className.match($.toggleClosed2) && nextSib && !nextSib.className.match($.toggleHidden2)) {
               nextSib.className += ' ' + $.toggleHidden2;
            }
         }

         // is there more to do? Do it, if so:
         if (el.firstChild) {
            this.crawl(el.firstChild);
         }
         
         if (nextSib) {
            this.crawl(nextSib);
         }
      },
      toggleState : function(el) {
         // change the style of the triggering element
         if(el.className.match($.toggleClosed2)) {
            el.className = el.className.replace($.toggleClosed2, '');
         }
         else {
            el.className = el.className + ' ' + $.toggleClosed2;
         }

         // the norgie we clicked has changed.  Now we need to
         // change the style of its parent node's next sibling
         var nextSib = this.getNextSibling(el);

         // check if it's really there; other scripts could have removed it
         if(nextSib && nextSib.className.match($.toggleHidden2)) {
            nextSib.className = nextSib.className.replace($.toggleHidden2, '');
         }
         else {
            nextSib.className += ' ' + $.toggleHidden2;
         }
      },
      getNextSibling : function(el) {
         var nextSib = el.nextSibling;
         // hack for Gecko browsers
         if (nextSib && nextSib.nodeType != 1) {
            nextSib = nextSib.nextSibling;
         }
         return nextSib;
      },
      getselfName2 : function(selfObj2) {
         // icky hack to get contents of selfObj2 into a string
         // suggestions will be gratefully appreciated
         var s = document.createElement('SPAN');
         s.innerHTML = selfObj2;
         // cut the fat, split the meat to namespace array
         var nameSpace = s.innerHTML.split('{')[1].split('(')[0].replace(/^\s+/, '').split('.');
         var selfName2 = '';
         // here we assume that the main function is up one level from the init function
         for (var i = 0; i < nameSpace.length - 1; i++) {
            if (selfName2) {
               selfName2 += '.';
            }
            selfName2 += nameSpace[i];
         }
         return selfName2;
      }
   };
}();

// feed it the CSS class names of your choice
window.onload = function() { 
   KENTBREW2.toggle.init(arguments.callee, 'togglechat', 'togglechat.closed', 'hidden');
}();