(function () {

    /**
     * Форма обратной связи
     *
     * @param root
     * @param button
     *
     * @event   click
     *
     */
    PICSA.Feedback = function (root, button) {
        var me = this;
        //Observable(me);

        me.root=root;
        me.button = button;

        me.layer = $('<div class="body-cap-up">' +
                    '<em class="l png"/></em>' +
                    '<em class="r png"/></em>' +
                  '</div>' +
                  '<div id="body">' +
                   '<h1>Задать вопрос</h1>' +
                   '<div class="form">' +
                     '<div id="indicator"></div>' +
                     '<form action="/feedback" id="fb_form" method="post">' +
                       '<label>Имя</label>' +
                       '<input id="name" name="name" type="text" class="textinput" /><br />' +
                       '<label>Email</label>' +
                       '<input id="email" name="email" type="text" class="textinput" /><br />' +
                       '<label>Вопрос</label>' +
                       '<textarea id="content" name="content" type="text" class="textinput" />' +
                       '<div class="button-div">' +
                         '<input name="commit" type="submit" class="button" value="Отправить" />' +
                       '</div>' +
                     '</form>' +
                     '<div id="message"></div>' +
                     '<div id="error"></div>' +
                   '</div>' +
                 '</div>'+
                 '<div class="body-cap">' +
                    '<em class="l png"/></em>' +
                    '<em class="r png"/></em>' +
                  '</div>');
        $(me.root).append(me.layer);

        $("form", me.root).submit(function(){
            var form = this;

            $.ajax({
                beforeSend: function(request){me.showIndicator();me.disableButton();},
                data: $.param($(form).serializeArray()),
                dataType: "script",
                success: function(request){
                    me.hideIndicator();
                    var data = eval('(' + request + ')');
                    if(data.success){
                        me.showMessage('Благодарим за отзыв!');
                        me.enableButton();
                        me.reset();
                    }else{
                        me.showError(data.errors);
                    }
                },
                type: "post",
                url: $(form).attr('action')
            });
            return false;
        });

        $(me.button)
            .click(function(){
                if(!me.visible()){
                    me.show();
                }else{
                    me.hide();
                }
                return false;
            });
        $(document).click(function(e){
            var parents = $(e.target).parents('.open');
            if(me.visible() && !$(e.target).hasClass('.open') && parents.length == 0){
                me.hide();
            }
        });
        
        me.setSize();
        $(window).resize(function(){
            me.setSize();
        });

        return me;
    }

    PICSA.Feedback.prototype = {
        setSize: function(){
            $(this.root).css({'top': $(window).height()-240});
            $(this.button).css({'top': $(window).height()-35});
        },
        show: function(){
            $(this.root).addClass('open');
            $(this.button).addClass('pressed');
        },
        hide: function(){
            $(this.root).removeClass('open');
            $(this.button).removeClass('pressed');
        },
        visible: function(){
            return $(this.root).hasClass('open');
        },
        showIndicator: function(){
            $('#indicator', this.root).show();
        },
        hideIndicator: function(){
            $('#indicator', this.root).hide();
        },
        showError: function(errors){
            var me = this;

            me.hideError();
            me.hideMessage();
            me.enableButton();
            $('#error', this.root).html(errors).show();
        },
        showMessage: function(message){
            var me = this;

            me.hideError();
            me.hideMessage();
            $('#message', this.root).html(message).slideDown();
        },
        hideError: function(){
            $('#error', this.root).hide();
        },
        hideMessage: function(){
            $('#message', this.root).hide();
        },
        disableButton: function(){
            $('.button', this.root).attr('disabled', true);
        },
        enableButton: function(){
            $('.button', this.root).attr('disabled', false);
        },
        hideButton: function(){
            $('.button', this.root).hide();
        },
        reset: function(){
            $("form", this.root).clearForm();
        }
    };

})();
