var app = app || {};

(function () {
    'use strict';

    app.controller = function() {

        this.list = new app.TodoList(); // Todo collection
        this.title = m.prop('');        // Temp title placeholder
        this.filter = m.prop(m.route.param('filter') || '');       // TodoList filter

        // Add a Todo 
        this.add = function(title) {
            if(this.title()) {
                this.list.push(new app.Todo({title: title()}));
                this.title('');
            }
        };

        //check whether a todo is visible
        this.isVisible = function(todo) {
            if(this.filter() == '')
                return true;
            if (this.filter() == 'active')
                return !todo.completed();
            if (this.filter() == 'completed')
                return todo.completed();
        }
        
        this.clearTitle = function() {
            this.title('')
        }

        // Removing a Todo from the list
        this.remove = function(key) {
            this.list.splice(key, 1)
        }

        // Remove all Todos where Completed == true
        this.clearCompleted = function() {
            for(var i = 0; i < this.list.length; i++) {
                if(this.list[i].completed())
                    this.list.splice(i, 1)
            }
        }

        // Total amount of Todos completed
        this.amountCompleted = function() {
            var amount = 0;
            
            for(var i = 0; i < this.list.length; i++)
                if(this.list[i].completed())
                    amount++;

            return amount;
        }
    };
    
})();

@@ -1163,39 +1163,32 @@
      for(var i =
- 0; i %3C
  this.list.lengt
@@ -1188,21 +1188,33 @@
 t.length
-; i++
+ - 1; i %3E= 0; i--
 ) %7B%0A    
@@ -1219,32 +1219,33 @@
               if
+ 
 (this.list%5Bi%5D.co
@@ -1246,32 +1246,34 @@
 %5Bi%5D.completed())
+ %7B
 %0A               
@@ -1295,24 +1295,43 @@
 splice(i, 1)
+;%0A                %7D
 %0A           

Fix "clear completed" button to actually work