(function ($, Drupal) {

  'use strict';

  Drupal.behaviors.usgs_masonry = {
    attach: function(context, settings) {
      if (settings.views && settings.views.ajaxViews && settings.masonry) {
        var instances = settings.views.ajaxViews;
        $.each(instances, function(view, settings) {
          var viewClass = '.js-view-dom-id-' + settings.view_dom_id;
          var viewName =  settings.view_name.replaceAll('_', '-');
          var masonryContainer = '.masonry-layout-' + viewName;
          $(masonryContainer).imagesLoaded(function () {
            $(viewClass).find(masonryContainer).masonry({
              itemSelector: '.masonry-item',
              columnWidth: '.grid-sizer',
              gutter: '.gutter-sizer',
            });
          });
        });
      }
    }
  };

})(jQuery, Drupal);
