(function() {
  var cs;

  cs = window.CS || (window.CS = {});

  cs.Data || (cs.Data = {});

  cs.Views || (cs.Views = {});

  cs.Views.GalleryPostView = Backbone.View.extend({
    events: {
      'click a.next-image': 'nextImage'
    },
    currentImage: 0,
    initialize: function(options) {
      var method;
      _.bindAll(this);
      this.$el = $(this.el);
      this.id = this.$el.data('id');
      this.appModel = options.appModel;
      if (this.$el.data('page') === 1) {
        this.isPage = true;
        method = 'get_page';
      } else {
        this.isPage = false;
        method = 'get_post';
      }
      $.ajax({
        url: "" + cs.Data.index,
        data: {
          id: this.id,
          json: method
        },
        dataType: 'json',
        success: this.hazData
      });
    },
    hazData: function(data) {
      this.model = new Backbone.Model();
      if (this.isPage) {
        this.model.set(data.page);
      } else {
        this.model.set(data.post);
      }
      this.render();
    },
    render: function() {
      var attachmentsThatAreOk;
      var _this = this;
      attachmentsThatAreOk = [];
      _.each(this.model.get('attachments'), function(attachment, index) {
        var h, nope, w;
        w = attachment.images.full.width;
        h = attachment.images.full.height;
        if (w === 303 && h === 416) {
          nope = 'ok';
        } else if (w === 714 && h === 796) {
          nope = 'ok';
        } else if (w === 0 && h === 0) {
          nope = 'ok';
        } else {
          attachmentsThatAreOk.push(attachment);
        }
      });
      this.model.set({
        attachments: attachmentsThatAreOk
      });
      _.each(this.model.get('attachments'), function(attachment, index) {
        _this.$('.thumbs').append("      <div class=\"thumb\" data-href=\"" + attachment.url + "\" data-index=\"" + index + "\">        <img src=\"" + attachment.images.thumbnail.url + "\"/>      </div>");
      });
      bbb.utils.extendViews(cs.Views, this.appModel);
      this.$('div.thumb').bind('click', this.thumbClick);
      this.showImage(0);
    },
    thumbClick: function(e) {
      var $el;
      e.preventDefault();
      $el = $(e.target).parents('.thumb');
      this.showImage(Number($el.data('index')));
    },
    showImage: function(n) {
      var image, newImage;
      image = this.model.get('attachments')[n];
      newImage = $("    <div class=\"img extend\" data-view=\"ImageView\">      <img data-src=\"" + image.url + "\" src=\"" + cs.Data.blankImage + "\"/>    </div>    ");
      $(newImage).bind('transition-complete', this.imageTransitionComplete);
      this.$('.large').append(newImage);
      bbb.utils.extendViews(cs.Views, this.appModel);
      this.$('div.thumb').removeClass('selected');
      this.$("div.thumb[data-index='" + n + "']").addClass('selected');
      this.currentImage = n;
      this.$('.photos .count').html("" + (this.currentImage + 1) + "<span class=\"slash\">/</span>" + (this.model.get('attachments').length));
      this.$('.photos p.caption').html(image.caption || '');
    },
    imageTransitionComplete: function(e) {
      var total;
      var _this = this;
      total = this.$('.large div.img').length;
      _.each(this.$('.large div.img'), function(el, i) {
        if (i < total - 1) $(el).remove();
      });
    },
    imageLoaded: function(e) {
      var $el;
      $el = $(e.target).parent();
    },
    nextImage: function(e) {
      e.preventDefault();
      if (this.currentImage < this.model.get('attachments').length - 1) {
        this.showImage(this.currentImage + 1);
      } else {
        this.showImage(0);
      }
    }
  });

}).call(this);

