function add_post_remove_confirmation() {
    $(".posted").find("a:contains('Remove')").click(function(e) {
        if(!confirm("For real? :o")) {
            e.preventDefault();
        }
    });
}

function post_reset() {
    $("#edit :reset").click(function(e) {
        e.preventDefault();
        if(confirm("Really reset?")) {
            $("#edit input:text").val("");
            $("#edit textarea").val("");

            $("#newtitle").html("");
            $("#newcontent").html("");
            $("#newtag").html("");
        }
    });
}

function set_post_changes() {
    var textarea = function() {
        $("#newcontent").html(markdown($(this).val()));
    }
    $("#edit textarea").change(textarea);
    $("#edit textarea").keydown(textarea);
    $("#edit textarea").keyup(textarea);

    var title = function() {
        $("#newtitle").html(markdown($(this).val()));
    }
    $("#edit .title").change(title);
    $("#edit .title").keydown(title);
    $("#edit .title").keyup(title);

    var category = function() {
        var cat = $(this).val();
        if(cat != "") {
            var sum = ' in ' + '<a href="' + url_base() + 'blog/category/' +
                cat + '">' + cat + '</a>';
        }
        else {
            sum = "";
        }
        $("#newcat").html(sum);
    }
    $("#edit .category").change(category);
    $("#edit .category").keydown(category);
    $("#edit .category").keyup(category);

    var tags = function() {
        var val = $(this).val();
        if(val != "") {
            var tags = val.split(';');
            var sum = ' Tagged ';
            for(x in tags) {
                sum +=  '<a href="' + url_base() + 'blog/tags/' +
                    tags[x] + '">' + tags[x] + '</a>, ';
            }
            //remove trailing ', '
            val = sum.substr(0, sum.length - 2);
        }
        $("#newtag").html(val);
    }
    $("#edit .tags").change(tags);
    $("#edit .tags").keydown(tags);
    $("#edit .tags").keyup(tags);
}

//add removal confirm to files
function add_file_remove_confirmation() {
    $(".filebox").find("a:contains('Delete')").livequery('click',function(e) {
        if(!confirm("Remove?")) {
            e.preventDefault();
        }
    });
}

//register Delete within summary
//the one in search and post archive
function add_summary_delete() {
    $(".summarybody").find("a:contains('Delete')").livequery('click',function(e) {
        e.preventDefault();

        if(confirm("Delete?")) {

            var body = $(this).parent(".summarybody");
            var link = $(this).attr('href');

            body.html(
                '<span id="loader">' +
                    '<img src="'+ url_base() +'media/images/ajax-loader.gif" />' +
                '</span>'
            );

            $.post(link, function() {
                body.remove();
            });
        }
    });
}

