ProspectDirector.Types.Core.ViewportSplitter = Class.extend({

    init: function (options) {

        this.options = $.extend({
            orientation: "vertical",
            resize: null
        }, options);

        this._splitter = $(this.options.selector);

        this.get_splitter().css({
            height: "100%"
        });

        this.get_splitter().addClass("viewport-splitter");
        this.get_splitter().data("viewportSplitter", this);

        this.get_splitter().kendoSplitter({
            orientation: this.options.orientation,
            panes: this._init_panes(),
            resize: this.options.resize
        });

        this.get_splitter().find(".form-tabstrip").next().css({
            height: "0px",
            borderWidth: "0px 0px 1px 0px",
            borderBottomColor: "#DDDDDD"
        });

        this._init_resizeHandler();

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    get_splitter: function () {
        return this._splitter;
    },

    //---------------------------
    //--- INITIALIZERS
    //---------------------------

    _init_panes: function () {

        var panes = [];

        this.get_splitter().children("div").each(function (idx, el) {

            var pane = $(el),
                data = $(el).data();

            switch (true) {

                case pane.hasClass("form-header"):

                    data = $.extend({
                        size: "40px",
                        resizable: false,
                        scrollable: false
                    }, data);

                    break;

                case pane.hasClass("form-toolbar"):

                    data = $.extend({
                        size: "25px",
                        resizable: false,
                        scrollable: false
                    }, data);

                    break;

                case pane.hasClass("form-tabstrip"):

                    data = $.extend({
                        size: "35px",
                        resizable: false,
                        scrollable: false
                    }, data);

                    break;

            }

            panes[panes.length] = data;

        });

        return panes;

    },

    _init_delay: function () {

        ProspectDirector.Types.Core.ViewportSplitter.delay = ProspectDirector.Types.Core.ViewportSplitter.delay ?
                                                    ProspectDirector.Types.Core.ViewportSplitter.delay : 10;

        this.options.delay = ProspectDirector.Types.Core.ViewportSplitter.delay;

    },

    _init_resizeHandler: function () {

        var windowResizeHandler = null,
            splitterElement = this.get_splitter().length > 0 ? this.get_splitter() : null,
            splitterControl = splitterElement ? splitterElement.data("kendoSplitter") : null,
            that = this;

        this._init_delay();

        windowResizeHandler = function () {

            if (splitterControl && splitterControl.element) {

                setTimeout(function () {
                    splitterControl.resize();
                }, that.options.delay);

            }
            else {
                $(window).off("resize", windowResizeHandler);
            }

        };

        $(window).on("resize", windowResizeHandler);

        setTimeout(function () {
            if (splitterControl) {
                splitterControl.resize();
            }
        }, 100);

    },

    //---------------------------
    //--- METHODS
    //---------------------------

    destroy: function () {

        var splitter = this.get_splitter().data("kendoSplitter");

        if (splitter) {
            this.get_splitter().removeData("viewportSplitter");
            splitter.destroy();
        }

    }

});
