(function() {
  var Nav;

  window.Coopers.Nav = Nav = (function() {

    function Nav(nav) {
      this.navJSON = nav;
      this.nav = [];
      this.tags = [];
      this.upLevel = false;
      this.buildNodeItems(this.navJSON);
      this.createDIYBeerNav();
      this.createMerchNav();
    }

    Nav.prototype.createMerchNav = function() {
      var count, exception, globalCount, grid_c, grid_o, grids, inner, item, li, m, ul, _i, _j, _len, _len2, _ref, _ref2;
      inner = $("#" + this.tags[0] + " .inner");
      _ref = this.nav[0][this.tags[0]];
      grid_o = "<div class='grd'>";
      grid_c = "</div>";
      grids = "";
      exception = 3;
      globalCount = 1;
      count = 0;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        item = _ref[_i];
        if (globalCount <= exception) {
          ul = "<ul>";
          _ref2 = item.children;
          for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
            li = _ref2[_j];
            ul = ul + li;
          }
          ul = ul + "</ul>";
          if (count === 0) {
            grids = grids + (grid_o + item.name + ul);
            count++;
            globalCount++;
          } else if (count === 1) {
            grids = grids + (item.name + ul + grid_c);
            count = 0;
          }
        }
      }
      m = "<h3 class=\"exception top\"><a href=\"/shop-merch/new-in-store\">New in Store</a></h3>";
      inner.prepend(grids);
      inner.find("ul").each(function() {
        return ($(this)).find("li:last").addClass("last");
      });
      return inner.find(".grd:eq(2)").addClass("grd3").append(m);
    };

    Nav.prototype.createGrid = function(grid) {
      grid = $("<div class='grd'>");
      return grid;
    };

    Nav.prototype.createDIYBeerNav = function() {
      var h3, item, right, ul, _i, _len, _ref, _ref2;
      right = $("#" + this.tags[1] + " .right");
      _ref = this.nav[1][this.tags[1]][0];
      h3 = $(_ref.name);
      ul = $("<ul></ul>");
      _ref2 = _ref.children;
      for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
        item = _ref2[_i];
        ul.append(item);
      }
      right.append(h3, ul);
      return ul.find("li:last").addClass("last");
    };

    Nav.prototype.buildNodeItems = function(array, current, next) {
      var href, item, level, name, navReference, _i, _len, _results;
      if (current == null) current = 0;
      if (next == null) next = null;
      current = current;
      if (next) {
        level = current + next;
      } else {
        level = current;
      }
      navReference = array;
      _results = [];
      for (_i = 0, _len = navReference.length; _i < _len; _i++) {
        item = navReference[_i];
        href = item.href;
        name = item.name;
        this.markUpNode(href, name, level);
        if (item.children.length > 0) {
          _results.push(this.buildNodeItems(item.children, level, 1));
        } else {
          _results.push(void 0);
        }
      }
      return _results;
    };

    Nav.prototype.markUpNode = function(href, name, level) {
      var markup;
      if (this.upLevel) {
        if (level === 1 && !this.upLevelPassed) {
          this._lvl1Obj = {};
          markup = "<h3><a href=\"/" + href + "\">" + this._objName + "</a></h3>";
          this._lvl1Obj.name = markup;
          this._lvl1Obj.children = [];
          this._obj[this._objParent].push(this._lvl1Obj);
          this.upLevelPassed = true;
        }
        level = level + 1;
      }
      if (level === 0) {
        this._obj = {};
        this._objParent = href;
        this._obj[href] = [];
        this._objName = name;
        this.tags.push(href);
        this.nav.push(this._obj);
        if (href === "shop-diy-beer") this.upLevel = true;
      }
      if (level === 1) {
        this._lvl1Obj = {};
        markup = "<h3><a href=\"/" + href + "\">" + name + "</a></h3>";
        this._lvl1Obj.name = markup;
        this._lvl1Obj.children = [];
        this._obj[this._objParent].push(this._lvl1Obj);
      }
      if (level === 2) {
        markup = "<li><a href=\"/" + href + "\">" + name + "</a></li>";
        return this._lvl1Obj.children.push(markup);
      }
    };

    return Nav;

  })();

}).call(this);

