$(document).ready(function(){
    add_events();
})
function add_events(){
    var current = $("#currently");
    var todoList = $("#todo_list");
    var edit = $("#edit");
    var editArea = $("#edit>textarea");
    var addTaskText = $("#addTaskText");
    var addTaskSave = $("#addTaskSave");
    
    edit.hide();
    todoList.show();
    if(current.val() != 'index'){
		$("#edit_back").val('Back');
	} else {
		$("#edit_back").val('Edit');
	}
	
    $("#edit_back").click(function(){
        $(this).unbind();
        if(current.val() == 'index'){
            current.val('index:edit');
            $.get("./ajax.php", 
                { plain: "true", time: "Math.random()" },
                function(data){
                    editArea.val(data);
                }
            );
            todoList.hide();
            edit.show();
        } else {
            current.val("index");
            $.get("./ajax.php", 
                { ajax: "true" },
                function(data){
                    todoList.html(data);
                    add_events();
                }
            );
        }
    });
    $("#edit>input").click(function(){
        current.val("index");
        $.post("./ajax.php",
            { todo: editArea.val() },
            function(data){
                todoList.html(data);
                edit.hide();
                todoList.show();
                add_events();
            }
        );
    });
    $("#addTaskSave").click(function(){
        current.val("index");
        $.post("./ajax.php",
            { task: addTaskText.val() },
	        function(data){
                todoList.html(data);
                edit.hide();
                todoList.show();
                add_events();
            }
        );
	addTaskText.val("");
    });
    $(".tag").each(function(){
        $(this).click(function(){
            current.val("tag:"+this.innerHTML);
            $.get("./ajax.php", 
                { tag: this.innerHTML },
                function(data){
                    todoList.html(data);
                    add_events();
                }
            );
        });
    });
    $("h1").each(function(){
        $(this).click(function(){
            current.val("title:"+this.innerHTML);
            $.get("./ajax.php", 
                { title: this.innerHTML },
                function(data){
                    todoList.html(data);
                    add_events();
                }
            );
        });
    });

    $(".todo").each(function(){
        var checkbox = $(this).children("input");
        if(this.innerHTML.match('@done') != null)
            checkbox.attr({ checked: "checked"});
        checkbox.click(function(){
            $.get("./ajax.php", 
                {
					toggle: "true", 
					todo: checkbox.val(), 
					current: current.val() 
				},
                function(data){
                    todoList.html(data);
                    add_events();
                }  
            )
        })
    })
    $("#project_select").change(function(){
        if($(this).val() != ''){
            current.val("title:"+$(this).val());
            $.get("./ajax.php", 
                { title: $(this).val() },
                function(data){
                    todoList.html(data);
                    add_events();
                }
            );
            this.selectedIndex = 0;
        }
    })
}
function due(days, inclusive, title){
    $.get("./ajax.php", 
        { days: days, inclusive: inclusive, title: title },
        function(data){
            $("#todo_list").html(data);
            add_events();
        }  
    )
}