function add_comment_handlers() {
    //register Delete for all comments
    $("#comment .commentinfo").find("a:contains('Delete')").livequery('click', function(e) {
        e.preventDefault();

        $(this).parent().append(
            '<span id="loader">' +
                '<img src="'+ url_base() +'media/images/ajax-loader.gif" />' +
            '</span>'
        );

        var comment = $(this).parents("#comment");
        $.post(url_base() +"blog/delete_comment/" + $(this).attr("id"),
            function() {
                comment.prepend('<span id="removed">Removed comment&nbsp;&nbsp;<a href="" id="toggle">Show</a></span>');
                comment.attr("id", "removed_comment");

                comment.find("a:contains('Edit')").remove();
                var del = comment.find("a:contains('Delete')");
                var href = del.attr('href');
                //make the delete href point to undelete ;)
                href = href.replace(/^(.*)(delete_comment\/[0-9]+)$/g, "$1un$2");
                del.attr('href', href);
                //rename it to reflect tha subtle change
                del.html('Undelete');

                comment.find("#text").hide();
                comment.find(".commentinfo").hide();

                comment.find("#loader").remove();
            }
        );
    });

    //register Erase for all comments
    $(".commentinfo").find("a:contains('Erase')").livequery('click', function(e) {
        e.preventDefault();

        if(confirm("Erase?!")) {

            $(this).parent().append(
                '<span id="loader">' +
                    '<img src="'+ url_base() +'media/images/ajax-loader.gif" />' +
                '</span>'
            );

            var comment = $(this).parents("#comment");
            if(!comment.length) {
                comment = $(this).parents("#removed_comment");
            }
            $.post(url_base() + "admin/erase_comment/" + $(this).attr("id"),
                function() {
                    comment.remove();
                    decr_comments_header();
                }
            );
        }
    });

    //add edit button to all comments who have Delete
    //if they have they're the owner :o
    $("#comment .commentinfo").each(function() {
        var id = $(this).find("a:contains('Delete')").attr("id");
        $(this).find("a:contains('Delete')").before(
            '<a href="javascript:return false;">Edit</a> '
        );
    });

    //register edit event
    //show a small preview of the edit, hide the 'real' comment
    $("#comment .commentinfo").find("a:contains('Edit')").livequery('click',function(e) {
        e.preventDefault();
        var real_comment = $(this).parent().parent();
        var content = real_comment.find("#text").html();
        var form_content = reverse_comment_markdown(content);
        var composed = real_comment.find("#name");
        var name = $(composed).find("a");
        var date = real_comment.find("#date").html();
        if(name.html() == null) {
            name = composed.html();
            site = "";
        }
        else {
            name = name.html();
            site = $(composed).find("a").attr("href");
        }
        composed = composed.html();
        real_comment.after(
            '<div class="commentboxedit" id="comment">' +
                '<span id="text">' + content +
                '</span>' +
                '<div class="commentinfo">' +
                    '<span id="name">' + composed + '</span> on <span id="date">' + date + '</span>' +
                '</div>' +
            '</div>' +
            '<form action="" method="post" id="editcommentform" class="commentbox">' +
                '<textarea id="text" rows="4" name="comment "class="comment">' +
                form_content.trim() + '</textarea><br /><br />' +
                '<input type="text" id="name" class="name" value="' + name.trim() + '"/>' +
                '<input type="text" id="site" class="site" value="' + site.trim() + '"/>' +
                '<a href="">Confirm</a> <a href="">Reset</a> <a href="">Cancel</a>' +
            '</form>'
        );
        real_comment.hide();
    });

    //register events for comment preview inside an edit
    $("#editcommentform textarea.comment").livequery('change',update_editcomment_content);
    $("#editcommentform textarea.comment").livequery('keydown',update_editcomment_content);
    $("#editcommentform textarea.comment").livequery('keyup',update_editcomment_content);

    $("#editcommentform #name").livequery('change',update_editcomment_name_site);
    $("#editcommentform #name").livequery('keydown',update_editcomment_name_site);
    $("#editcommentform #name").livequery('keyup',update_editcomment_name_site);

    $("#editcommentform #site").livequery('change',update_editcomment_name_site);
    $("#editcommentform #site").livequery('keydown',update_editcomment_name_site);
    $("#editcommentform #site").livequery('keyup',update_editcomment_name_site);

    //cancel the edit, remove preview and restore the original
    $("#editcommentform").find("a:contains('Cancel')").livequery('click',function(e) {
        e.preventDefault();
        var form = $(this).parent();
        var edit = form.prev();
        var real = edit.prev();

        form.remove();
        edit.remove();
        real.show();
    });

    //reset the edit - clear it
    $("#editcommentform").find("a:contains('Reset')").livequery('click',function(e) {
        e.preventDefault();
        var form = $(this).parent();
        var edit = form.prev();

        form.find("textarea.comment").val("");
        form.find("textarea.comment").addClass("badcomment");
        form.find("#name").val("");
        form.find("#site").val("");

        edit.find("#text").html("-content-");
        edit.find("#name").html("Anonymous");
    });

    //add confirm handler for edit, send comment, recieve and restore original with the edits
    $("#editcommentform").find("a:contains('Confirm')").livequery('click',function(e) {
        e.preventDefault();
        var form = $(this).parent();
        var edit = form.prev();
        var real = edit.prev();

        var content = form.find("textarea.comment").val();
        var name = form.find("#name").val();
        if(name == "") {
            name = "Anonymous";
        }
        var site = form.find("#site").val();

        if(!content.length) return;

        form.html(
            '<span id="loader">' +
                '<img src="'+ url_base() +'media/images/ajax-loader.gif" />' +
            '</span>'
        );

        $.post(url_base() + "blog/edit_comment/" + real.find("a:contains('Delete')").attr('id'),
            {
                name: name,
                site: site,
                content: content
            },
            function(xml) {
                var composed = compose_name_site($("name",xml).text(), $("site",xml).text());
                var content = $("content",xml).text();

                real.find("#text").html(comment_markdown(content));
                real.find("#name").html(composed);

                edit.remove();
                form.remove();
                real.show();

            }, 'xml'
        );
    });

    //add Show link for deleted comments
    //hide the real comment
    $("#removed_comment #text").each(function() {
        var comment = $(this).parent();
        var removed = comment.find("#removed");
        removed.append(
            '&nbsp;&nbsp;<a href="" id="toggle">Show</a>'
        );

        $(this).hide();
        comment.find(".commentinfo").hide();
    });

    //register hide/show for deleted comments
    $("#removed_comment #removed").find("a").livequery('click', function(e) {
        e.preventDefault();

        var action = $(this).html();
        var comment = $(this).parent().parent();
        if(action == "Show") {
            var text = comment.find("#text");
            var info = comment.find(".commentinfo");

            text.show();
            info.show();

            $(this).html("Hide");
        }
        else if(action == "Hide") {
            var text = comment.find("#text");
            var info = comment.find(".commentinfo");

            text.hide();
            info.hide();

            $(this).html("Show");
        }
    });

    //register undelete action
    $("#removed_comment .commentinfo").find("a:contains('Undelete')").livequery('click',function(e) {
        e.preventDefault();

        $(this).parent().append(
            '<span id="loader">' +
                '<img src="'+ url_base() +'media/images/ajax-loader.gif" />' +
            '</span>'
        );

        var comment = $(this).parents("#removed_comment");
        $.post(url_base() + "blog/undelete_comment/" + $(this).attr("id"),
            function() {
                comment.find("#removed").remove();
                comment.attr("id", "comment");

                var del = comment.find("a:contains('Undelete')");
                var href = del.attr('href');
                //change undelete href to delete
                href = href.replace(/^(.*)(un)(delete_comment\/[0-9]+)$/g, "$1$3");
                del.attr('href', href);
                //set link name to reflect small change
                del.html('Delete');

                var id = del.attr("id");
                del.before(
                    '<a href="">Edit</a> '
                );

                comment.find("#loader").remove();
            }
        );
    });
}

$(function() {
    //add edit/remove/delete handlers to comments
    add_comment_handlers();

    //add delete
    add_summary_delete();

    //add confirmation on remove
    add_post_remove_confirmation();
    add_file_remove_confirmation();

    //post creation
    post_reset();
    set_post_changes();
});


