function expandYear(year)
{
  $('.archiveYearLink').removeClass('archiveLinkSelected');
  $('.archiveMonthLink').removeClass('archiveLinkSelected');

  var newYearDiv = $('#archiveYearContents'+year);
  if($('#archiveCurrentYear').html() == year) {
    $('#archiveCurrentYear').html('');
    $('.archiveMonthContents').slideUp();
    newYearDiv.slideUp();
    return;
  }
  
  $('#archiveYearLink'+year).addClass('archiveLinkSelected');
  
  //hide all showing years
  $('.archiveMonthContents').slideUp();
  $('.archiveYearContents').slideUp();
  
  //reset the month and save the new year
  $('#archiveCurrentMonth').html('');
  $('#archiveCurrentYear').html(year);
  
  //contents were already received, just reveal them, otherwise make an ajax call
  if(newYearDiv.html() != "") {
    newYearDiv.slideDown();
  } else {
    var type = $('#archiveCurrentType').html();
    newYearDiv.load('archive.ajax.php', {action:'getYear',year:year,archiveType:type}, onAjaxComplete);
  }
}

function expandMonth(month)
{
  $('.archiveMonthLink').removeClass('archiveLinkSelected');
  
  var year = $('#archiveCurrentYear').html();
  var newMonthDiv = $('#archiveMonthContents'+year+month);
  
  if($('#archiveCurrentMonth').html() == month) {
    $('#archiveCurrentMonth').html('');
    newMonthDiv.slideUp();
    return;
  }
  
  $('#archiveMonthLink'+month).addClass('archiveLinkSelected');
  
  //set the current month
  $('#archiveCurrentMonth').html(month);
  
  //hide all showing months
  $('.archiveMonthContents').slideUp();
  
  //contents were already received, just reveal them, otherwise make an ajax call
  if(newMonthDiv.html() != "") {
    newMonthDiv.slideDown();
  } else {
    var type = $('#archiveCurrentType').html();
    newMonthDiv.load('archive.ajax.php', {action:'getMonth',year:year,month:month,archiveType:type}, onAjaxComplete);
  }
}

function onAjaxComplete()
{
  $(this).slideDown(function() { $(this).submit(); } );
}

function initArchive(year, month)
{
  var newYearDiv = $('#archiveYearContents'+year);
  newYearDiv.one("submit", function() { expandMonth(month); } )
  expandYear(year);
}
