/*
 * Javascript for comment annotations
 */

function comment_add(comment_id, hopper_id, item_id)
{
    comment_form('reply', comment_id, hopper_id, item_id, '', comment_id == 0 ? 'Add comment' : 'Reply');
}


function comment_edit(comment_id, hopper_id, item_id)
{
    var body_src = document.getElementById('comment_val_'+comment_id);
    comment_form('edit', comment_id, hopper_id, item_id, body_src.innerHTML, 'Save comment');
}


function comment_form(command, comment_id, hopper_id, item_id, comment_text, submit_text)
{
    var form_div;
    if (comment_id != 0)
        form_div = document.getElementById('c_reply_'+comment_id);
    else
        form_div = document.getElementById('hopper_'+hopper_id+'_item_'+item_id);

    if (comment_id != 0 || hopper_id != 0)
        document.getElementById('viewable_to').style.display = 'none';
    else
        document.getElementById('viewable_to').style.display = 'block';

    var form = document.getElementById('comment_form');

    form.parentNode.removeChild(form);
    form_div.appendChild(form);

    form['command'].value = command;
    form['item_id'].value = item_id;
    form['comment_id'].value = comment_id;
    form['hopper_id'].value = hopper_id;
    form['comment'].value = comment_text;

    document.getElementById('c_submit').value = submit_text;

    return true;
}


function comment_cancel()
{
    var hider = document.getElementById('hopper_hider');
    var form = document.getElementById('comment_form');
    form.parentNode.removeChild(form);
    hider.appendChild(form);
}


function comment_check_group()
{
    document.getElementById('viewable_group').checked = true;
}


function comment_delete(comment_id, hopper_id, item_id)
{
    comment_cancel();

    if (!confirm("Are you sure you want to delete this comment?"))
        return false;

    var form_args = "comment_id=" + comment_id + "&item_id=" + item_id + "&hopper_id=" + hopper_id + "&command=delete";

    var c_base_url  = document.getElementById("base_url").value;

    var url = c_base_url  + "/hopper_comments/";

    var req = new AsynchRequest(url,
            //function on success - using response_text
            function(response_text)
            {
                var container;
                if (hopper_id != 0)
                    container = document.getElementById("hopper_comments");
                else
                    container = document.getElementById("comments");
                container.innerHTML = response_text;
            },

            //function on start of load
            function()
            {

            },

            //function on error
            function(error_state)
            {

            },

            "POST",
            form_args
    );

    req.send();
    return false;
}


function comment_submit()
{
    var form = document.getElementById('comment_form');
    var c_hopper_id = form['hopper_id'].value;
    var c_base_url  = document.getElementById("base_url").value;

    var form_args = build_form_args(form);

    var url = "/sinatra/compere/hopper_comments/";

    var req = new AsynchRequest(url,
            //function on success - using response_text
            function(response_text)
            {
                comment_cancel();
                var container;
                if (c_hopper_id != 0)
                {
                    // previously, just set container['hopper_comments'].innerHTML = response_text,
                    // but something changed, so now response_text == ''
                    // reload entire record page instead to ensure that everything is up-to-date
                    window.location.reload();
                }
                else
                {
                    container = document.getElementById("comments");
                    container.innerHTML = response_text;
                    form["comment"].value="";
                }
            },

            //function on start of load
            function()
            {

            },

            //function on error
            function(error_state)
            {

            },

            "POST",
            form_args
    );

    req.send();
    return false;
}


function build_form_args(form)
{
    var args = '';
    var qhash = new Hash({});
    for (var i = 0; i != form.elements.length; i++) {
        var input = form.elements[i];
        if (input.name == '')
            continue;
        if ((input.type == 'checkbox' || input.type == 'radio') && !input.checked)
            continue;
        qhash.set(input.name, input.value);
    }
    args = qhash.toQueryString();
    return args;
}

