window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
  function CoronavirusLandingPage () {}

  CoronavirusLandingPage.prototype.start = function ($element) {
    // Confirm the user is on the coronavirus landing page
    if (this.checkOnLandingPage()) {
      this.globarBarSeen()
    }
    this.addAccordionOpenAllTracking($element)
  }

  CoronavirusLandingPage.prototype.checkOnLandingPage = function () {
    return window.location.pathname === '/coronavirus'
  }

  CoronavirusLandingPage.prototype.globarBarSeen = function () {
    if (window.GOVUK.cookie('global_bar_seen')) {
      // Get current version of global bar, if cookie has been set
      var currentBannerVersion = JSON.parse(window.GOVUK.cookie('global_bar_seen')).version

      // Automatically hide the additional information section in the banner by setting the cookie
      window.GOVUK.setCookie('global_bar_seen', JSON.stringify({ count: 999, version: currentBannerVersion }), { days: 84 })
    }
  }

  CoronavirusLandingPage.prototype.addAccordionOpenAllTracking = function ($element) {
    $element.find('.govuk-accordion__open-all').on('click', function(e) {
      var expanded = $(e.target).attr('aria-expanded') == 'true'
      var label = expanded ?  'Expand all' : 'Collapse all'
      var action = expanded ?  'accordionOpened' : 'accordionClosed'
      var options = { transport: 'beacon', label: label }

      GOVUK.analytics.trackEvent('pageElementInteraction', action, options)
    })
  }

  Modules.CoronavirusLandingPage = CoronavirusLandingPage
})(window.GOVUK.Modules)

@@ -203,24 +203,45 @@
 $element) %7B%0A
+    var $this = this%0A
     // Confi
@@ -357,20 +357,162 @@
 )%0A    %7D%0A
+%0A
     
+// Ensure that the %22Open/Close all%22 element exists when attaching the event listeners for tracking%0A    $(document).ready(function() %7B%0A      $
 this.add
@@ -546,16 +546,23 @@
 lement)%0A
+    %7D)%0A
   %7D%0A%0A  C

Enable open/close all accordion tracking