(function($) {
    $.fn.Question = function (settings) {

        var self = this;
        var showDialogButton = this.next();


        var search = new SearchBox({
                    el: this,
                    list: "questions",
                    url:settings.find_question_url,
                    additionalData: {"place":settings.place},
                    build_label: function (item) {
                        return item.text;
                    },
                    build_value: function (item) {
                        return item.text;
                    },
                    select: function(event, ui) {
                        if (ui.item) {
                            if (ui.item.action == "found") {
                                window.location =  ui.item.item.url;
                            }
                        }
                        return true;
                    },
                    not_found_handler: function (term) {
                        return [
                        ];
                    }
                });

        $(showDialogButton).click(function () {
            $.post(settings.ask_question_url, {"place":settings.place, "text": self.val()},
                    function (data) {
                        if (data.success) {
                            if (!data.is_authenticated) {
                                AuthDialog.get().register(msgs["registration_required"]);
                            } else {
                                window.location.reload();
                            }
                        } else {
                            show_errors(f, data);
                        }
                    },
                    "json");

            return false;
        });


        $.extend(this, {
                    onFocus: function () {
                        self.stopTime("check-if-can-submit");
                        self.everyTime(100, 'check-if-can-submit', function () {
                            if (self.val().length > 0) {
                                showDialogButton.button("enable");
                            } else {
                                showDialogButton.button("disable");
                            }
                        });
                    },
                    onBlur: function () {
                        self.stopTime("check-if-can-submit");
                    }
                });
        this.focus(this.onFocus);
        this.blur(this.onBlur);
    };

    var dialog_methods = {
        init : function(options) {
            var settings = {

            };

            return this.each(function() {

                if (options) {
                    $.extend(settings, options);
                }
                var self = $(this);

                var dialog = self.dialog({
                            autoOpen: false,
                            title:'Zadaj pytanie',
                            width:400,
                            maxHeight:600,
                            minHeight:300,
                            modal:true
                        });
                $("form", this).submit(function () {
                    var f = $(this);
                    $.post(settings.ask_question_url, $(this).serialize(),
                            function (data) {
                                if (data.success) {
                                    self.dialog.dialog("close");
                                    if (!data.is_authenticated) {
                                        AuthDialog.get().register(msgs["registration_required"]);
                                    } else {
                                        window.location.reload();
                                    }
                                } else {
                                    show_errors(f, data);
                                }
                            },
                            "json");

                    return false;
                });
            });
        },
        show : function() {
            this.dialog("open");
        },
        hide : function() {
            this.dialog("close");
        }
    };


    $.fn.QuestionDialog = function (method) {
        if (dialog_methods[method]) {
            return dialog_methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || ! method) {
            return dialog_methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.QuestionDialog');
        }
    };
})(jQuery);

