// Generated by CoffeeScript 1.6.3
(function() {
  var Alfred, _ref, _ref1, _ref2, _ref3,
    __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; };

  Alfred = (function(_super) {
    __extends(Alfred, _super);

    function Alfred() {
      _ref = Alfred.__super__.constructor.apply(this, arguments);
      return _ref;
    }

    Alfred.root('todos#index', {
      as: "all"
    });

    Alfred.route('/completed', 'todos#completed');

    Alfred.route('/active', 'todos#active');

    return Alfred;

  })(Batman.App);

  Alfred.TodosController = (function(_super) {
    __extends(TodosController, _super);

    function TodosController() {
      TodosController.__super__.constructor.apply(this, arguments);
      this.set('newTodo', new Alfred.Todo({
        completed: false
      }));
    }

    TodosController.prototype.routingKey = 'todos';

    TodosController.prototype.index = function() {
      return this.set('currentTodoSet', 'all');
    };

    TodosController.prototype.completed = function() {
      this.set('currentTodoSet', 'completed');
      return this.render({
        source: 'todos/index'
      });
    };

    TodosController.prototype.active = function() {
      this.set('currentTodoSet', 'active');
      return this.render({
        source: 'todos/index'
      });
    };

    return TodosController;

  })(Batman.Controller);

  Alfred.TodosIndexView = (function(_super) {
    __extends(TodosIndexView, _super);

    function TodosIndexView() {
      _ref1 = TodosIndexView.__super__.constructor.apply(this, arguments);
      return _ref1;
    }

    TodosIndexView.prototype.createTodo = function(node, event, context) {
      var newTodo, title;
      event.preventDefault();
      title = node[0].value;
      if (title) {
        newTodo = new Alfred.Todo({
          title: title,
          completed: false
        });
        return newTodo.save(function(err) {
          if (!err) {
            return node[0].value = null;
          }
        });
      }
    };

    TodosIndexView.prototype.destroyTodo = function(node, event, context) {
      var todo;
      todo = context.get('todo');
      return todo.destroy(function(err) {
        if (err) {
          throw err;
        }
      });
    };

    TodosIndexView.prototype.toggleAll = function(node, context) {
      return Alfred.Todo.get('all').forEach(function(todo) {
        return todo.set('completed', !!node.checked);
      });
    };

    TodosIndexView.prototype.clearCompleted = function() {
      return Alfred.Todo.get('completed').forEach(function(todo) {
        return todo.destroy(function(err) {
          if (err) {
            throw err;
          }
        });
      });
    };

    TodosIndexView.prototype.toggleEditing = function(node, event, context) {
      var editing, input, todo;
      todo = context.get('todo');
      editing = todo.set('editing', !todo.get('editing'));
      if (editing) {
        input = document.getElementById("todo-input-" + (todo.get('id')));
        return input.focus();
      }
    };

    TodosIndexView.prototype.disableEditingOnEnter = function(node, event, context) {
      if (Batman.DOM.events.isEnter(event)) {
        return node.blur();
      }
    };

    return TodosIndexView;

  })(Batman.View);

  Alfred.TodosActiveView = (function(_super) {
    __extends(TodosActiveView, _super);

    function TodosActiveView() {
      _ref2 = TodosActiveView.__super__.constructor.apply(this, arguments);
      return _ref2;
    }

    return TodosActiveView;

  })(Alfred.TodosIndexView);

  Alfred.TodosCompletedView = (function(_super) {
    __extends(TodosCompletedView, _super);

    function TodosCompletedView() {
      _ref3 = TodosCompletedView.__super__.constructor.apply(this, arguments);
      return _ref3;
    }

    return TodosCompletedView;

  })(Alfred.TodosIndexView);

  Alfred.Todo = (function(_super) {
    __extends(Todo, _super);

    Todo.persist(Batman.LocalStorage);

    Todo.storageKey = 'todos-batman';

    function Todo() {
      Todo.__super__.constructor.apply(this, arguments);
      this.observe('completed', function(newValue, oldValue) {
        return this.save();
      });
      this.observe('editing', function(newValue, oldValue) {
        if (newValue === false) {
          if (this.get('title').length > 0) {
            this.set('title', this.get('title').trim());
            return this.save();
          } else {
            return this.destroy();
          }
        }
      });
      this;
    }

    Todo.encode('title', 'completed');

    Todo.validate('title', {
      presence: true
    });

    Todo.classAccessor('active', function() {
      return this.get('all').filter(function(todo) {
        return !todo.get('completed');
      });
    });

    Todo.classAccessor('completed', function() {
      return this.get('all').filter(function(todo) {
        return todo.get('completed');
      });
    });

    return Todo;

  })(Batman.Model);

  this.Alfred = Alfred;

}).call(this);
