/// class ProspectDirector.Web.UI.WindowDOM

/**
@constructor
@class WindowDOM
@namespace Web.UI
@param options {Object}
**/
ProspectDirector.Web.UI.WindowDOM = Class.extend({

    //================================
    // FIELDS
    //================================

    options: null,
    _window: null,
    _windowContainer: null,
    _windowContent: null,
    _windowTitle: null,
    _windowTitleBar: null,

    //================================
    // CONSTRUCTOR
    //================================

    init: function (options) {
        this.options = options;
    },

    //================================
    // PROPERTIES
    //================================

    get_id: function () {
        return this.options.id;
    },

    get_window: function () {
        return this._window;
    },

    get_container: function () {
        return this._windowContainer;
    },

    get_content: function () {
        return this._windowContent;
    },

    get_titleBar: function () {
        return this._windowTitleBar;
    },

    get_title: function () {
        return this._windowTitle;
    },

    get_showLoadingMessage: function () {
        return this.options.showLoadingMessage;
    },

    get_buttonRestore: function () {
        return this.get_container().find(".k-i-restore");
    },

    get_buttonMaximize: function () {
        return this.get_container().find(".k-i-maximize");
    },

    get_buttonClose: function () {
        return this.get_container().find(".k-i-close");
    },

    //================================
    // EVENT HANDLERS
    //================================

    _windowResize: function (e) {

        var fn = this.options.events["resize"];

        e = e || {};

        e.content = this.dimensions();

        if ($.isFunction(fn)) {
            $.proxy(fn, this)(e);
        }

        this.setWindowButtonIcons();

    },

    _windowRefresh: function (e) {

        var fn = this.options.events["refresh"];

        if ($.isFunction(fn)) {
            fn();
        }

        this.get_window().center();
        this.get_window().trigger("resize");

        this._windowResize();

    },

    _windowClose: function (e) {
        //window["focusLock"].off(this.get_content().get(0));
        this.destroy();
    },

    _browserResize: function (e) {

        var isMaximized = this.get_window().options.isMaximized;

        if (!isMaximized) {
            this.get_window().center();
        }
        
    },

    //================================
    // PUBLIC METHODS
    //================================

    activate: function () {

        var that = this,
            fnRefresh = $.proxy(this._windowRefresh, this),
            $node = $(this.render()).appendTo("body DIV[data-role=windows]"),
            $nodeParent = null,
            options = $.extend(this.options.config, {
                animation: false,
                resize: $.proxy(this._windowResize, this),
                refresh: fnRefresh,
                visible: false,
                close: $.proxy(this.options.close, this),
                maximize: () => {
                    this.setWindowButtonIcons();
                }
            });

        this._window = $node.kendoWindow(options).data("kendoWindow");

        $nodeParent = $node.parent();
        $nodeParent.attr("ID", this.get_id());
        $nodeParent.addClass("-jadejs-window");

        this._windowContainer = $nodeParent;
        this._windowContent = $node;
        this._windowTitle = $nodeParent.find(".k-window-titlebar > .k-window-title");
        this._windowTitleBar = $nodeParent.find(".k-window-titlebar");

        this.get_window().center();
        this.get_window().open();

        this.setWindowButtonIcons();

        $(window).resize($.proxy(this._browserResize, this));
        
        setTimeout(function () {
            that.get_content().find("input, textarea, button, select, a").each(function (index, el) {
                var node = $(el);
                if (node.attr("tabIndex") == null) {
                    node.attr("tabIndex", 9999);
                }
            });
        }, 1000);
        
    },

    destroy: function (options) {
        this.get_window().destroy();
    },

    dimensions: function () {

        return {
            height: this.get_container() - this.get_titleBar().outerHeight,
            width: this.get_container().outerWidth()
        };

    },

    maximize: function () {
        this.get_window().maximize();
    },

    render: function () {

        var $result = $("<div></div>");
        var $loader = $("<table class=\"-pd-window-loading\"><tr><td>Loading...</td></tr></table>");

        if (this.options.config.markup) {
            $result.append(this.options.config.markup);
        }
        else {
            if (this.get_showLoadingMessage()) {
                $result.append($loader);
            }
        }

        return $result[0].outerHTML;

    },

    setOptions: function (options) {
        this.get_window().setOptions(options);
    },

    resize: function () {
        this.get_window().resize();
    },

    setWindowButtonIcons: function () {
        if (!this.get_buttonClose().hasClass("icofont-close")) {
            this.get_buttonClose().addClass("icofont-close");
            this.get_buttonClose().empty();
        }
        if (!this.get_buttonMaximize().hasClass("icofont-maximize")) {
            this.get_buttonMaximize().addClass("icofont-maximize");
            this.get_buttonMaximize().empty();
        }
        if (!this.get_buttonRestore().hasClass("icofont-restore")) {
            this.get_buttonRestore().addClass("icofont-restore");
            this.get_buttonRestore().empty();
        }
    }

});
