/**
* DO NOT EDIT THIS FILE.
* See the following change record for more information,
* https://www.drupal.org/node/2815083
* @preserve
**/

(function ($, Drupal) {
  'use strict';

  Drupal.AudiofieldJplayer = {};

  Drupal.AudiofieldJplayer.generate = function (context, settings) {
    $('#jquery_jplayer_' + settings.unique_id, context).once('generate-jplayer').jPlayer({
      cssSelectorAncestor: '#jp_container_' + settings.unique_id
    }, {
      ready: function ready() {
        var mediaArray = {
          title: settings.description
        };
        mediaArray[settings.filetype] = settings.file;
        $('#jquery_jplayer_' + settings.unique_id, context).jPlayer('setMedia', mediaArray);
      },
      canplay: function canplay() {
        if (!!settings.autoplay) {
          $('#jquery_jplayer_' + settings.unique_id, context).jPlayer('play');
        }
      },
      swfPath: '/libraries/jplayer/dist/jplayer',
      supplied: settings.filetype,
      wmode: 'window',
      useStateClassSkin: true,
      autoBlur: false,
      preload: settings.lazyload,
      smoothPlayBar: true,
      keyEnabled: true,
      remainingDuration: false,
      toggleDuration: false,
      volume: settings.volume
    });
  };

  Drupal.AudiofieldJplayer.generatePlaylist = function (context, settings) {
    $.each($(context).find('#jquery_jplayer_' + settings.unique_id).once('generate-jplayer'), function (index, item) {
      var thisPlaylist = new jPlayerPlaylist({
        jPlayer: $(item),
        cssSelectorAncestor: '#jp_container_' + settings.unique_id
      }, [], {
        canplay: function canplay() {
          if (!!settings.autoplay) {
            $(item).jPlayer('play');
          }
        },
        playlistOptions: {
          enableRemoveControls: false
        },
        swfPath: '/libraries/jplayer/dist/jplayer',
        wmode: 'window',
        useStateClassSkin: true,
        autoBlur: false,
        preload: settings.lazyload,
        smoothPlayBar: true,
        keyEnabled: true,
        volume: settings.volume
      });

      $.each(settings.files, function (key, fileEntry) {
        var mediaArray = {
          title: fileEntry.description
        };
        mediaArray[fileEntry.filetype] = fileEntry.file;

        thisPlaylist.add(mediaArray);
      });
    });
  };

  Drupal.AudiofieldJplayer.generateCircle = function (context, file) {
    $.each($(context).find('#jquery_jplayer_' + file.fid).once('generate-jplayer'), function (index, item) {
      var mediaArray = {};
      mediaArray[file.filetype] = file.file;

      new CirclePlayer($(item), mediaArray, {
        cssSelectorAncestor: '#cp_container_' + file.fid,
        canplay: function canplay() {
          if (!!file.autoplay) {
            $(item).jPlayer('play');
          }
        },
        swfPath: '/libraries/jplayer/dist/jplayer',
        wmode: 'window',
        keyEnabled: true,
        preload: settings.lazyload,
        supplied: file.filetype
      });
    });
  };

  Drupal.behaviors.audiofieldjplayer = {
    attach: function attach(context, settings) {
      $.each(settings.audiofieldjplayer, function (key, settingEntry) {
        if (settingEntry.playertype === 'default') {
          Drupal.AudiofieldJplayer.generate(context, settingEntry);
        } else if (settingEntry.playertype === 'playlist') {
            Drupal.AudiofieldJplayer.generatePlaylist(context, settingEntry);
          } else if (settingEntry.playertype === 'circle') {
              $.each(settingEntry.files, function (key2, fileEntry) {
                Drupal.AudiofieldJplayer.generateCircle(context, fileEntry);
              });
            }
      });
    }
  };
})(jQuery, Drupal);
