$(function() {
  var $body = $('body');

  $body.on('click', '.reference_list__item', function(event) {
    event.preventDefault();
    var $this = $(this);
    var $reference_list = $this.parents('.reference_list');
    var active = $reference_list.attr('data-active');
    var id = $this.parents('.reference_list__item__frame').attr('data-id');
    var iframe;
    var player;

    if (active) {
      iframe = $reference_list.find('.reference_list__video__item.data_id_' + active).find('iframe').get(0);
      player = new Vimeo.Player(iframe);
      player.pause();
    }

    if (id === 'x') {
      $reference_list.removeAttr('data-active');
    } else {
      $reference_list.attr('data-active', id);

      iframe = $reference_list.find('.reference_list__video__item.data_id_' + id).find('iframe').get(0);
      player = new Vimeo.Player(iframe);
      player.play();
    }
  });
});
