text: // Generated by CoffeeScript 1.9.1
  var AppRouter, app;
  
  app = void 0;
  
  Backbone.View.prototype.close = function() {
    if (this.beforeClose) {
      this.beforeClose();
    }
    return this.undelegateEvents();
  };
  
  Backbone.View.prototype.updateMeta = function(title, description) {
    $("title").html(title);
    $("meta[name=description]").attr("content", description);
    $("#og-image").attr("content", "http://mylittlefacewhen.com/static/cheerilee-square-300.png");
    $("#cd-layout").remove();
    $("link[rel=image_src]").remove();
    return $("link[rel=canonical]").remove();
  };
  
  Backbone.View.prototype.navigateAnchor = function(event) {
    event.preventDefault();
    return app.navigate(event.currentTarget.getAttribute("href"), {
      trigger: true
    });
  };
  
  AppRouter = Backbone.Router.extend({
    initialize: function() {
      this.faceList = new FaceCollection();
      this.randFaceList = new FaceCollection();
      this.randomQueue = new FaceCollection();
      this.tagList = new TagCollection();
      this.firstLoad = window.location.hash ? false : true;
      this.imageServices = ["http://pinkie.mylittlefacewhen.com", "http://dashie.mylittlefacewhen.com"];
      this.fastest = {
        service: void 0,
        speed: 10000
      };
      _.each(this.imageServices, (function(_this) {
        return function(service) {
          var time;
          time = new Date().getTime();
          return $.ajax({
            method: "GET",
            url: service + "/media/speedtest.txt",
            complete: function() {
              var speed;
              speed = new Date().getTime() - time;
              if (speed < _this.fastest.speed) {
                return _this.fastest = {
                  service: service,
                  speed: speed
                };
              }
            }
          });
        };
      })(this));
      return this.topView = new TopView().render();
    },
    _trackPageview: function() {
      var url;
      try {
        url = Backbone.history.getFragment();
        return _gaq.push(['_trackPageview', "/" + url]);
      } catch (_error) {}
    },
    getImageService: function() {
      return this.fastest.service || this.imageServices[0];
    },
    routes: {
      "": "new",
      "hot": "hot",
      "new": "new",
      "popular": "popular",
      "unreviewed": "unreviewed",
      "develop": "develop",
      "develop/api": "apidoc",
      "develop/api/:version": "apidoc",
      "changelog": "changes",
      "f": "random",
      "f/:id": "face",
      "face": "random",
      "feedback": "feedback",
      "random": "random",
      "randoms": "randoms",
      "search/*query": "search",
      "submit": "submit",
      "tags": "tags"
    },
    hot: function() {
      return this.main("-hotness");
    },
    "new": function() {
      return this.main("-id");
    },
    popular: function() {
      return this.main("-views");
    },
    main: function(ordering) {
      return this.before("#m_posts", (function(_this) {
        return function() {
          var params;
          params = {
            collection: _this.faceList,
            order_by: ordering
          };
          return new MainView(params).render();
        };
      })(this));
    },
    unreviewed: function() {
      return this.before("none", (function(_this) {
        return function() {
          _this.randFaceList = new FaceCollection();
          return new UnreviewedView({
            collection: _this.randFaceList
          }).render();
        };
      })(this));
    },
    apidoc: function(version) {
      return this.before("#m_api", (function(_this) {
        return function() {
          if (version === null) {
            version = "v3";
          }
          console.log(version);
          return _this.pageload(new APIDocView({
            version: version
          }));
        };
      })(this));
    },
    changes: function() {
      return this.before("none", (function(_this) {
        return function() {
          return _this.pageload(new ChangesView());
        };
      })(this));
    },
    develop: function() {
      return this.before("#m_develop", (function(_this) {
        return function() {
          return _this.pageload(new DevelopView());
        };
      })(this));
    },
    face: function(id) {
      return this.before("none", (function(_this) {
        return function() {
          var model, page, random;
          model = _this.faceList.get(id);
          random = _this.randFaceList.get(id);
          if (random) {
            model = random;
          }
          if (!model) {
            page = new SingleView({
              model: new Face({
                id: id
              }),
              firstLoad: _this.firstLoad
            });
          } else {
            page = new SingleView({
              model: model,
              firstLoad: _this.firstLoad
            });
          }
          return _this.pageload(page);
        };
      })(this));
    },
    feedback: function() {
      return this.before("#m_feedback", (function(_this) {
        return function() {
          return _this.pageload(new FeedbackView());
        };
      })(this));
    },
    random: function() {
      var face;
      if (this.randomQueue.length < 1) {
        return this.randomQueue.fetch({
          data: {
            order_by: "random",
            limit: 3,
            accepted: true,
            removed: false
          },
          success: (function(_this) {
            return function(data) {
              return _this.random();
            };
          })(this)
        });
      } else {
        this.select("none");
        face = this.randomQueue.pop();
        if (!this.randFaceList.get(face.id)) {
          this.randFaceList.add(face);
        }
        return app.navigate("f/" + (face.get("id")) + "/", {
          trigger: true
        });
      }
    },
    randoms: function() {
      return this.before("#m_randoms", (function(_this) {
        return function() {
          return new RandomsView().render();
        };
      })(this));
    },
    submit: function() {
      return this.before("#m_submit", (function(_this) {
        return function() {
          return _this.pageload(new SubmitView());
        };
      })(this));
    },
    search: function() {
      return this.before("none", (function(_this) {
        return function() {
          return new SearchView().render();
        };
      })(this));
    },
    tags: function() {
      return this.before("#m_tags", (function(_this) {
        return function() {
          return _this.pageload(new TagsView({
            collection: _this.tagList
          }));
        };
      })(this));
    },
    pageload: function(page) {
      if (this.firstLoad) {
        return page;
      } else {
        return page.render();
      }
    },
    select: function(item) {
      $("#topmenu div").removeClass("selected");
      return $(item + " div").addClass("selected");
    },
    before: function(select, callback) {
      this.select(select);
      this._trackPageview();
      if (this.currentPage) {
        this.currentPage.close();
      }
      this.currentPage = callback();
      if (this.firstLoad) {
        return this.firstLoad = false;
      }
    }
  });
  
  tpl.loadTemplates(["main", "thumbnail", "top", "single", "tag", "randoms", "randomsImage", "apidoc-v1", "apidoc-v2", "apidoc-v3", "changelog", "develop", "feedback", "submit", "submitItem", "search", "tags", "tagsItem", "meta"], function() {
    var action, route, routes;
    routes = AppRouter.prototype.routes;
    for (route in routes) {
      action = routes[route];
      routes[route + "/"] = action;
    }
    AppRouter.prototype.routes = routes;
    app = new AppRouter();
    return Backbone.history.start({
      pushState: true
    });
  });
  