/* ------------------------------------------------------------------------------
   Greg McIlhiney 3/2004
   Sidebar code based on, but drastically modified/enhanced,  
      http://theimposter.org/examples/exploder/
   Cookie code from, A list Apart style changer with Javascript.
      http://www.alistapart.com/articles/alternate/
------------------------------------------------------------------------------ */


function toggleMenu(theID) {
   if (document.getElementById(theID).style.display != 'block') {
      document.getElementById(theID).style.display = 'block';
   } else {
      document.getElementById(theID).style.display = 'none';
   }
}

function getListStatus() {
   var nodes = document.getElementsByTagName('ul') // store all ul list elements
   var listStatus = '';  // status of each module, what we will write to cookie
   var theID = '';       // current ID of list element within loop

   for(var i = 0; i < nodes.length; i++) {
      // the ID of current list
      theID = nodes[i].getAttribute('id');
    
      // find out if it is one of our modules menus
      if (theID) {
         if (theID.indexOf('module') != -1) {

            if (document.getElementById(theID).style.display == 'block') {
               listStatus += theID + '#1,';
            } else {
               listStatus += theID + '#0,';
            }
         }
      }
   }
   // delete last comma char
   listStatus = listStatus.substring(0, listStatus.length -1);
   
   return listStatus;
}


function writeCookie(name,value,days) {
   if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = "; expires=" + date.toGMTString();
   } else {
      var expires = "";
   }
   document.cookie = name + "=" + escape(value) + expires + "; path=/";
}


function readCookie(name) {
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}


function setListStatus(status) {
   var listNodes = status.split(',');

   for (var i = 0; i < listNodes.length; i++) {
      var pairNodes = listNodes[i].split('#');
      var listID = pairNodes[0];
      var listValue = pairNodes[1];
      if (listValue == '1') {
         document.getElementById(listID).style.display = 'block';
      }
   }
}


function testIt () {
   var status = unescape(readCookie('moduleStatus'));
   setListStatus(status);
}



window.onload = function() {
   var status = unescape(readCookie('moduleStatus'));
   setListStatus(status);
}


window.onunload = function() {
   writeCookie('moduleStatus', getListStatus(), 365);
}