"use strict";
/*global Auth*/
/*jslint white: true, devel: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true, indent: 2 */
var Router = {
  history: [],
  routes: [{
    pattern: /^$/,
    action: function () {
      $.modal.close();
    }
  }, {
    pattern: /^terms$/,
    action: function () {
      EK.Auth.showTerms();
    }
  }, {
    pattern: /^(signup|login)$/,
    action: function (hash) {
      EK.Auth.popup(hash);
    }
  }],
  add: function (routes) {
    this.routes = $.merge(this.routes, routes);
  },
  route: function (e) {
    var hash = window.location.hash.replace(/^#/, '');
    //var path = window.location.pathname;
    for (var i = 0; i < this.routes.length; i += 1) {
      var r = this.routes[i];
      if (r.pattern.test(hash) && this.should_execute(hash)) {
        r.action(hash);
        this.history.push(hash);
      }
    }
  },
  should_execute: function (hash) {
    return !this.history.length || (this.history[this.history.length - 1] !== hash);
  }
};
$(function () {
  $(window).bind('hashchange', $.proxy(Router.route, Router));
  window.setTimeout(function () {
    $(window).trigger('hashchange');
  }, 50);
});
