$(document).ready(function () {
  if (window.location.hash != '') {
    var head = $(window.location.hash).parent();
    if (head.attr('id')) {
      var parent_slide = head.parents('.tbodyslide')[0];
      if ($(parent_slide).attr('id')) {
        var parent_number = $(parent_slide).attr('id').match(/tbodyslide([0-9-]+)/)[1];
        var parent_head = $('#tbodyhead' + number);
      
        parent_head.addClass('open');
        $(parent_slide).addClass('open');
      }
      
      var number = head.attr('id').match(/theadslide([0-9-]+)/)[1];
      var slide = $('#tbodyslide' + number);
      
      head.addClass('open');
      slide.addClass('open');
    }
  }
  else {
    var head = $('.theadslide:first');
    if (head.attr('id')) {
      var number = head.attr('id').match(/theadslide([0-9-]+)/)[1];
      var slide = $('#tbodyslide' + number);

      if (number.match(/[0-9]{4}-[0-9]{2}/)) {
        head.addClass('open');
        slide.addClass('open');
      }
    }
  }
  // find the elements and hook the hover event
  $('.theadslide').click(function() {
    var number = $(this).attr('id').match(/theadslide([0-9-]+)/)[1];
    var slide = $('#tbodyslide' + number);

    if (slide.hasClass('open')) {
      slide.slideUp(1000);
      slide.removeClass('open');
      $(this).removeClass('open');
    }
    else {
      slide.slideDown(1000, function(){ $(this).css('display', 'inline-block') });
      slide.addClass('open');
      $(this).addClass('open');
    }
  });
});
