

function expand_year() {

  clicked_li = this.parentNode;
  ul = clicked_li.parentNode;
  
  /* collapse expanded */
  for(li = ul.firstChild; li; li = li.nextSibling) {
    if (li.tagName != 'li' && li.tagName != 'LI') continue;
    if (li.className && (li.className.indexOf("collapsed") > -1)) continue;

    a = document.createElement('a');
    a.href = "#";
    a.onclick = expand_year;
    a.appendChild(li.firstChild);
    li.insertBefore(a, li.firstChild);

    if (li.className == "selected") li.className = "collapsed selected";
    else li.className = "collapsed";

    break;
  }

  /* expand clicked */
  if (clicked_li.className == "collapsed selected") clicked_li.className = "selected";
  else clicked_li.className = "";

  /* remove 'expand' link */
  clicked_li.insertBefore(this.firstChild, this);
  clicked_li.removeChild(this);
}


div = document.getElementById("archive");

/* find the archive UL object */
for(top_ul = div.firstChild;top_ul;top_ul = top_ul.nextSibling) {
  if (top_ul.tagName == 'ul' || top_ul.tagName == 'UL') break;
}

selected_archive = document.getElementById("current_archive");
if (selected_archive && selected_archive.firstChild && selected_archive.firstChild.nodeValue) {
   text = selected_archive.firstChild.nodeValue;
   split = text.split(" ", 2);
   selected_month = split[0];
   selected_year = split[1];
}
else {
   selected_month = null;
   selected_year = null;
}

current_year = null;
current_year_li = null;
current_year_ul = null;

/* iterate over LI... */
for(li = top_ul.firstChild; li; li = next) {
   next = li.nextSibling;
   if (li.tagName != 'li' && li.tagName != 'LI') continue;
   for(a = li.firstChild; a; a = a.nextSibling) {
      if (a.tagName == "a" || a.tagName == "A") break;
   }
   if (!a) continue;
   text = a.firstChild.nodeValue;
   text = text.replace("\n", "");
   text = text.replace("\xa0", " ");
   split = text.split(" ", 3);
   month = split[0];
   year = split[1];
   count = split[2];
   a.firstChild.nodeValue = month + "\xa0" + count;
   if (year != current_year) {
      current_year = year;
      current_year_li = document.createElement('li');
      text = document.createTextNode(year);
      if (current_year == selected_year) {
         current_year_li.appendChild(text);
         current_year_li.className = "selected";
      }
      else {
         a = document.createElement('a');
         a.href = "#";
         a.onclick = expand_year;
         a.appendChild(text);
         current_year_li.appendChild(a);
         current_year_li.className = "collapsed";
      }
      current_year_ul = document.createElement('ul');
      current_year_li.appendChild(current_year_ul);
      top_ul.insertBefore(current_year_li, li);
   }
   current_year_ul.appendChild(li);
   if (year == selected_year && month == selected_month) {
      li.className = "selected";
   }
}



