/*!
 * jQuery imagesLoaded plugin v1.0.4
 * http://github.com/desandro/imagesloaded
 *
 * MIT License. by Paul Irish et al.
 */

(function($, undefined) {

  // $('#my-container').imagesLoaded(myFunction)
  // or
  // $('img').imagesLoaded(myFunction)

  // execute a callback when all images have loaded.
  // needed because .load() doesn't work on cached images

  // callback function gets image collection as argument
  //  `this` is the container

  $.fn.imagesLoaded = function( callback ) {
    var $this = this,
        $images = $this.find('img').add( $this.filter('img') ),
        len = $images.length,
        blank = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';

    function triggerCallback() {
      callback.call( $this, $images );
    }

    function imgLoaded( event ) {
      if ( --len <= 0 && event.target.src !== blank ){
        setTimeout( triggerCallback );
        $images.unbind( 'load error', imgLoaded );
      }
    }

    if ( !len ) {
      triggerCallback();
    }

    $images.bind( 'load error',  imgLoaded ).each( function() {
      // cached images don't fire load sometimes, so we reset src.
      if (this.complete || typeof this.complete === "undefined"){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = blank;
        this.src = src;
      }
    });

    return $this;
  };
})(jQuery);

// DOMContentLoaded
$(function() {
  
  // cache container
  var $container = $('#main_inner');
  // initialize isotope
  // $container.isotope({
  //   // options
  //   itemSelector : '.view',
  //   layoutMode : 'fitRows'
  // });
  
  var $filterLinks = $('#gallery_menu a');

  // filter items when filter links are clicked
  $filterLinks.click(function(e) {
    e.preventDefault();
    
    var $this = $(this),
        selectedFilters = [],
        selector = '*';
    
    $this.toggleClass('active');
    
    // 'show all' is activated
    if ($this.attr('data-filter') == '*' && $this.hasClass('active')) {
      // deactivate other filters
      $filterLinks.each(function() {
        if ($(this).attr('data-filter') != '*') {
          $(this).removeClass('active');
        }
      });
    } else {
      $filterLinks.map(function() {
        // deactivate 'show all' link
        if ($(this).attr('data-filter') == '*') {
          $(this).removeClass('active');
        }
        // collect active filters
        if ($(this).hasClass('active')) {
          selectedFilters.push($(this).attr('data-filter'));
        }
      });
      selector = selectedFilters.join(', ');
    }
    
    // activate 'show all' if no filters selected
    if (selectedFilters.length == 0) {
      $filterLinks.first().addClass('active');
    }
    
    // run the filter
    $container.isotope({ filter: selector });
  });
  
  // PrettyPhoto
  $("a[data-js='prettyPhoto']").prettyPhoto({
    social_tools: '',
    default_width: 700,
    default_height: 700
  });
  
  // $(document).on('load', '.artwork_mask', function() {
  //     var $this = $(this);
  //     $this.imagesLoaded(function() {
  //         var img = $this.find('img').first();
  //         $this.css('height', img.height() - 40); //.css('width', img.width()
  //     });
  // });
  
  // Crop copyright info on large images
  var $mask = $('.artwork_mask');
  $mask.imagesLoaded(function() {
      var img = $mask.find('img').first();
      $mask.css('height', img.height() - 40); //.css('width', img.width()
  });
  
  if ($('#tweets').length) { getTwitterFeed('gruner', 5, false); };
});
