(function() {
  var Flipbook, Gallery, GalleryItem, Util, cm;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  if (!Function.prototype.bind) {
    Function.prototype.bind = function(context) {
      return $.proxy(this, context);
    };
  }
  if (!String.prototype.startsWith) {
    String.prototype.startsWith = function(text) {
      return this.indexOf(text) === 0;
    };
  }
  if (!String.prototype.isEmpty) {
    String.prototype.isEmpty = function() {
      return !(this && this.length > 0);
    };
  }
  Util = {
    getDimensions: function(width, height, maxWidth, maxHeight) {
      var multiplier, newHeight, newWidth, returnHeight, returnWidth;
      returnWidth = 0;
      returnHeight = 0;
      if (height <= maxHeight && width <= maxWidth) {
        returnWidth = width;
        returnHeight = height;
      } else {
        multiplier = maxWidth / width;
        if (height * multiplier <= maxHeight) {
          newHeight = Math.round(height * multiplier);
          returnWidth = maxWidth;
          returnHeight = newHeight;
        } else {
          multiplier = maxHeight / height;
          newWidth = Math.round(width * multiplier);
          returnWidth = newWidth;
          returnHeight = maxHeight;
        }
      }
      return {
        width: returnWidth,
        height: returnHeight
      };
    }
  };
  GalleryItem = (function() {
    function GalleryItem(index, model, gallery) {
      this.index = index;
      this.model = model;
      this.gallery = gallery;
      this.media = this.model.media;
      this.loaded = false;
      this.loading = false;
      this.description = this.model.description;
      this.first = this.index === 0;
      this.last = (this.index + 1) === this.gallery.items.length;
      if (this.model.media.url) {
        this.url = this.model.media.url;
      } else {
        this.size = Util.getDimensions(this.media.width, this.media.height, this.gallery.width, this.gallery.height);
        this.url = "http://m.cmcdn.net/" + this.media.id + "/" + this.size.width + "x" + this.size.height + ".jpeg";
      }
      this.isVideo = this.media.type.startsWith('video');
    }
    GalleryItem.prototype.load = function() {
      var d;
      d = $.Deferred();
      if (this.isVideo) {
        d.resolve();
      } else {
        this.image = new Image();
        if (this.loaded) {
          d.resolve();
        } else {
          this.loading = true;
          this.image.onload = __bind(function() {
            this.loaded = true;
            this.loading = false;
            return d.resolve();
          }, this);
          this.image.src = this.url;
        }
      }
      return d.promise();
    };
    return GalleryItem;
  })();
  Gallery = (function() {
    function Gallery(element, items, options) {
      var d, i;
      this.element = element;
      this.items = items;
      this.options = options || {};
      this.index = null;
      this.first = false;
      this.last = false;
      this.slides = (function() {
        var _len, _ref, _results;
        _ref = this.items;
        _results = [];
        for (i = 0, _len = _ref.length; i < _len; i++) {
          d = _ref[i];
          _results.push(new GalleryItem(i, d, this));
        }
        return _results;
      }).call(this);
    }
    Gallery.prototype.init = function(hash) {
      if (hash) {
        return this.hashcode(hash);
      } else {
        return this.showSlide(0);
      }
    };
    Gallery.prototype.validateIndex = function(index) {
      index = parseInt(index) || 0;
      if (index < 0) {
        return this.slides.length - 1;
      }
      if (index >= this.slides.length) {
        return 0;
      }
      return index;
    };
    Gallery.prototype.next = function() {
      return this.showSlide(this.index + 1);
    };
    Gallery.prototype.previous = function() {
      return this.showSlide(this.index - 1);
    };
    Gallery.prototype.showSlide = function(index) {
      if (this.index === index) {
        return;
      }
      this.index = this.validateIndex(index);
      this.slide = this.slides[this.index];
      this.first = this.slide.first;
      this.last = this.slide.last;
      return $(this).trigger('slides:show', this.slide);
    };
    Gallery.prototype.hashcode = function(hash) {
      if (!hash) {
        return "#" + (this.index + 1);
      }
      if (hash === this.hashcode() && this.index !== null) {
        return;
      }
      return this.showSlide(this.indexFromHashcode(hash));
    };
    Gallery.prototype.indexFromHashcode = function(hash) {
      return this.validateIndex(hash.match(/#(\d+)/)[1] - 1);
    };
    return Gallery;
  })();
  Flipbook = (function() {
    __extends(Flipbook, Gallery);
    function Flipbook(element, items, options) {
      this.element = element;
      this.items = items;
      $(this).bind('slides:show', this.displaySlide.bind(this));
      this.canvas = this.element.find('.canvas');
      this.artwork = this.element.find('.artwork, .mediaPlayer');
      this.artwork.css('opacity', 1);
      this.caption = this.element.find('.caption');
      this.width = this.artwork.width();
      this.height = this.artwork.height();
      Flipbook.__super__.constructor.call(this, this.element, this.items, options);
    }
    Flipbook.prototype.displaySlide = function(e, item) {
      if (item.loaded) {
        this.canvas.removeClass('loading');
      } else {
        this.canvas.addClass('loading');
      }
      if (item.description && !item.description.isEmpty()) {
        this.caption.html(item.description).show();
      } else {
        this.caption.hide();
      }
      this.artwork.css('opacity', 0);
      if (item.first) {
        this.element.addClass('start');
      } else {
        this.element.removeClass('start');
      }
      if (item.last) {
        this.element.addClass('end');
      } else {
        this.element.removeClass('end');
      }
      return $.when(item.load()).then(__bind(function() {
        var player, poster, posterSize, r, renditions;
        if (this.index !== item.index) {
          return;
        }
        if (item.isVideo) {
          this.artwork.css('opacity', 1);
          player = $('.mediaPlayer').data('controller');
          renditions = (function() {
            var _i, _len, _ref, _results;
            _ref = item.model.media.renditions;
            _results = [];
            for (_i = 0, _len = _ref.length; _i < _len; _i++) {
              r = _ref[_i];
              _results.push(new MediaSource(r));
            }
            return _results;
          })();
          player.setSources(renditions);
          poster = item.model.thumbnail;
          posterSize = Util.getDimensions(poster.width, poster.height, this.width, this.height);
          poster.url = "http://m.cmcdn.net/" + poster.id + "/" + posterSize.width + "x" + posterSize.height + ".jpeg";
          player.setPoster(poster);
          return player.reload();
        } else {
          this.artwork.css({
            backgroundImage: "url('" + item.url + "')"
          });
          return this.artwork.stop().animate({
            opacity: 1
          }, 500);
        }
      }, this));
    };
    return Flipbook;
  })();
  if (!window.CM) {
    window.CM = {};
  }
  cm = window.CM;
  cm.Flipbook = Flipbook;
}).call(this);
