$(function() {
  var $tabs = $('#search-results-tabs'),
      $searchForm = $('.js-search-hash');

  if($tabs.length > 0){
    $tabs.tabs({ 'defaultTab' : getSelectedSearchTabIndex(), scrollOnload: true });
  }

  function getSelectedSearchTabIndex(){
    var tabIds = $('.search-navigation a').map(function(i, el){
      return $(el).attr('href').split('#').pop();
    });
    $selectedTab = $('input[name=tab]');
    selectedTab = $.inArray($selectedTab.val(), tabIds);

    return selectedTab > -1 ? selectedTab : 0;
  }

  if($searchForm.length){
    $tabs.on('tabChanged', updateSearchForms);
  }

  // Update the forms so that the selected tab is remembered for the next search.
  // This function applies to any form with the "tab" hidden input in it:
  //   * form at the top
  //   * government filtering form
  function updateSearchForms(e, hash){
    if(typeof hash !== 'undefined'){
      var $selectedTab = $('input[name=tab]');

      if($selectedTab.length === 0){
        $selectedTab = $('<input type="hidden" name="tab">');
        $searchForm.prepend($selectedTab);
      }

      $selectedTab.val(hash);
    }
  }
});

@@ -205,16 +205,339 @@
 );%0A  %7D%0A%0A
+  // Returns the index of the tab, or defaults to 0 (ie the first tab)%0A  //%0A  // To do this, it looks at which tab has the %22active%22 class.%0A  // This might be the one that the user has selected, or one that the server%0A  // has defaulted to (eg the first tab might have no results, so we'd set the%0A  // second one as active)%0A
   functi
@@ -694,95 +694,172 @@
 %7D);%0A
+%0A
     
-$selected
+var active
 Tab
+Id
  = $(
-'input%5Bname=tab%5D');%0A    selectedTab = $.inArray($selectedTab.val()
+%22.search-navigation li.active a%22).first()%0A                        .attr(%22href%22).replace(%22#%22, %22%22);%0A%0A    var tabIndex = $.inArray(activeTabId
 , ta
@@ -865,17 +865,16 @@
 abIds);%0A
-%0A
     retu
@@ -880,38 +880,32 @@
 urn 
-selectedTab %3E -1 ? selectedTab
+tabIndex %3E -1 ? tabIndex
  : 0

Correctly set the defaultTab in some situations