﻿var Rhino = function() { };
Rhino.prototype.Comment = function(selector) {
    var root = this;
    var formPanel = $(selector + " .commentform div.form");
    var progressPanel = $(selector + " .commentform div.progress");
    var commentList = $(selector + " .commentlist");
    var button = $(selector + " .commentform .form input[type='button']");
    var form = $(selector + " .commentform div.form form");

    this.isValidForm = function() {
        var errors = [];
        form.find(".required", this).each(function() {
            if (this.value.length == 0)
                errors.push("<li>A " + $(this).attr("title") + " is required.");
        });
        if ($("#recaptcha_response_field").val().length == 0)
            errors.push("<li>A Validation Code is required.");

        if (errors.length == 0)
            return true;
        else {
            progressPanel.slideDown("fast", function() {
                progressPanel.html("<ul class=\"errors\">" + errors.join("</li>") + "</li></ul>");
            });
            return false;
        }
    };
    this.postForm = function() {
        formPanel.slideUp("normal", function() {
            progressPanel.html("<h3>Your comment is being processed...</h3>").slideDown("fast", function() {
                //Post comment
                $.post(form.attr("action"), root.serializeForm(form), function(data) {
                    //Display result
                    if (data) {
                        if (data.substring(0, 5) == "error") {
                            progressPanel.html("<h3>" + data + "</h3>");
                        }
                        else {
                            progressPanel.html("<h3>Your comment has been submitted. Thank you!</h3>");
                            if (commentList.find(".comment").length == 0)
                                commentList.html("");
                            commentList.append(data);
                        }
                    } else {
                        progressPanel.html("<h3>Error occurred</h3>");
                    }

                });

            });
        });
    };
    this.serializeForm = function(form) {
        var inputs = [];
        form.find(":input", this).each(function() {
            inputs.push(this.name + '=' + escape(this.value));
        });
        inputs.push("challenge" + "=" + Recaptcha.get_challenge());
        inputs.push("response" + "=" + Recaptcha.get_response());
        return inputs.join("&");
    };
    this.remove = function(element) {
        var anchor = $(element);
        var id = anchor.attr("rel");
        $.post("/comments/post", "action=remove&commentid=" + id, function(data) {
            anchor.parents("div.comment").remove();
        });
    };
    this.approve = function(element) {
        var anchor = $(element);
        var id = anchor.attr("rel");
        var hash = anchor.attr("title");
        $.post("/comments/post", "action=moderate&commentid=" + id + "&hash=" + hash, function(data) {
            anchor.parents("div.comment").replaceWith(data);
        });
    };
    this.init = function() {
        button.click(function(e) {
            e.preventDefault();
            if (root.isValidForm())
                root.postForm();
        });
        commentList.find("a.remove").click(function(e) {
            e.preventDefault();
            root.remove(this);
        });
        commentList.find("a.moderate").click(function(e) {
            e.preventDefault();
            root.approve(this);
        });
    };
};
var rhino = new Rhino();