var DELETE_MSG = "Are you sure you want to delete this paper?\nNote: There is no Undo."
var DELETE_POST_MSG = "Are you sure you want to delete this post?\nNote: There is no Undo."

$(document).ready(function() {
    var urls = window.location.href.substring(7).split("/");
    if (urls.length > 2 && urls[2] == "view") {
        jQuery.hotkeys.add('Shift+e', function() { view_project_action("edit"); });
        jQuery.hotkeys.add('Ctrl+Shift+d', function() { view_project_action("archive"); });    
        jQuery.hotkeys.add('Shift+f', fold);
        jQuery.hotkeys.add('Shift+u', unfold);
    }
    init_events();
})

function init_events() {
    $("div.project:first").css("margin-top", "0");
    
    $("a.delete").click(function() {
        var answer = confirm(DELETE_MSG);
        if (answer) {
            var href = $(this).attr("href");
            var args = href.split("/");
            var post_url = "/" + args[1] + "/delete/" + args[2];
            $.post(post_url, 
                { username: args[1], papername: args[2] },
                function () { window.location.href = "/" + args[1] + "/view/"; }
            );
        } 
        return false;
    });
    
    $("a.delete_post").click(function() {
        var answer = confirm(DELETE_POST_MSG);
        if (answer) {
            var href = $(this).attr("href");
            var args = href.split("/");
            var post_url = "/news/delete/" + args[3];
            $.post(post_url, 
                { username: args[1], papername: args[2] },
                function () { window.location.href = "/news"; }
            );
        } 
        return false;
    });
    
    $(".task").each(function() { do_task($(this)); });
        
    $("a.project").each(function() {
        var urls = window.location.href.substring(7).split("/");
        var project = this.innerHTML.substring(0, this.innerHTML.length-1)
        var project_url = "/" + urls[1] + "/view/" + urls[3] + "/project/" + project;
        $(this).attr("href", project_url);
    });
    
    $("img.fold").click(function() {
        if ($(this).attr("src") == "/static/arrow_bottom.png") {
            $(this).attr("src", "/static/arrow_right.png");    
            $(this).siblings("span.tasks").slideUp();
        } else {
            $(this).attr("src", "/static/arrow_bottom.png");
            $(this).siblings("span.tasks").slideDown();
        }
    });    
    
    $("img.fold").each(function() {
        if ($(this).next().text() == "Archive:") {
            $(this).attr("src", "/static/arrow_right.png");
            $(this).siblings("span.tasks").slideToggle();
        }
    });
    
    $("a#action_fold").click(fold);
    
    $("a#action_unfold").click(unfold);
    
    $("img.edit_project").click(function() { do_edit($(this)) });
            
    $(".tag").each(function() { do_tag($(this)); });
}

function fold() {
    $("img.fold").attr("src", "/static/arrow_right.png");
    $("span.tasks").slideUp(); 
    return false;
}

function unfold() {
    $("img.fold").attr("src", "/static/arrow_bottom.png");
    $("span.tasks").slideDown(); 
    return false;
}

function do_edit(img_tag) {
    var tasks = img_tag.siblings("span.tasks");
    var project_name = tasks.prev().prev().text();
    project_name = project_name.substring(0, project_name.length - 1);
    tasks.hide();
    var urls = window.location.href.substring(7).split("/");
    var ajax_url = "/" + urls[1] + "/edit_project/" + urls[3] + "/project/" + project_name
    $.get(ajax_url,
        null, 
        function(content) {
            tasks.after("<div class='edit_project'><form><textarea>"
                + content + "</textarea></form>"
                + "<p><button type='submit' class='button positive'>"
                + "<img src='/static/blueprint/plugins/buttons/icons/tick.png' alt=''> Save </button>"
                + "<a href='#' class='cancel button negative'>"
                + "<img src='/static/blueprint/plugins/buttons/icons/cross.png' alt=''> Cancel </a></p><br></div>");
            $("textarea").css("height", "180px");
            $(":button.positive").click(function() {
                $.post(ajax_url, 
                        { content: $("textarea").text() },
                        function(html) {
                            alert(html);
                        }
                );
            });
            $(".cancel").click(function() {
                tasks.show();
                tasks.next().hide();
                return false;
            });
        }
    );
}

function do_tag(current_tag) {
    current_tag.click(function() {
        var urls = window.location.href.substring(7).split("/");
        var tag = this.innerHTML;
        window.location.href = "/" + urls[1] + "/view/" + urls[3] + "/tag/" + tag;
    });
}

function do_task(current_task) {
    var img = current_task.children("img");
    
    img.click(function() {
        var urls = window.location.href.substring(7).split("/");
        var toggle = (img.attr("src") == "/static/unchecked.png" 
                        || img.attr("src") == ("http://" + urls[0] + "/static/unchecked.png"));
		img.attr("src", "/static/ing.gif");			
        $.post(window.location.href + "/task",
            {
				line: img.attr("id")
			},
            function (html) {
                current_task.replaceWith(html);
                var new_task = $("#" + img.attr("id")).parent();
                do_task(new_task);
                new_task.find(".tag").each(function() { do_tag($(this)); });
            }  
        );
    });    
}

function view_project_action(action) {
    var urls = window.location.href.substring(7).split("/");
    if (urls[2] != "view" || urls[4] == "tag") 
        return;
    
    if (urls.length == 4)
        window.location.href = "/" + urls[1] + "/" + action + "/" + urls[3];
    else
        window.location.href = "/" + urls[1] + "/" + action + "/" + urls[3] + "/project/" +  urls[5];
}