﻿
JsBase.Controls.ModalWindow = function (overlay, container, contentContainer, closeButton) {

    var _this = this;

    var _visible = false;
    this.Visible = function (value) {
        if (typeof (value) == "undefined") {
            return _visible;
        }
        else {
            _visible = value;
        }
    }

    var _height;
    this.Height = function (value) {
        if (typeof (value) == "undefined") {
            return _height;
        }
        else {
            _height = value;
        }
    }

    var _width;
    this.Width = function (value) {
        if (typeof (value) == "undefined") {
            return _width;
        }
        else {
            _width = value;
        }
    }

    var CalculateOffsetTop = function (height) {
        return (($(window).height() - height) / 2);
    }

    var CalculateOffsetLeft = function (width) {
        return (($(window).width() - width) / 2);
    }

    this.Init = function () {
        closeButton.click(function () { _this.HideModal() });
        delete this.Init;
    }

    this.ShowModal = function (width, height, content, showCloseButton, closeOnOverlay) {

        this.DisposeContent();

        if (width == null) {
            width = content.width() - 14;
        }

        if (height == null) {
            height = content.height() - 20;
        }

        container.css(
            {
                "display": "block",
                "width": width + "px",
                "height": height + 20 + "px",
                "top": CalculateOffsetTop(height + 20),
                "left": CalculateOffsetLeft(width)
            }
        );

        contentContainer.css(
            {
                "width": width,
                "height": height
            }
        );

        contentContainer.html(content);

        overlay.css(
            {
                "display": "block",
                "height": $(document).height()
            }
        );

        if (content instanceof jQuery)
            content.show();

        if ($.browser.msie && $.browser.version.substr(0, 1) < 7)
            $(document).find("select").css("visibility", "hidden");

        if (closeOnOverlay)
            overlay.click(function () { _this.HideModal() });

        if (showCloseButton)
            closeButton.show();
        else
            closeButton.hide();

        $(window).resize(function () { _this.Reposition() });

        this.Visible(true);
        this.Width(width);
        this.Height(height);
    }

    this.HideModal = function () {

        this.DisposeContent();

        container.css("display", "none");
        overlay.css("display", "none");

        if ($.browser.msie && $.browser.version.substr(0, 1) < 7)
            $(document).find("select").css("visibility", "visible");

        this.Visible(false);

        $(window).unbind("resize");
        overlay.unbind("click");
    }

    this.Reposition = function () {

        container.css(
                {
                    "top": CalculateOffsetTop(this.Height() + 20),
                    "left": CalculateOffsetLeft(this.Width())
                }
            );

        overlay.css(
            {
                "height": $(document).height()
            }
        );
    }

    this.Resize = function (width, height) {

        container.css(
            {
                "display": "block",
                "width": width + "px",
                "height": height + 20 + "px",
                "top": CalculateOffsetTop(height + 20),
                "left": CalculateOffsetLeft(width)
            }
        );

        contentContainer.css(
            {
                "width": width,
                "height": height
            }
        );

        this.Width(width);
        this.Height(height);
    }

    this.DisposeContent = function () {
        $("body").append(contentContainer.children().hide());
    }

    this.Init();
}
