// Generated by CoffeeScript 1.3.3
(function() {
  var TabSet,
    __hasProp = {}.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; };

  TabSet = (function(_super) {

    __extends(TabSet, _super);

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

    TabSet.prototype.initialize = function(options) {
      "the template for this view needs to have a tab holder element and\na pane holder element\n\n\nI wish I had one dictionary, but I can't figure out what the key should be\n\n{ x : {tab_el, pane_el, view}\n\nahh , the key should be route";

      var tab_view_obj, _i, _len, _ref;
      this.el = $(options.el);
      this.tab_view_objs = options.tab_view_objs;
      this.tab_holder_el = $("<ul class='nav nav-tabs'></ul>");
      this.pane_holder_el = $("<div class='tab-content'></div>");
      this.tab_view_dict = {};
      _ref = this.tab_view_objs;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        tab_view_obj = _ref[_i];
        this.add_tab(tab_view_obj);
      }
      return this.render();
    };

    TabSet.prototype.render = function() {
      $(this.el).append(this.tab_holder_el);
      return $(this.el).append(this.pane_holder_el);
    };

    TabSet.prototype.add_tab = function(tab_view_obj) {
      var route, tvo;
      tvo = tab_view_obj;
      route = tvo.route;
      if (_.has(this.tab_view_dict, route)) {
        this.activate(route);
      } else {
        this.tab_view_dict[route] = tab_view_obj;
        tvo.tab_el = this._create_tab(tab_view_obj);
        tvo.pane_el = this._create_pane(tab_view_obj);
        this.tab_holder_el.append(tvo.tab_el);
        this.pane_holder_el.append(tvo.pane_el);
      }
      if (_.keys(this.tab_view_dict).length === 1) {
        this.activate(_.keys(this.tab_view_dict)[0]);
      }
      return tvo.pane_el;
    };

    TabSet.prototype._remove_tab = function(route) {
      var tvo;
      tvo = this.tab_view_dict[route];
      $(tvo.tab_el).remove();
      return $(tvo.pane_el).remove();
    };

    TabSet.prototype._create_tab = function(tab_view_obj) {
      var tab,
        _this = this;
      tab = $("<li><a id=" + tab_view_obj.route + "Tab class='tab-tab'>" + tab_view_obj.tab_name + "</a></li>");
      tab.click(function(e) {
        return _this.activate(tab_view_obj.route);
      });
      return tab;
    };

    TabSet.prototype._create_pane = function(tab_view_obj) {
      var pane;
      pane = $("<div class='tab-pane'></div>");
      return pane.append(tab_view_obj.view.el);
    };

    TabSet.prototype.activate = function(route) {
      " I'm not sure how to handle the deferred rendering bit here,\nshould tabs only be rendered on first view? or on instatiation?\nwhat about tabs that depend on data being loaded?\n\nthe common pattern in backbone land that I have seen is\n\n\nBackbone.Router.extend({\n  routes: {\n    \"some_tab\" : ->\n      model = new ModelType({id:5})\n      $.when(model.fetch()).then(->\n        view = new View(model:model, el: '#some_id')\n        view.render())}})\n\n\nso far as this tab container is concerned, I want to flip that on\nits head and somehow bundle calling render() on the view from the\nTabContainer, make the view responsible for fetching its own model\n\nI'm not quite sure how to do that, or whether or not I will run\ninto problems that way\n\n\nhopefully I can figure out how to plumb activate into a router\n\n";

      var tab_view_obj, tvo;
      tvo = tab_view_obj = this.tab_view_dict[route];
      this.tab_holder_el.find('.active').removeClass('active');
      tvo.tab_el.addClass('active');
      this.pane_holder_el.find('.active').removeClass('active');
      return tvo.pane_el.addClass('active');
    };

    TabSet.prototype.remove_tab = function(route) {
      this._remove_tab(route);
      return delete this.tab_view_dict[route];
    };

    return TabSet;

  })(Backbone.View);

  window.$AL.TabSet = TabSet;

}).call(this);
