(function() {
  var AppView, ProjectCollection, ProjectModel, ThumbnailView;
  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; };

  AppView = (function() {

    __extends(AppView, Backbone.View);

    function AppView() {
      this.onThumbnailLoaded = __bind(this.onThumbnailLoaded, this);
      this.onThumbnailClicked = __bind(this.onThumbnailClicked, this);
      this.appendThumbnail = __bind(this.appendThumbnail, this);
      this.render = __bind(this.render, this);
      this.onResize = __bind(this.onResize, this);
      this.onLoad = __bind(this.onLoad, this);
      AppView.__super__.constructor.apply(this, arguments);
    }

    AppView.prototype.el = $("body");

    AppView.prototype.events = {
      'click li a': 'onMenuClick'
    };

    AppView.prototype.loaded = 0;

    AppView.prototype.numLoaded = 2;

    AppView.prototype.thumbLoaded = 0;

    AppView.prototype.initialize = function() {
      $(window).load(this.onLoad);
      $(window).resize(this.onResize);
      this.projectCollection = new ProjectCollection();
      return this.projectCollection.fetch({
        success: this.onLoad,
        error: function() {}
      });
    };

    AppView.prototype.onLoad = function() {
      this.loaded++;
      if (this.loaded === this.numLoaded) return this.hide();
    };

    AppView.prototype.onResize = function() {
      var w;
      if ($("#zoom-image")) {
        w = $(window).width() - 230;
        if (w > window.realWidth) w = window.realWidth;
        return $("#zoom-image").width(w);
      }
    };

    AppView.prototype.onMenuClick = function(e_) {
      var filterId;
      filterId = $(e_.currentTarget).attr('data-filter').toLowerCase();
      if (filterId !== "email") {
        e_.preventDefault();
        $('#container').isotope({
          filter: filterId
        });
        return this.closeZoom();
      }
    };

    AppView.prototype.scrollUp = function() {
      return $('html,body').stop().animate({
        scrollTop: 0
      }, 500);
    };

    AppView.prototype.hide = function() {
      var _this = this;
      return this.el.delay(1000).fadeOut("fast", function() {
        _this.render();
        $('#container').isotope({
          itemSelector: '.item',
          masonry: {
            columnWidth: 1
          }
        });
        $('#container').isotope({
          filter: ".none"
        });
        return _this.show();
      });
    };

    AppView.prototype.show = function() {
      var _this = this;
      return this.el.fadeIn("slow", function() {
        $('#container').isotope({
          filter: "*"
        });
        return _this.showComplete();
      });
    };

    AppView.prototype.showComplete = function() {
      return $('#container').delay(1000).isotope('reLayout');
    };

    AppView.prototype.render = function() {
      var go, goS, projectModel, tw, twS, _i, _len, _ref, _results;
      $("#preloader").remove();
      this.el.append($("#template_body").html());
      $("#zoom").hide();
      go = document.createElement('script');
      go.type = 'text/javascript';
      go.async = true;
      go.src = 'https://apis.google.com/js/plusone.js';
      goS = document.getElementsByTagName('script')[0];
      goS.parentNode.insertBefore(go, goS);
      tw = document.createElement('script');
      tw.type = 'text/javascript';
      tw.async = true;
      tw.src = 'http://platform.twitter.com/widgets.js';
      twS = document.getElementsByTagName('script')[0];
      twS.parentNode.insertBefore(tw, twS);
      _ref = this.projectCollection.models;
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        projectModel = _ref[_i];
        _results.push(this.appendThumbnail(projectModel));
      }
      return _results;
    };

    AppView.prototype.appendThumbnail = function(model_) {
      var thumbnailView;
      thumbnailView = new ThumbnailView({
        model: model_
      });
      thumbnailView.bind(thumbnailView.EVENT_CLICKED, this.onThumbnailClicked);
      thumbnailView.bind(thumbnailView.EVENT_LOADED, this.onThumbnailLoaded);
      return $("#container").append(thumbnailView.render().el);
    };

    AppView.prototype.closeZoom = function() {
      var _this = this;
      this.currentImage = "";
      return $('#zoom').stop().animate({
        opacity: 0.0
      }, 100, function() {
        $('#zoom').hide(500);
        return _this.scrollUp();
      });
    };

    AppView.prototype.onThumbnailClicked = function(model_) {
      var _this = this;
      if (this.currentImage === model_.get("image_url")) return;
      this.currentImage = model_.get("image_url");
      return $('#zoom').stop().animate({
        opacity: 0.0
      }, 100, function() {
        _this.scrollUp();
        return $('#zoom').hide(500, function() {
          var zoomTemplate;
          zoomTemplate = _.template($("#template_zoom").html());
          $("img", '#zoom').remove();
          $('#zoom').html('');
          $('#zoom').append(zoomTemplate({
            image_url: "images/projects/" + _this.currentImage,
            title: model_.get("title"),
            description: model_.get("description")
          }));
          $("img", '#zoom').load(function() {
            return window.realWidth = this.width;
          });
          return $("img", '#zoom').load(function() {
            _this.onResize();
            return $('#zoom').show(500).animate({
              opacity: 1.0
            }, 250);
          });
        });
      });
    };

    AppView.prototype.onThumbnailLoaded = function(view_) {
      this.thumbLoaded++;
      if (this.thumbLoaded === this.projectCollection.models.length) {
        return $('#container').stop().isotope('reLayout');
      }
    };

    return AppView;

  })();

  ThumbnailView = (function() {

    __extends(ThumbnailView, Backbone.View);

    function ThumbnailView() {
      this.render = __bind(this.render, this);
      ThumbnailView.__super__.constructor.apply(this, arguments);
    }

    ThumbnailView.prototype.EVENT_CLICKED = "clicked";

    ThumbnailView.prototype.EVENT_LOADED = "loaded";

    ThumbnailView.prototype.events = {
      'click a': 'onClick',
      'mouseover img': 'onOver',
      'mouseleave img': 'onOut'
    };

    ThumbnailView.prototype.initialize = function() {
      $(this.el).addClass("item");
      return this.template = _.template($("#template_thumbnail").html());
    };

    ThumbnailView.prototype.render = function() {
      var _this = this;
      $(this.el).addClass(this.model.get("tags").join(" ").toLowerCase());
      $(this.el).append(this.template({
        thumbnail_url: "images/projects/" + this.model.get("thumbnail_url"),
        image_url: "images/projects/" + this.model.get("image_url"),
        title: this.model.get("title")
      }));
      $("img", this.el).load(function() {
        return _this.trigger(_this.EVENT_LOADED, _this);
      });
      return this;
    };

    ThumbnailView.prototype.onClick = function(e_) {
      e_.preventDefault();
      return this.trigger(this.EVENT_CLICKED, this.model);
    };

    ThumbnailView.prototype.onOver = function(e_) {
      return $("img", this.el).stop().animate({
        opacity: 0.4
      }, 250);
    };

    ThumbnailView.prototype.onOut = function(e_) {
      return $("img", this.el).stop().animate({
        opacity: 1.0
      }, 250);
    };

    return ThumbnailView;

  })();

  ProjectModel = (function() {

    __extends(ProjectModel, Backbone.Model);

    function ProjectModel() {
      ProjectModel.__super__.constructor.apply(this, arguments);
    }

    ProjectModel.prototype.defaults = {
      title: "",
      description: "",
      thumbnail_url: "",
      image_url: "",
      tags: []
    };

    ProjectModel.prototype.initialize = function() {};

    return ProjectModel;

  })();

  ProjectCollection = (function() {

    __extends(ProjectCollection, Backbone.Collection);

    function ProjectCollection() {
      ProjectCollection.__super__.constructor.apply(this, arguments);
    }

    ProjectCollection.prototype.model = ProjectModel;

    ProjectCollection.prototype.url = "data/projects.js";

    ProjectCollection.prototype.initialize = function() {};

    return ProjectCollection;

  })();

  $(function() {
    var app_view;
    return app_view = new AppView();
  });

}).call(this);

