(function ($) {
  'use strict'

  /* Isotope --------------------- */
  function isotopeSetup () {
    // Initialize Isotope after all images have loaded
    var $container = $('.organic-widgets-portfolio-holder')
    $container.isotope({ // Isotope options, 'item' matches the class in the PHP
      itemSelector: '.organic-widgets-portfolio-item',
      percentPosition: true,
      masonry: {
        gutter: '.organic-widgets-grid-spacer'
      }
    })

    $('.filter-button-group').on('click', 'button', function () {
      var filterValue = $(this).attr('data-filter')
      $container.isotope({ filter: filterValue })
    })
  }

  // On Document Ready, Bind Functions
  $(document).on('ready', function () {
    // Check for customizer and listen for changes that require rebindings
    if (typeof wp.customize !== 'undefined') {
      wp.customize.selectiveRefresh.bind('partial-content-rendered', function () {
        isotopeSetup()
      })
    }
  })

  $(window).on('load', isotopeSetup)

})(jQuery)
