(function(){
	var app = angular.module("todos", []);

	function getNewId() {
		var storedId = _.parseInt(window.localStorage.getItem(window.storageConstants.IDS_KEY)) || 0;
		window.localStorage.setItem(window.storageConstants.IDS_KEY, storedId+1);
		return storedId;
	}

	function category(name) {
		return {
			id: getNewId(),
		};
	}

	function todo(name, dueDate) {
		return {
			id: getNewId(),
			name: name,
			dueDate: dueDate,
			editing: false,
			createdDate: new Date(),
			version: 1, // for comparision in future versions in case data changes
		}
	}

	function category(name) {
		return {
			id: getNewId(), // TODO use a different id set than todos
			name: name,
			todos: [],
			createdDate: new Date(),
			version: 1,
		}
	}

	app.controller("CategoryController", function() {
		var logger = loggerInstance("CategoryController");

		function loadcategories() {
			var storedcategories;

			try {
				storedcategories = angular.fromJson(window.localStorage.getItem(window.storageConstants.CATEGORIES_KEY));
			} catch (e) {
				logger.e("could not parse categories because " + e);
			}

			if (!storedcategories) return [];
			else return storedcategories;
		}

		function saveCategories(categoriesToStore) {
			window.localStorage.setItem(window.storageConstants.CATEGORIES_KEY, angular.toJson(categoriesToStore));
		}

		this.categories = loadcategories();
		this.currentCategory = this.categories[0];

		if (this.categories.length === 0) {
			this.categories.push(new category("cat 1"));
			saveCategories(this.categories);
		}
	});


	app.controller("TodosController", function() {
		var logger = loggerInstance("TodosController");

		function loadTodos() {
			var storedTodos;

			try {
				storedTodos = angular.fromJson(window.localStorage.getItem(window.storageConstants.TODOS_KEY));
			} catch (e) {
				logger.e("could not parse todos because " + e);
			}

			if (!storedTodos) return [];
			else return storedTodos;
		}

		function storeTodos(todosToStore) {
			window.localStorage.setItem(window.storageConstants.TODOS_KEY, angular.toJson(todosToStore));
		}

		this.todos = loadTodos();
		window.todos = this.todos;

		this.addTodo = function(todo){
			logger.d("adding " + todo.id);
			this.todos.push(todo);
			
			this.save();
		}

		this.removeTodo = function(todoId) {
			logger.d("removing " + todoId);
			this.todos = _.reject(this.todos, {id:todoId});
			this.save();
		}

		this.save = function() {
			storeTodos(this.todos);
		}
	});

	app.controller("TodoController", function() {
		this.toggleEdit = function() {
			// this.find("textarea").focus();
		}
	});

	app.controller("AddTodoController", function() {
		this.todo = new todo("", new Date());

		this.addTodo = function(todosController) {
			if (this.todo.name.length === 0) {
				alert("Enter some text");
			} else {
				todosController.addTodo(this.todo);
				this.todo = new todo("", new Date());
			}
		}
	});
})();
@@ -1536,24 +1536,399 @@
 gories);%0A%09%09%7D
+%0A%0A%09%09this.save = function() %7B%0A%09%09%09saveCategories(this.categories);%0A%09%09%7D%0A%0A%09%09this.addTodo = function(todo)%7B%0A%09%09%09logger.d(%22adding %22 + todo.id);%0A%09%09%09this.currentCategory.todos.push(todo);%0A%09%09%09%0A%09%09%09this.save();%0A%09%09%7D%0A%0A%09%09this.removeTodo = function(todoId) %7B%0A%09%09%09logger.d(%22removing %22 + todoId);%0A%09%09%09this.currentCategory.todos = _.reject(this.categories.todos, %7Bid:todoId%7D);%0A%09%09%09this.save();%0A%09%09%7D
 %0A%09%7D);%0A%0A%0A%09app
@@ -2488,37 +2488,8 @@
 s();
-%0A%09%09window.todos = this.todos;
 %0A%0A%09%09

Work on adding todos to categories