/*

   Copyright (C) 2010, Bálint Kis (balint@k-i-s.net)
   All rights reserved.

*/

function fadeInElement(identifier, opacity, target_opacity, height, target_height) {
    var element = window.document.getElementById(identifier);
    var doItAgain = 0;

    if (opacity < target_opacity) {
        opacity += 5;
        doItAgain = 1;
    }
    else {
        opacity = target_opacity;
    }

    if (height < target_height) {
        height += target_height * 10/100;
        doItAgain = 1;
    }
    else {
        height = target_height;
    }

    setOpacity(element, opacity);
    element.style.height = height + "px";

    if (doItAgain) {
        window.setTimeout("fadeInElement('"+identifier+"', "+opacity+", "+target_opacity+", "+height+", "+target_height+")", 5);
    }
    else {
        element.style.height = "auto";
    }
}

function setOpacity(element, opacity) {
    element.style.filter = "alpha(opacity:"+opacity+")"; /* IE/Win */
    element.style.KHTMLOpacity = opacity/100; /* Konqueror */
    element.style.MozOpacity = opacity/100; /* Mozilla */
    element.style.opacity = opacity/100;
}

function fadeIn(identifier) {
    var fading_element = window.document.getElementById(identifier);
    var target_height = fading_element.offsetHeight;
    fading_element.style.visibility = "visible";
    fading_element.style.height = 0 + "px";
    setOpacity(fading_element, 0);
    fadeInElement(identifier, 0, 100, 0, target_height);
}

function transFadeElement(identifier_hide, identifier_show, opacity, target_opacity) {
    var element_hide = window.document.getElementById(identifier_hide);
    var element_show = window.document.getElementById(identifier_show);
    var doItAgain = 0;

    if (opacity < target_opacity) {
        opacity += 5;
        doItAgain = 1;
    }
    else {
        element_hide.style.display = 'none';
        element_show.style.display = 'block';
        opacity = target_opacity;
    }

    setOpacity(element_hide, 100 - opacity);
    setOpacity(element_show, opacity);

    if (doItAgain) {
        window.setTimeout("transFadeElement('"+identifier_hide+"', '"+identifier_show+"', "+opacity+", "+target_opacity+")", 5);
    }
}

function transFade(identifier_hide, identifier_show) {
    var element_show = window.document.getElementById(identifier_show);
    element_show.style.display = 'block';
    transFadeElement(identifier_hide, identifier_show, 0, 100);
}

var req;
var comment_identifier;

function handleReply() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            var message = req.responseText;
            window.document.getElementById('result').innerHTML = message;
        }
    }
}

function sendHTTPRequest(url, answerHandler) {
    if (typeof XMLHttpRequest != "undefined") {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.onreadystatechange = answerHandler;
    req.send(null);
}

function handleCommentValidationReply() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            var message = req.responseText;
            if (message == 0) {
                window.document.getElementById('comment_validate_' + comment_identifier).style.display = 'none';
                comment_identifier = null;
            }
        }
    }
}

function handleCommentDeletionReply() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            var message = req.responseText;
            if (message == 0) {
                window.document.getElementById('comment_' + comment_identifier).style.display = 'none';
                comment_identifier = null;
            }
        }
    }
}

function handleCommentSaveReply() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            var message = req.responseText;
            if (message == 0) {
                toggleDisplay('comment_body_edit_' + comment_identifier, 'comment_body_' + comment_identifier);
                toggleDisplay('save_button_' + comment_identifier, 'edit_button_' + comment_identifier);
                comment_identifier = null;
            }
        }
    }
}

function commentValidate(comment_id) {
    var url = "request.comment.action.php?action=validate&comment=" + encodeURIComponent(comment_id);
    comment_identifier = comment_id;
    sendHTTPRequest(url, handleCommentValidationReply);
}

function commentDelete(comment_id) {
    var x = confirm("Are you sure you want to delete this comment?")
    if (x) {
        var url = "request.comment.action.php?action=delete&comment=" + encodeURIComponent(comment_id);
        comment_identifier = comment_id;
        sendHTTPRequest(url, handleCommentDeletionReply);
    }
}

function commentSave(comment_id, is_member) {
    if (!is_member) {
        var new_name = window.document.getElementById('comment_editor_name_' + comment_id).value;
        var new_url = window.document.getElementById('comment_editor_url_' + comment_id).value;
    }
    var new_comment = window.document.getElementById('comment_editor_message_' + comment_id).value;

    var url = "request.comment.action.php?action=save&comment=" + encodeURIComponent(comment_id) +
        "&body=" + encodeURIComponent(new_comment);
    if (!is_member) {
        url += "&name="+encodeURIComponent(new_name);
        url += "&url="+encodeURIComponent(new_url);
    }

    comment_identifier = comment_id;
    if (!is_member) {
        window.document.getElementById('comment_anchor_' + comment_identifier).href = new_url;
        window.document.getElementById('comment_anchor_' + comment_identifier).innerHTML = new_name;
    }
    window.document.getElementById('comment_body_' + comment_identifier).innerHTML = new_comment.replace(/\n/gi, "<br />");

    sendHTTPRequest(url, handleCommentSaveReply);
}

