jQuery(function ($) {
  function bsmCampaignCarousel() {
    $('#bsm-campaign-carousel .carouselPlayPause').on('click', function (e) {
      if ($(this).hasClass("pause")) {
        $('#bsm-campaign-carousel').carousel('pause');
        $(this).removeClass("pause");
        $(this).addClass("play");
        $('#bsm-campaign-carousel').addClass("paused");
      } else {
        $('#bsm-campaign-carousel').carousel('cycle');
        $(this).removeClass("play");
        $(this).addClass("pause");
        $('#bsm-campaign-carousel').removeClass("paused");
      }
    });
  }

  function infoRevealPanel() {

    function changeAttributes(panel) {
      if (panel.isExpanded === true) {
        $("#" + panel.panelid).addClass("is-expanded");
      } else {
        $("#" + panel.panelid).removeClass("is-expanded");
      }
      $("#" + panel.panelid + " .btn-expand").attr("aria-selected", panel.isExpanded ? "true" :
        "false");
      $("#" + panel.panelid + " .info-reveal__content").attr({
        "aria-hidden": panel.isExpanded ? "false" : "true",
        "tabindex": panel.isExpanded ? "0" : "-1"
      });
    }

    function togglePanel(panel, elementid) {
      if (panel.panelid == elementid) {
        // Triggered panel
        panel.isExpanded = panel.isExpanded ? false : true;
        changeAttributes(panel);
      } else {
        // Other panel
        panel.isExpanded = false;
        changeAttributes(panel);
      }
    }

    var panels = [],
      trigger = $('#bsm .info-reveal .btn-expand');

    $('#bsm .info-reveal').each(function () {
      var panel = {
        "panelid": $(this).attr('id'),
        "isExpanded": false
      }
      panels.push(panel);
    });

    trigger.on("click", function (e) {
      e.preventDefault();
      var elementid = $(this).parent().attr('id');
      // panels.forEach(panel => togglePanel(panel, elementid));
      // replaced with for loop to support older ie versions
      for (i = 0; i < panels.length; i++) {
        togglePanel(panels[i], elementid);
      }
    });
  }

  bsmCampaignCarousel();
  infoRevealPanel();
});
