var jscape = jscape || {};
jscape.settings = jscape.settings || {};

/** @class */
jscape.settings.ServerSettingsApplication = jscape.Application.extend(
    /** @lends {jscape.settings.ServerSettingsApplication.prototype} */
{
    /** @override */
    onReady: function() {
        this._super();
        this.view.show(this);
    },

    /** @override */
    _createView: function() {
        this._super();
        this.view = new jscape.settings.ServerSettingsView();
    },

    /** @override */
    _launch: function (managementService) {
        var _super = this._super;
        $.when(managementService.getServerAddresses(), managementService.getServerKeys())
                .done($.proxy(function (addresses, serverKeys) { _super.call(this, managementService, addresses, serverKeys); }, this))
                .fail($.proxy(function () { _super.call(this, managementService, [], []); }, this));
    }
});

/** @class */
jscape.settings.ServerSettingsView = jscape.DashboardView.extend(
    /** @lends {jscape.settings.ServerSettingsView.prototype} */
{
    /** @constructor */
    initialize: function() {
        this._super($("#server-settings[role=tablist]"));

        this.modules.tabs({
            fit: true,
            border: true,
            tabPosition: "left",
            headerWidth: 200,
            onSelect: $.proxy(this._onSelect$Tab, this)
        });
        this.modules.find("div[role=tablist]")
                .filter(function () { return $(this).parents("div.dialog").length == 0; })
                .each($.proxy(function (_, tablist) {
                          $(tablist).tabs({border: false, onSelect: $.proxy(this._onSelect$Tab, this)});
                      }, this));

        this.modules.find(">div.tabs-header>div.tabs-wrap")
                .hover(
                function () { $(this).css({overflowY: "auto", overflowX: "hidden"}); },
                function () { $(this).css({overflowY: "", overflowX: ""}); }
        );
    },

    /** @override */
    show: function() {
        this._super.apply(this, arguments);
        this.modules.tabs("unselect", 0).tabs("select", 0);
    }
});

/** @class */
jscape.settings.SettingsModule = jscape.ApplicationModule.extend(
    /** @lends {jscape.settings.SettingsModule.prototype} */
{
    serverAddresses: null,
    serverKeys: null,

    /** @override */
    onCreate: function(managementService, serverAddresses, serverKeys) {
        this.serverAddresses = serverAddresses;
        this.serverKeys = serverKeys;

        this._super(managementService);
    },

    _fireApplySuccess: function () {
        $(document).triggerHandler("broadcast", [window.R.string.APPLICATION_SUCCESS_APPLY]);
    }
});