var ajaxUrl="/forum/forum.php";
var pageUrl="index.php?PAGE_ID=23";
$(document).ready(function(){
/*    $(".forum h3:first").addClass("active");
    $(".forum .rootChapter:not(:first)").hide();

    $(".forum h3").click(switchChapters);*/
    
    $(".subchapterLink").click(loadChapterContent);
});

/*function switchChapters() {
    $(this).next(".rootChapter").slideToggle("slow")
    .siblings(".rootChapter:visible").slideUp("slow");
    $(this).addClass("active");
    $(this).siblings("h3").removeClass("active");
}*/

function loadChapterContent() {
    $("#subchapterContainer").attr("chapterID", $(this).attr("subchapterID"));
    $.get(ajaxUrl, {
        action: "getChapterContent",
        chapter: $(this).attr("subchapterID")
    }, displayChapterContent);
}
    
function displayChapterContent(chapterContent) {
    $("#subchapterContainer").empty();
    $("#subchapterContainer").append(chapterContent);
    $(".topicLink").click(displayTopic);
}

function displayTopic() {
    window.location.href = pageUrl +"&action=getTopic&topic=" + ($(this).attr("topicID"));
}

function showTopicEditor(topicID) {
    if (topicID > 0) {
        window.location.href = pageUrl +"&action=editTopic&topicID=" + topicID;
    } else {
        chapterID = $("#subchapterContainer").attr("chapterID");
        window.location.href = pageUrl +"&action=createTopic&chapter=" + chapterID;
    }
}

function sendOnCtrlEnter(event) {
    if (event.ctrlKey && ((event.keyCode == 10)||(event.keyCode == 13))) {
        sendAnswer();
    }
}

function sendAnswer() {
    $.post(ajaxUrl, {
        action: "addAnswer",
        answer: $("#answerArea").val(),
        topic: $("#topic").attr("topicID")
    }, displayNewAnswer);
}

function displayNewAnswer(result) {
    authorName = "Вы, только что";
    answer = $("#answerArea").val();
    $("#comments").prepend(printAnswer(authorName, answer));
}

function printAnswer(authorName, answer) {
    result = "<div class=\"comment\">\n";
    result += "    <div class=\"commentAuthorName\">"+authorName+"</div>\n";
    result += "    <div class=\"commentText\">"+answer+"</div>\n";
    result += "</div>";
    result += "<hr>";
    return result;
}
