$(document).ready(function() {
    var chapterid = $("div.chapter>div.titlepage>div>div>h2.title>a").attr('id');
    if (!chapterid) chapterid = $("div.appendix>div.titlepage>div>div>h2.title>a").attr('id');
    if (!chapterid) return false;
    $("div.toc").each(function() {
        $(this).append("<div class=\"comment_count\">Loading...</div>");
    });
    function initialise(id) {
        return "<span class=\"comments\" id=\"chap" + id + "\"><span pid=\"" + id + "\">" + "Loading..." + "</span></span>";
    }
    function pinitialise() {
        return "<span class=\"chapter_comments\" id=\"chapter_comments_info\"><span>" + "Loading..." + "</span></span>";
    }
    $("p[@id]").each(
        function () {$(this).append(initialise($(this).attr("id")));}
    );
    $("pre[@id]").each(
        function () {$(this).append(initialise($(this).attr("id")));}
    );
    $("div.article").each(
        function () {$(this).append(pinitialise());}
    );
    $.getJSON(location.protocol + "//" + location.host + "/comment/count/chapter/" + chapterid,
        function (data) {
            var total = 0;
            $.each(data,
                function (id, count) {
                    var intgr = count-0;
                    total = total + intgr;
                }
            );
            $("div.toc div.comment_count").each(function() {
                $(this).replaceWith("<div class=\"comment_count\"><a href=\"#\" onclick=\"loadAllComments();return false;\">Load all "+total+" Comments (Slow)</a></div>");
            });
            $.each(data,
                function (id, count) {
                    var str = count == 1 ? "comment" : "comments";
                    $("span#chap"+id+" a").replaceWith(
                        "<a href=\"#\" id=\"link"+id+"\" onclick=\"loadComments('"+id+"');return false;\">"
                        + count + " " + str + "</a>"
                    );
                    $("span#chap"+id+" a").fadeTo("normal", 0.35).hover(
                        function() { $(this).fadeTo("normal", 0.8); },
                        function() { $(this).fadeTo("normal", 0.35); }
                    );
                }
            );
        }
    );
    $.getJSON(location.protocol + "//" + location.host + "/comment/countchapter/chapter/" + chapterid,
        function (data) {
            $.each(data,
                function (id, count) {
                    var str1 = count == 1 ? "There is" : "There are";
                    var str2 = count == 1 ? "chapter comment" : "chapter comments";
                    $("span#chapter_comments_info a").replaceWith(
                        "<a href=\"#\" id=\"chapter_comments_link\" onclick=\"loadPageComments();return false;\">"
                        + str1 + " " + count + " " + str2 + "</a>"
                    );
                    $("span#chapter_comments_info a").fadeTo("normal", 0.35).hover(
                        function() { $(this).fadeTo("normal", 0.8); },
                        function() { $(this).fadeTo("normal", 0.35); }
                    );
                }
            );
        }
    );
    $("span.comments span").each(function() {
        var id = $(this).attr("pid");
        $(this).replaceWith(
            "<a href=\"#\" id=\"link"+id+"\" onclick=\"loadComments('"+id+"');return false;\">"
            + "No comments</a>"
        );
    });
    $("span.comments a").fadeTo("normal", 0.35).hover(
        function() { $(this).fadeTo("normal", 0.8); },
        function() { $(this).fadeTo("normal", 0.35); }
    );
    $("span.chapter_comments span").each(function() {
        $(this).replaceWith(
            "<a href=\"#\" id=\"chapter_comments_link\" onclick=\"loadPageComments();return false;\">"
            + "No chapter comments</a>"
        );
    });
    $("span.chapter_comments a").fadeTo("normal", 0.35).hover(
        function() { $(this).fadeTo("normal", 0.8); },
        function() { $(this).fadeTo("normal", 0.35); }
    );
    $("div#powered img").mouseover(function() {
        var oldsrc = $(this).attr("src");
        var newsrc = oldsrc.replace("_grey","");
        $(this).attr("src",newsrc);
    }).mouseout(function() {
        var oldsrc = $(this).attr("src");
        var newsrc = oldsrc.replace(".","_grey.");
        $(this).attr("src",newsrc);
    });
});

function loadComments(id) {
    var chapterid = $("div.chapter>div.titlepage>div>div>h2.title>a").attr('id');
    $.get(location.protocol + "//"
    + location.host + "/comment/chapter/" + chapterid + "/paragraph/" + id, function(data) {
        $("#"+id).after(data);
        formAsAjax(id);
        $("#comments"+id).slideToggle("slow");
    });
    $("#link"+id).removeAttr("onclick");
    $("#link"+id).click(function() {
        $("#comments"+id).slideToggle("slow");
        $("#comments"+id+" p.success").slideToggle("fast");
        $("#comments"+id+" p.success").remove();
        return false;
    });
    return false;
}

function loadPageComments() {
    var chapterid = $("div.chapter>div.titlepage>div>div>h2.title>a").attr('id');
    $.get(location.protocol + "//"
    + location.host + "/pagecomments/chapter/" + chapterid, function(data) {
        $("#chapter_comments_info").after(data);
        pformAsAjax();
        $("#chapter_comments").slideToggle("slow");
    });
    $("#chapter_comments_link").removeAttr("onclick");
    $("#chapter_comments_link").click(function() {
        $("#chapter_comments").slideToggle("slow");
        $("#chapter_comments"+" p.success").slideToggle("fast");
        $("#chapter_comments"+" p.success").remove();
        return false;
    });
    return false;
}

function loadAllComments() {
    $("p[@id]").each(function() {
        var id = $(this).attr("id");
        var commentText = $("#link"+id).html();
        if (commentText !== "No comments") {
            loadComments(id);
        }
    });
    $("div.comment_count a").each(function() {
        $(this).removeAttr("onclick");
        $(this).click(function() {
            $("p[@id]").each(function() {
                var id = $(this).attr("id");
                $("#comments"+id).slideToggle("slow");
                $("#comments"+id+" p.success").slideToggle("fast");
                $("#comments"+id+" p.success").remove();
            });
        });
    });
    return false;
}

function formAsAjax(id) {
    $('#form'+id).ajaxForm({success: function(responseText, statusText) {
        $("#comments"+id).replaceWith(responseText);
        formAsAjax(id);
        var commentCount = $("#comments"+id+" div.comment").size();
        var str = commentCount == 1 ? "comment" : "comments";
        if (commentCount > 0){
            $("#link"+id).html(commentCount+" "+str);
        }
    }});
}

function pformAsAjax() {
    $('#chapter_comments_form').ajaxForm({success: function(responseText, statusText) {
        $("#chapter_comments").replaceWith(responseText);
        pformAsAjax();
        var commentCount = $("#chapter_comments div.comment").size();
        var str = commentCount == 1 ? "comment" : "comments";
        if (commentCount > 0){
            $("#chapter_comments_link").html(commentCount+" "+str);
        }
    }});
}
