﻿_WindowManager = function() {
    this._Tabs = [];
    this._Windows = [];
    this._Index = 1000;
    this._onCloseEvents = [];
    this._onApplyEvents = [];
    this._onOKEvents = [];
    this._onCancelEvents = [];
    this._onFullscreenEvents = [];
    this._onRestoreEvents = [];
    this._onMinimizeEvents = [];
    this._onYesEvents = [];
    this._onNoEvents = [];
};

_WindowManager.prototype =
{
    get_Tabs: function() { return this._Tabs; },
    get_Windows: function() { return this._Windows; },
    set_Windows: function(value) { this._Windows = value; },

    initialize: function() {
    },
    dispose: function() {
    },
    Focus: function(id) {
        if (this._Windows.length < 1) return;

        // Get window to focus
        var _win = this.GetWindow(id);

        // Remove focusing window
        this.RemoveWindow(id);

        // Add the focusing window to the end of the window collection
        this.AddWindow(_win);

        // Re-index all windows
        this._reindexWindows();
    },
    _reindexWindows: function() {
        // Re-index all windows
        var _ind = this._Index;
        for (var i = 0; i < this._Windows.length; i++) {
            this._Windows[i].set_Index(_ind);

            // Set new index
            var _el = this._Windows[i].get_element();
            if (_el) {
                _el.style.zIndex = _ind;
            }

            // Save new index
            this._Windows[i]._Settings.Save(this._Windows[i], this._Windows[i]._CookieName);

            // Inrement index counter
            // +2 leave empty index for disabled display
            _ind += 2;
        }
    },
    GetWindow: function(id) {
        for (var i = 0; i < this._Windows.length; i++) {
            var _win = this._Windows[i];

            if (_win.get_ControlID() == id) {
                return _win;
            }
        }

        return null;
    },
    AddWindow: function(window) {
        for (var i = 0; i < this._Windows.length; i++) {
            var win = this._Windows[i];

            // Replace existing window
            if (win.get_ControlID() == window.get_ControlID()) {
                this._Windows[i] = window;
                return;
            }
        }
        this._Windows.push(window);
    },
    RemoveWindow: function(windowid) {
        for (var i = 0; i < this._Windows.length; i++) {
            // Get window that need to be removed
            if (this._Windows[i].get_ControlID() == windowid) {
                this._Windows.remove(i, i);
            }
        }

        // Re-index all windows
        this._reindexWindows();
    },
    ReloadWindow: function(id) {
        var _window = this.GetWindow(id);
        if (_window) {
            _window.reload();
        }
    },
    AddTab: function(tid, cid) {
        this._Tabs.push({ id: tid, controlid: cid, control: $(cid) });
    },

    Minimize: function(id, fsb, rsb) {
        var _win = this.GetWindow(id);
        PageManager.EnableDisplay();

        for (var i = 0; i < this._onMinimizeEvents.length; i++) {
            this._onMinimizeEvents[i]();
        }

        return _win.Minimize(fsb, rsb);
    },
    Restore: function(id, fsb, rsb) {
        var _win = this.GetWindow(id);

        for (var i = 0; i < this._onRestoreEvents.length; i++) {
            this._onRestoreEvents[i]();
        }

        return _win.Restore(fsb, rsb);
    },
    Fullscreen: function(id, fsb, rsb) {
        var _win = this.GetWindow(id);

        for (var i = 0; i < this._onFullscreenEvents.length; i++) {
            this._onFullscreenEvents[i]();
        }

        return _win.Fullscreen(fsb, rsb);
    },
    Close: function(id) {
        var _win = this.GetWindow(id);
        PageManager.EnableDisplay();

        for (var i = 0; i < this._onCloseEvents.length; i++) {
            this._onCloseEvents[i]();
        }

        if (_win) {
            return _win.Close();
        }
    },
    Apply: function(id) {
        for (var i = 0; i < this._onApplyEvents.length; i++) {
            this._onApplyEvents[i]();
        }
    },
    OK: function(id) {
        for (var i = 0; i < this._onOKEvents.length; i++) {
            this._onOKEvents[i]();
        }
    },
    Yes: function(id) {
        for (var i = 0; i < this._onYesEvents.length; i++) {
            this._onYesEvents[i]();
        }
    },
    No: function(id) {
        for (var i = 0; i < this._onNoEvents.length; i++) {
            this._onNoEvents[i]();
        }
    },
    Cancel: function(id) {
        for (var i = 0; i < this._onCancelEvents.length; i++) {
            this._onCancelEvents[i]();
        }
    },

    add_onCloseEvent: function(func) {
        this._onCloseEvents.push(func);
    },
    add_onApplyEvent: function(func) {
        this._onApplyEvents.push(func);
    },
    add_onOKEvent: function(func) {
        this._onOKEvents.push(func);
    },
    add_onCancelEvent: function(func) {
        this._onCancelEvents.push(func);
    },
    add_onFullscreenEvent: function(func) {
        this._onFullscreenEvents.push(func);
    },
    add_onRestoreEvent: function(func) {
        this._onRestoreEvents.push(func);
    },
    add_onMinimizeEvent: function(func) {
        this._onMinimizeEvents.push(func);
    },
    add_onYesEvent: function(func) {
        this._onYesEvents.push(func);
    },
    add_onNoEvent: function(func) {
        this._onNoEvents.push(func);
    }
};

var WindowManager = new _WindowManager();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();