function add_comment_handler(event)
{
    var elem_id = event.target.id;
    elem_id = elem_id.replace(/add_comment_/i, '');
    elem_name = "#reply_form_"+elem_id;
    if (comment_form_active_id != elem_id || comment_form_active_action != 'add') {
        clear_comment_form_active(comment_form_active_id);
        comment_form_active_id = elem_id;
        comment_form_active_action = 'add';
        $(elem_name).html('<div id="comment_form_active" style="display:none;">'+$("#comment_form_container").html()+'</div>');
        $("#comment_form_active #parent_id").attr("value", elem_id);
        $("#comment_form_active #comment_submit").val(title_add_comment);
        $("#comment_form_active #result_processing #processing_text").html(processing_text_saving);
        $("#comment_form_active #save_comment").submit(function(){save_comment(save_comment_url);});
    }
    show_hide_comment_form();
    return false;
}

function edit_comment_handler(event)
{
    var elem_id = event.target.id;
    elem_id = elem_id.replace(/edit_comment_/i, '');
    elem_name = "#reply_form_"+elem_id;
    if (comment_form_active_id != elem_id || comment_form_active_action != 'edit') {
        clear_comment_form_active(comment_form_active_id);
        comment_form_active_id = elem_id;
        comment_form_active_action = 'edit';
        $(elem_name).html('<div id="comment_form_active" style="display:none;">'+$("#comment_form_container").html()+'</div>');
        $("#comment_form_active #comment_id").attr("value", elem_id);
        $("#comment_form_active #comment_title").val($("#item_"+elem_id+" h3").html());
        $("#comment_form_active #comment_text").val($("#item_"+elem_id+" .item_content").html());
        $("#comment_form_active #comment_submit").val(title_save_comment);
        $("#comment_form_active #result_processing #processing_text").html(processing_text_saving);
        $("#comment_form_active #save_comment").submit(function(){save_comment(save_comment_url);});
    }
    show_hide_comment_form();
    return false;
}

function delete_comment_handler(event)
{
    var elem_id = event.target.id;
    elem_id = elem_id.replace(/delete_comment_/i, '');
    elem_name = "#reply_form_"+elem_id;
    $("#item_"+elem_id).attr("class", "item_to_delete");
    if (confirm(message_delete_comment)) {
        clear_comment_form_active(comment_form_active_id);
        comment_form_active_id = elem_id;
        comment_form_active_action = 'delete';
        $(elem_name).html('<div id="comment_form_active">'+$("#comment_form_container").html()+'</div>');
        $("#comment_form_active #comment_form").html('');
        $("#comment_form_active #result_processing #processing_text").html(processing_text_deleting);
        delete_comment(delete_comment_url);
    } else {
        $("#item_"+elem_id).attr("class", "item");
    }
    return false;
}

function clear_comment_form_active(elem_id)
{
    $("#reply_form_"+elem_id).html('');
}

function show_hide_comment_form()
{
    if ($('#comment_form_active').is(':hidden')) {
        $('#comment_form_active').fadeIn("slow");
        location = '#addform';
    } else {
        $('#comment_form_active').fadeOut("slow");
    }
}

function save_comment(url) 
{
    $("#comment_form_active #comment_form").hide();
    $("#comment_form_active #result_processing").show();
    params = $("#comment_form_active #save_comment").serializeArray();
    ajax.request(url, params, save_comment__callback);
}

function save_comment__callback(data)
{
    $("#comment_form_active #result_processing").hide();
    if (data.validation_error) {
        $("#comment_form_active #action_validation_error").html(data.validation_error);
        $("#comment_form_active #action_validation_error").show();
        $("#comment_form_active #comment_form").show();
    } else {
        $("div[@class^=item_title]").attr("class", "item_title");
        if (data.error) {
            $("#comment_form_active").hide();
            alert(data.error);
            clear_comment_form_active(comment_form_active_id);
        } else {
            if (comment_form_active_action == 'add') {
                $("#comment_form_active").html(data.message);
                $("#comment_form_active").fadeOut(2500, function(){clear_comment_form_active(data.parent_id);});
            } else {
                clear_comment_form_active(data.comment_id);
            }
            $("#comments_container #parent_" + data.parent_id).html(data.comments);
        }
        comment_form_active_id = '';
        comment_form_active_action = '';
        $("a[@id^=add_comment]").click(add_comment_handler);
        $("a[@id^=edit_comment]").click(edit_comment_handler);
        $("a[@id^=delete_comment]").click(delete_comment_handler);
        $("img[@id^=inc_rate_comment]").click(rate_comment_handler);
        $("img[@id^=dec_rate_comment]").click(rate_comment_handler);
    }
}

function delete_comment(url) 
{
    $("#comment_form_active").show();
    $("#comment_form_active #result_processing").show();
    params = {
        comment_id: comment_form_active_id
    };
    ajax.request(url, params, delete_comment__callback);
}

function delete_comment__callback(data)
{
    $("#comment_form_active #result_processing").hide();
    $("#item_"+comment_form_active_id).attr("class", "item");
    if (data.error) {
        clear_comment_form_active(comment_form_active_id);
        alert(data.error);
    } else {
        $("#comments_container #parent_"+data.parent_id).html(data.comments);
    }
    comment_form_active_id = '';
    comment_form_active_action = '';
    $("a[@id^=add_comment]").click(add_comment_handler);
    $("a[@id^=edit_comment]").click(edit_comment_handler);
    $("a[@id^=delete_comment]").click(delete_comment_handler);
    $("img[@id^=inc_rate_comment]").click(rate_comment_handler);
    $("img[@id^=dec_rate_comment]").click(rate_comment_handler);
}


function rate_comment_handler(event)
{
    var elem_id = event.target.id;
    if (elem_id.search(/inc_rate_comment_/i) != -1) {
        comment_rate_id = elem_id.replace(/inc_rate_comment_/i, '');
        comment_rate_action = 'inc';
    } else if (elem_id.search(/dec_rate_comment_/i) != -1) {
        comment_rate_id = elem_id.replace(/dec_rate_comment_/i, '');
        comment_rate_action = 'dec';
    } else {
        return false;
    }
    rate_comment(rate_comment_url);
    return false;
}

function rate_comment(url) 
{
    $("#rate_comment_"+comment_rate_id).hide();
    params = {
        comment_id: comment_rate_id,
        rate_action: comment_rate_action
    };
    ajax.request(url, params, rate_comment__callback);
}

function rate_comment__callback(data)
{
    if (data.error) {
        alert(data.error);
    } else {
        data.comment_rating += '';
        $("#rate_comment_"+comment_rate_id).html(data.comment_rating);
        $("#rate_comment_"+comment_rate_id).show();
    }
}

