var CURR_EDIT;

function get_post(id, callback) {
    show_loading($("#reply_header"), "append");
    server_query({
        url: '/forums/get-post/'+id+'/',
        dataType: "json",
        type: "GET",
        
        success: function(obj) {
            hide_loading($("#reply_header"));
            callback(obj, id);
        },
        error: function() {
            hide_loading($("#reply_header"));
        }
    });
}

function edit_post(id) {
    get_post(id, function(obj) {
        CURR_EDIT = id;
        populate_box(obj.message);
    });
}

function quote_post(id) {
    get_post(id, append_quote);
}

function append_quote(obj) {
    if ( !obj )
        return;
    var selector = $((CURR_EDIT?"#edit_box":"#id_raw_message"));
    var curr_val = selector.val();
    if ( curr_val.length != 0 )
        curr_val += "\n";
    selector.val(curr_val+"[quote="+obj.poster+"]"+obj.message+"[/quote]\n");
}

function send_edit() {
    var body = $("#edit_box").val().replace(/^\s+|\s+$/g, "");
    if ( body.length == 0 ) {
        alert("Please input a post body.");
        return;
    }
    
    server_query({
        url: '/forums/edit-post/'+CURR_EDIT+'/',
        dataType: "json",
        type: "POST",
        data: {
            'message': body
        },
        success: function(obj) {
            $("#post_body_"+CURR_EDIT).html(obj);
            hide_edit();
        },
        error: function() { }
    })
}

function hide_edit() {
    CURR_EDIT = null;
    $("#edit_tbl").hide();
    $("#reply_div").show();
    $("#reply_header").html("Quick Reply");
}

function populate_box(obj) {
    var edit_tbl = $("#edit_tbl");
    if ( edit_tbl.length == 0 ) {
        var str = '<table id="edit_tbl"><tr><th>Body:<div style="visibility: hidden">Character:</div>' +
            '</th><td><textarea id="edit_box"></textarea></td><tr><td>&nbsp;</td>' +
            '<td><input type="button" onclick="send_edit();" value="Edit"/> ' +
            '<input type="button" onclick="hide_edit();" value="Cancel"/></td></table>';
        $("#post_box").after(str);
        edit_tbl = $("#edit_tbl");
    }
    
    edit_tbl.show();
    $("#reply_div").hide();
    $("#edit_box").val(obj);
    $("#reply_header").html("Edit Post");
}

function delete_post(id) {
    var elem = $("#post_body_"+id);
    var data = {};
    if ( elem.find("div.forum_deleted_post").length > 0 ) {
        data['undelete'] = true;
    }
    show_loading(elem, "prepend").css("float", "right");
    server_query({
        url: '/forums/delete-post/'+id+'/',
        data: data,
        dataType: "json",
        success: function(obj) {
            hide_loading(elem);
            if ( obj == "undelete" ) {
                elem.find("div.forum_deleted_post").remove();
            } else {
                elem.prepend("<div class='forum_deleted_post>This "+obj+
                             " has been deleted. You may still read it below.</div>");
            }
        },
        error: function() {
            hide_loading(elem);
        }
    })
}

function show_loading(obj, where) {
    var elem = obj.find("img#loading_image");
    if ( elem.length == 0 ) {
        var elem = document.createElement("img");
        elem.src = "/media/loading.gif";
        elem.id = "loading_image";
        elem = $(elem);
    }
    elem.show();
    if ( where == 'prepend' )
        obj.prepend(elem.remove());
    else if ( where == 'append' )
        obj.append(elem.remove());
    else if ( where == 'before' )
        obj.before(elem.remove());
    else if ( where == 'after' )
        obj.after(elem.remove());
    return elem;
}

function hide_loading(obj) {
    obj.find("#loading_image").hide();
}

$.ajaxSetup( {
    dataType: "json",
    type: "POST"
} );

function server_query(data) {
	$.ajax(data);
}

