"use strict";

$(document).ready(function () {
  $(document).on('click', '.js-newspagination-loadmore', function (e) {
    var $loadButton = $(this);
    $loadButton.addClass('is-loading'); // var ajaxUrl = $(this).data('link');
    // data link not working with categories and doNotDisplayAlreadyDisplaedNews

    var ajaxUrl = $(this).attr('href');

    if (ajaxUrl !== undefined && ajaxUrl !== '') {
      e.preventDefault();
      var container = 'js-newspagination-' + $(this).data('container');
      $.ajax({
        async: true,
        url: ajaxUrl,
        type: 'GET',
        success: function success(result) {
          $loadButton.remove(); // addNews

          var ajaxDom = $(result).find('#' + container).children();
          $('#' + container).append(ajaxDom); // add Copyright images

          var $copyrightDom = $(result).find('.ImageCopyright-list').children();

          if ($copyrightDom.length) {
            $('.ImageCopyright-list').append($copyrightDom); // remove copyright images with same id

            $('.ImageCopyright').each(function () {
              var ids = $('[id="' + this.id + '"]');

              if (ids.length > 1 && ids[0] === this) {
                $(ids[1]).remove();
              }
            });
          }
        }
      });
    }
  });
});