'use strict';

// ----------------- ./polyfills.js -----------------
//
(function () {

  // legacy ie console.log fix
  //
  if (!window.console) {
    // eslint-disable-next-line no-global-assign
    console = {
      log: function log() {}
    };
  }

  // legacy ie console.log fix
  //
  if (!window.console) {
    console = { log: function log() {} }; // eslint-disable-line no-global-assign
  }

  // ie custom events fix
  //
  (function () {
    if (typeof window.CustomEvent === 'function') return false; //If not IE

    function CustomEvent(event, params) {
      params = params || { bubbles: false, cancelable: false, detail: undefined };
      var evt = document.createEvent('CustomEvent');
      evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
      return evt;
    }

    CustomEvent.prototype = window.Event.prototype;

    window.CustomEvent = CustomEvent;
  })();
})();

// ----------------- ./ui.js -----------------
//
(function () {

  var $ = window.jQuery,
      fastClick = window.Origami.fastclick;

  var breakpoints = {
    sm: 768,
    md: 992,
    lg: 1200
  };

  //Sticky Table Header on Scroll
  $.fn.fixMe = function ($t_fixed, tableEvent) {
    return this.each(function () {
      var $this = $(this);

      function init() {
        resizeFixed();
      }

      function resizeFixed() {
        $t_fixed.find('th').each(function (index) {
          $(this).css('width', $this.find('th').eq(index).outerWidth() + 'px'); //set width

          if ($this.find('th').eq(index).attr('colspan')) {
            $(this).attr('colspan', $this.find('th').eq(index).attr('colspan')); //set colspan
          }
        });
      }

      function scrollFixed() {
        var offset = $(this).scrollTop(),
            tableOffsetTop = $this.offset().top,
            tableOffsetBottom = tableOffsetTop + $this.height() - $this.find('thead').height();

        if (offset < tableOffsetTop || offset > tableOffsetBottom) $t_fixed.hide();else if (offset >= tableOffsetTop && offset <= tableOffsetBottom && $t_fixed.is(':hidden')) $t_fixed.show();
      }

      $t_fixed.on(tableEvent, resizeFixed);
      $(window).resize(resizeFixed);
      $(window).scroll(scrollFixed);
      init();
    });
  };

  function initFastClick() {
    fastClick(document.body);
  }

  function initContentActions() {
    $('.content-toggle').click(function (e) {
      e.preventDefault();
      $($(this).data('target') || this).toggleClass($(this).data('target-class') || 'active');
    });
    $('.content-activate').click(function (e) {
      e.preventDefault();
      $($(this).data('target') || this).addClass($(this).data('target-class') || 'active');
    });
    $('.content-deactivate').click(function (e) {
      e.preventDefault();
      $($(this).data('target') || this).removeClass($(this).data('target-class') || 'active');
    });
  }

  function initOptionButtons() {
    $(document.body).on('click', '.btn-option', function (e) {
      var $btn = $(e.target),
          $allBtn = $btn.siblings('.btn-option-all');
      $btn.toggleClass('active');
      if ($btn.parent().children('.btn-option.active').length > 0) $allBtn.removeClass('active');else $allBtn.addClass('active');
    });

    $(document.body).on('click', '.btn-option-all', function (e) {
      $(e.target).addClass('active').siblings('.btn-option').removeClass('active');
    });
  }

  function initPopover() {
    var hotspots = $('[data-toggle="popover"]');

    hotspots.popover().on('shown.bs.popover', function (e) {
      var _this = $(this);

      _this.addClass('active');

      $(this).parent().find('div.popover .close-pop-hover').on('click', function (e) {
        _this.popover('hide').removeClass('active');
      });
    }).on('hide.bs.popover', function () {
      if (hotspots.hasClass('active')) {
        $(this).removeClass('active');
      }
    });

    hotspots.on('click', function (e) {
      hotspots.not(this).popover('hide');
    });
  }

  function initGlidePathPlotter() {
    // Give the points coordinates
    $('.glidepath-point').each(function (e) {
      $(this).css({
        'top': $(this).data('top'),
        'left': $(this).data('left')
      });
    });
  }
  // reponsive colspan logic for any element with a data-resp-colspan attribute
  // attribute value should be json that provides colspans for different breakpoints *and narrower*
  function initResponsiveColspans() {
    var $elems = $('[data-resp-colspan]');

    // store default colspan
    $elems.each(function (idx, elem) {
      return $(elem).attr('data-resp-colspan-default', $(elem).attr('colspan'));
    });

    $(window).on('resize', function () {
      var width = $(window).width();

      _.each($elems, function (elem) {
        var $elem = $(elem),
            csConf = $elem.data('resp-colspan'),
            csDefault = $elem.data('resp-colspan-default');

        if (width < breakpoints.sm && csConf.xs) $elem.attr('colspan', csConf.xs);else if (width < breakpoints.md && csConf.sm) $elem.attr('colspan', csConf.sm);else if (width < breakpoints.lg) $elem.attr('colspan', csConf.md);else $elem.attr('colspan', csDefault);
      });
    });
  }

  function initStickyHeader() {
    $('.hasStickyHeader').fixMe();
  }

  $(function () {
    initFastClick();
    initContentActions();
    initOptionButtons();
    initPopover();
    initGlidePathPlotter();
    initResponsiveColspans();
  });
})();
