/*
 * $Id: windy2.js 2171 2010-04-12 10:14:52Z thomas $
 * AdaCore corporate web site utility functions
 */

/*
 * Collapsible archives
 */

var expanded_year_head_elem;
/* Currently expanded year head element, if any */

var year_items = new Array ();
/* Array indexed by years, listing corresponding items */

function set_visibility (year_head_elem, visibility) {
  var year = year_head_elem.id.match (/([0-9]+)$/)[1];
  var img = year_head_elem.childNodes[0];

  if (visibility) {
    img.src = theme_dir + "/images/folder_16px_open.png";
    img.alt = "[-]";

  } else {
    img.src = theme_dir + "/images/folder_16px.png";
    img.alt = "[+]";
  }

  for (var j = 0; j < year_items[year].length; j++) {
    year_items[year][j].style.display = visibility ? "list-item" : "none";
  }
}

function clicked_year_head (year_head_elem) {
  if (expanded_year_head_elem == year_head_elem) {
    set_visibility (year_head_elem, 0);
    expanded_year_head_elem = null;

  } else {
    if (expanded_year_head_elem)
      set_visibility (expanded_year_head_elem, 0);
    set_visibility (year_head_elem, 1);
    expanded_year_head_elem = year_head_elem;
  }
}

function decorate_archives_menu () {
  var menu_elem = document.getElementById ("archives-menu");
  var current_year = 0;
  var year_heads = new Array ();

  if (!menu_elem)
    return;

  for (var j = 0; j < menu_elem.childNodes.length; j++) {
    var item = menu_elem.childNodes[j];
    if (item.tagName != "LI")
      continue;

    item_year = item.childNodes[0].title.match (/([0-9]+)$/)[1];

    if (item_year != current_year) {
      current_year = item_year;
      var head = document.createElement ('A');
      head.id = "year_head_" + item_year;
      head.appendChild (document.createElement ('IMG'));
      head.appendChild (document.createTextNode (item_year));
      head.onclick = function() { clicked_year_head (this) };

      /*
       * Record all heads in year_heads array so that we can make them
       * initially invisible after all their corresponding monthly items
       * have been seen.
       */

      year_heads.push (head);

      var head_li = document.createElement ('LI');
      head_li.className = "year_head";
      head_li.appendChild (head);
      menu_elem.insertBefore (head_li, item);

      year_items[item_year] = new Array ();
    }
    year_items[item_year].push (item);
    item.className = "month_link";

    /*
     * If current page is one of the monthly archives, expand enclosing year,
     * and highlight corresponding item.
     */

    var item_a = item.childNodes[0];
    if (item_a.getAttribute ("href") == location.href) {
       expanded_year_head_elem = year_heads[year_heads.length - 1];
       item_a.className = "current_month";
    }
  }
  for (var j = 0; j < year_heads.length; j++) {
    set_visibility (year_heads[j], (year_heads[j] == expanded_year_head_elem));
  }
}

