(function($) {
  $.fn.tabControl = function(options) {
    var active   = undefined,
        contents = $('.tab_content'),
        buttons  = this.find('ul:first-child li').each(function(index) {
                     $(this).data('content', $(contents[index]));
                   });

    buttons.mousedown(function() {
      if (active && this !== active.get(0))                 // Remove the old class
        active.removeClass('active').data('content').hide();

      active = $(this).addClass('active');                  // Update active element
      active.data('content').show();
      
      var action = (active.attr('id') == 'tab1') ? 'show' : 'hide';
      $('#footnotes')[action]();
    });

    var initial_tab = this.attr('active') || 1;
    if (window.location.href.indexOf('?') > 0) {
      $.each(window.location.href.split('?')[1].split('&'), function(index) {
        var key = this.split('=')[0], value = this.split('=')[1];
        if (key == 'tab' && value <= buttons.length)
          initial_tab = value; return false;
      });
    }
    buttons.eq(initial_tab -1).mousedown();  // Initialize active tab
  };
})(jQuery);