var Modal = (function() {

'use strict';

function Modal(element) {
  this.element = element;
  this.modal = element.find('.modal__container');
  this.modalInner = element.find('.modal__inner');
  this.closeButton = element.find('.modal__close');
  this.modalTrigger = $('[data-modal]');

  this.bindEvents();
}

Modal.prototype.bindEvents = function() {
  var self = this;

  this.modalTrigger.on('click', function() {
    self.toggleModal(true);
  });

  this.element.on('click', function() {
    self.toggleModal(false);
  });

  this.closeButton.on('click', function() {
    self.toggleModal(false);
  });

  $(window).on('keydown', function(event) {
    if (event.keyCode === 27) {
      self.toggleModal(false);
    }
  });

  this.modalInner.on('click', function(event) {
    event.stopPropagation();
  });
};

Modal.prototype.toggleModal = function(open) {
  this.element.toggleClass('modal--open', open);
};

return Modal;

})();

$(function() {

if ($('.modal').length) {
  var modal = new Modal($('.modal'));
}

});