

var savedContent = "";
var currentlyEditingPostId = "";

function updatePostContent(post)
{
    var xmlHttp = createXHRequest();
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4)
            document.getElementById("post-area-content").value = xmlHttp.responseText;
    }
    xmlHttp.open("GET", "/users/ajax/get-post-content.jsp?post=" + post, true);
    xmlHttp.send(null);
}

function togglePostArea() {
    hideEditArea();
    var postArea = document.getElementById("post-area");
    var icon = document.getElementById("img-post-area");

    if(postArea.style.display == "block") {
        postArea.style.display = "none";
        icon.src = "/images/new.gif";
        icon.alt = "Ecrire un article";
    } else {
        postArea.style.display = "block";
        icon.src = "/images/cancel.gif";
        icon.alt = "Annuler";
        document.getElementById("post-area-title").focus();
    }

    icon.title = icon.alt;
}

function toggleComments(postId) {
    var container = document.getElementById('comments-' + postId);
    if(container.style.display == 'block')
        container.style.display = 'none';
    else
        container.style.display = 'block';
}

function toggleCommentArea(postId) {
    var container = document.getElementById('comment-area-' + postId);
    if(container.style.display == 'block')
        container.style.display = 'none';
    else {
        container.style.display = 'block';
        document.getElementById('comment-area-content-' + postId).focus();
    }
}

function toggleEditArea(postId, title) {
    if(currentlyEditingPostId == postId) {
        hideEditArea();
        return;
    }

    hideEditArea();

    var content = document.getElementById("post-" + postId);
    var postArea = document.getElementById("post-area");
    currentlyEditingPostId = postId;

    savedContent = content.innerHTML;

    document.getElementById("post-area-container").removeChild(postArea);

    content.innerHTML = "";
    content.appendChild(postArea);
    updatePostContent(postId);

    var icon = document.getElementById("img-edit-" + currentlyEditingPostId);
    icon.src = "/images/cancel.gif";
    icon.alt = "Annuler";
    icon.title = icon.alt;

    document.getElementById("post-area-post-id").value = postId;
    document.getElementById("post-area-title").value = title;
    postArea.style.display = "block";
    document.getElementById("post-area-title").focus();
}

function hideEditArea() {
    if(currentlyEditingPostId == "") {
        var postIcon = document.getElementById("img-post-area");
        if(icon) {
            postIcon.src = "/images/new.gif";
            postIcon.alt = "Ecrire un article";
        }
        return;
    }

    var icon = document.getElementById("img-edit-" + currentlyEditingPostId);
    icon.src = "/images/edit.gif";
    icon.alt = "Editer";
    icon.title = icon.alt;

    var content = document.getElementById("post-" + currentlyEditingPostId);
    var postArea = document.getElementById("post-area");
    currentlyEditingPostId = "";

    postArea.style.display = "none";
    document.getElementById("post-area-post-id").value = "";
    document.getElementById("post-area-title").value = "";
    document.getElementById("post-area-content").value = "";
    content.removeChild(postArea);

    content.innerHTML = savedContent;
    document.getElementById("post-area-container").appendChild(postArea);
}
