

var AuthDialog = function (settings) {
    $.extend(this, settings || {});
    this.d = $("#auth_dialog");
    this.d.dialog({ width:400, autoOpen:false, modal:true, resizable:false});
    var self = this;
    $(".register-button").click(self.onRegister);
    $(".login-button").click(self.onLogin);
};

AuthDialog.instance = null;

AuthDialog.get = function (settings) {
    if (AuthDialog.instance == null) {
        AuthDialog.instance = new AuthDialog(settings);
    }
    return AuthDialog.instance;
};

AuthDialog.prototype = {
    d: null,
    dashboard_url:null,
    show : function (msg) {
        this.d.dialog("open");
        if (msg) {
            this.show_msg(msg);
        }
    },
    show_msg: function(msg) {
        var message = this.d.find(".message");
        if (msg) {
            message.text(msg);
            message.addClass("ui-state-highlight pam");
            message.parent().addClass("ui-widget-content")
        } else {
            message.text('');
            message.removeClass("ui-state-highlight pam");
            message.parent().removeClass("ui-widget-content");
        }

    },
    login : function (msg) {
        this.show(msg);
        $(".login-view").show();
        $(".register-view").hide();
        $(".login-view .username").focus();
        this.d.dialog("option", "title", "Zaloguj się");

    },
    register : function (msg) {
        this.show(msg);
        $(".login-view").hide();
        $(".register-view").show();
        $(".register-view .username").focus();
        this.d.dialog("option", "title", "Zarejestruj się");
    },
    onRegister: function () {
        var form = $(".register-view form");
        form.submit();
//        $.post(form.attr("action"), form.serialize(), function (data) {
//            if (data.success) {
//                AuthDialog.get().hide();
//                this.document.href = ""
//            }
//        }, "json")
    },
    onLogin: function () {
        $(".login-view form").submit();
    },
    hide: function () {
        this.d.dialog("close");
    }
};
