(function($) {
    function SendMessage() {
        
    }
    
    SendMessage.prototype = {

        on_ready : function sendmsg_on_ready() {
            $("form#campionature").submit(function () {
                return false;
            });
            var me = this;
            $("form#campionature input[type=submit]").click(function() {
                me.on_click();
            });
        },
        
        on_click : function sendmsg_on_click() {
            // remove all error messages
            $(".resultmsg").remove();
            $(".failedcondition").each(function() { $(this).removeClass('failedcondition'); });

            var act = $("form#campionature").attr("action");
            var data = $("form#campionature").serialize();
            var me = this;
            var os = function(d,s,j) {
                me.on_success(d,s,j);
            };
            var of = function(j,s,e) {
                me.on_failure(j,s,e);
            };
            $.post(act, data, os, 'json').error(of);
        },

        error: function sendmsg_error(what, where) {
            this.result(what, where, 'error');
        },
        
        success: function sendmsg_success(what, where) {
            this.result(what, where, 'success');
        },
        
        result: function sendmsg_result(what, where, clazz) {
            where.css('position', 'relative');
            if (where.attr('type') != "submit")
                where.addClass('failedcondition');
            else
                where.addClass('operationfailed');
            var wx = where.position().left;
            var wy = where.position().top;
            var wh = where.height();

            var e = $(what);
            var ey = wy + wh + 4 + "px";
            var ex = wx + "px";

            e.addClass('resultmsg').addClass(clazz).css({
                left: ex,
                top: ey
            });
            e.insertAfter(where);

            return e;
        },

        on_success : function sendmsg_on_success(data, status, jxhr) {
            if (typeof(data) == "object") {
                if (data.success) {
                    //this.success("<p>Invio della mail avvenuto con successo</p>", $("form#campionature input[type=submit]"));
                    window.location = data.redirect;
                } else {
                    for (var k in data.errors) {
                        if (k != "success" && k != "generic") {
                            var where = $("form#campionature [name=" + k + "]");
                            var what = "<p>" + data.errors[k] + "</p>";
                            this.error(what, where);
                        }
                    }
                    this.error("<p>Errore durante l'invio del messaggio</p>", $("form#campionature input[type=submit]"));
                }
            }
        },

        on_failure : function sendmsg_on_failure(jxhr, e, ex) {
            this.error("<p>Impossibile inviare il messaggio, riprovare più tardi</p>", $("form#campionature input[type=submit]"));
        }
    };
    
    window.Promask = window.Promask || {};
    window.Promask.SendMessage = SendMessage;

    var sendmsg = new SendMessage();
    $(function() {
        sendmsg.on_ready();
    });

})(jQuery);

