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);
      }
    });
  }

  function fixedCta() {

    function scrollCheck() {
      var scrollDistance = $(window).scrollTop();
      if (scrollDistance > initHeight) {
        fixedElement.css("bottom", "0");
      } else {
        fixedElement.css("bottom", "-100%");
      }
    }

    function initFixedBar() {
      if ($(window).width() >= 768) {
        $('.js-appendToBody').detach().appendTo('body');
        fixedElement.css({
          "position": "fixed",
          "bottom": "-100%"
        });

        // Possible solution to footer problem - add margin-bottom to footer to compensate
        // for fixed element height
        $('footer').css("margin-bottom", fixedElement.height() + "px");

        scrollCheck();

        $(window).on("scroll", scrollCheck);
      } else {
        fixedElement.css({
          "position": "relative",
          "bottom": "0"
        });
      }

    }

    var fixedElement = $('.js-fixed'),
      initHeight = 250;

    initFixedBar();

    $('.js-close-fixed').on("click", function(e) {
      e.preventDefault();
      fixedElement.remove();
    });
  }

  bsmCampaignCarousel();
  infoRevealPanel();
  fixedCta();
});
