/**
 * Scripts related to editing posts in place.
 * 
 * @author justin
 */

/*
function scrollToEditor(editorEl) {
    var pos = editorEl.viewportOffset(),
        dim = editorEl.getDimensions(),
        off = editorEl.cumulativeOffset(),
        vpd = document.viewport.getDimensions();
    if (pos.top < 0 || pos.top + dim.height > vpd.height) {
        window.scrollTo(0, off.top);
    }
}
*/

var util = {
    appendHiddenInput: function(form, name, value) {
        var hiddenInput = document.createElement("INPUT");
        hiddenInput.setAttribute("type", "hidden");
        hiddenInput.setAttribute("name", name);
        hiddenInput.setAttribute("value", value);
        return form.appendChild(hiddenInput);
    },
    unescapePlainTextWithLinks: function(val) {
        return val.replace(/<[bB][rR][^>]*>/g, "\n")
            .replace(/<\/?[aA][^>]*>/g, "")
            .unescapeHTML();
    }
};

var post = {
    toggleEdit: function(postId, visible) {
        if (visible) {
            $("postBody" + postId).hide();
            $("postEditForm" + postId).show();
        }
        else {
            $("postEditForm" + postId).hide();
            $("postBody" + postId).show();
        }
    },
    toggleReply: function(threadId, visible) {
        if (visible) {
            $("postReply" + threadId).show();
        }
        else {
            $("postReply" + threadId).hide();
        }
    },
    toggleTitleEdit: function(visible) {
        if (visible) {
            $("topicTitle").hide();
            $("topicTitleForm").show();
        }
        else {
            $("topicTitleForm").hide();
            $("topicTitle").show();
        }
    },
    page: function(topicId, pageNum) {
        return true;
    },
    select: function(topicId, postId) {
        return true;
    },
    save: function(formEl) {
        //go through form and disabled form input submit and buttons
        Form.getElements(formEl).each(function(elem) {
            type = elem.type.toLowerCase();
            if (elem.tagName.toLowerCase() == "input" && (type == "submit" || type == "button")) {
               elem.disabled = true;
            }
        });
    },
    saveTitle: function(formEl) {    
        util.appendHiddenInput($(formEl), "topic.title", $F("topicTitleInput"));    
        return true;    
    },
    // 'delete' is a JS keyword
    delete0 : function(postId) {
        $("postDeleteForm" + postId).submit();
    },
    block : function(postId) {
        // only comments can be blocked
        return false;
    }
};
