// Generated by CoffeeScript 1.10.0
$(function() {
  var play;
  play = function(parent, item, callback) {
    var width;
    width = parent.width();
    item.imagesLoaded(function() {
      var _this, nHeight, nWidth;
      _this = this[0];
      nWidth = _this.naturalWidth;
      nHeight = _this.naturalHeight;
      callback();
      this.animate({
        opacity: 1
      }, 500);
      parent.animate({
        height: width * nHeight / nWidth
      }, 500);
    });
  };
  $('.gallery').each(function() {
    var all, cur, loading, photoset, point;
    cur = $(this);
    point = 0;
    photoset = cur.children('.photoset').children();
    all = photoset.length;
    loading = true;
    play(cur, photoset.eq(0), function() {
      loading = false;
    });
    if (all > 1) {
      cur.on('click', '.prev', function() {
        var next;
        if (!loading) {
          next = (point - 1) % all;
          loading = true;
          play(cur, photoset.eq(next), function() {
            photoset.eq(point).animate({
              opacity: 0
            }, 500);
            loading = false;
            point = next;
          });
        }
      }).on('click', '.next', function() {
        var next;
        if (!loading) {
          next = (point + 1) % all;
          loading = true;
          play(cur, photoset.eq(next), function() {
            photoset.eq(cur).animate({
              opacity: 0
            }, 500);
            loading = false;
            point = next;
          });
        }
      });
    } else {
      $('.control').hide();
    }
  });
});
