﻿String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var BlueEnum = function() { };
BlueEnum.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 blueenum = new BlueEnum();
