Type.registerNamespace('ATODesign.SitoMastro.WebApp');

ATODesign.SitoMastro.WebApp.ModalPopup = function(popupElement, defaultBackgroundElement) {
    this._popupElement = popupElement;
    this._defaultBackgroundElement = defaultBackgroundElement;
    this._isVisible = false;
    this._mustHide = false;
    this._isShowing = false;
    ATODesign.SitoMastro.WebApp.ModalPopup.initializeBase(this, [popupElement, defaultBackgroundElement]);
}

ATODesign.SitoMastro.WebApp.ModalPopup.prototype =
{
    initialize: function() {
        ATODesign.SitoMastro.WebApp.ModalPopup.callBaseMethod(this, 'initialize');
    },
    initializeDefaultBackgroundElement: function() {
        if (this._defaultBackgroundElement) {
            var clientWidth = $(window).width();
            var clientHeight = $(window).height();
            this._defaultBackgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth) + 'px';
            this._defaultBackgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight) + 'px';
        }
    },
    show: function() {
        if (this._defaultBackgroundElement &&
            (this.get_isVisible() == false)) {
            this.set_isShowing(true);
            this.initializeDefaultBackgroundElement();
            $("#" + this._defaultBackgroundElement.id).fadeIn("fast",
                                                              Function.createDelegate(this,
                                                                                      this._defaultBackgroundElementShowed_callback));
        }
    },
    _defaultBackgroundElementShowed_callback: function() {
        if (this._popupElement) {
            this.positionPopup();
            $("#" + this._popupElement.id).fadeIn("fast",
                                                  Function.createDelegate(this,
                                                                          this._popupElementShowed_callback));
        }
    },
    _popupElementShowed_callback: function() {
        this.set_isVisible(true);
        this.set_isShowing(false);

        if (this.get_mustHide() == true) {
            if (typeof sleep == 'function') {
                sleep(1000);
            }
            else {
                alert("'sleep' function is not defined.");
            }

            this.hide();
        }
        else {
            //$addHandler(this, "scroll", Function.createDelegate(this._defaultBackgroundElement, this._window_onscroll));
        }
    },
    positionPopup: function(x, y) {
        if (this._defaultBackgroundElement) {
            var windowWidth = $(window).width();
            var windowHeight = $(window).height();

            if (this._popupElement) {
                var popupHeight = $("#" + this._popupElement.id).height();
                var popupWidth = $("#" + this._popupElement.id).width();

                $("#" + this._popupElement.id).css({
                    "position": "absolute",
                    "top": windowHeight / 2 - popupHeight / 2 + $(window).scrollTop() + ((arguments.length == 2) ? y : 0),
                    "left": windowWidth / 2 - popupWidth / 2 + $(window).scrollLeft() + ((arguments.length == 1) ? x : 0)
                });
            }
        }
    },
    hide: function() {
        var pbc = $get('divProgressBarContainer');

        if (pbc) {
            pbc.style.display = 'none';
        }

        var pb = $get('divProgressBar');

        if (pb) {
            pb.style.width = '0';
        }

        if (this.get_isShowing() == false) {
            if (this._popupElement &&
                (this.get_isVisible() == true)) {
                $("#" + this._popupElement.id).fadeOut("fast", Function.createDelegate(this, this._popupElementHided_callback));
            }
        }
        else {
            this.set_mustHide(true);
        }
    },
    _popupElementHided_callback: function() {
        if (this._defaultBackgroundElement) {
            $("#" + this._defaultBackgroundElement.id).fadeOut("fast",
                                                               Function.createDelegate(this,
                                                                                       this._defaultBackgroundElementHided_callback));
        }
    },
    _defaultBackgroundElementHided_callback: function() {
        this.set_mustHide(false);
        this.set_isVisible(false);

//        var eventList = this.get_events();

//        if (eventList &&
//            (typeof eventList.getHandler == 'function')) {
//            var handler = eventList.getHandler('scroll');

//            if (handler) {
//                $removeHandler(this, "scroll", handler);
//            }
//        }
    },
//    _window_onscroll: function(e) {
//        this.positionPopup(window.pageXOffset, window.pageYOffset);
//    },
    _popupElement_callback: function() {
        $("#" + this._defaultBackgroundElement.id).fadeOut("fast");
    },
    get_isVisible: function() {
        return this._isVisible;
    },
    set_isVisible: function(value) {
        this._isVisible = value;
    },
    get_mustHide: function() {
        return this._mustHide;
    },
    set_mustHide: function(value) {
        this._mustHide = value;
    },
    get_isShowing: function() {
        return this._isShowing;
    },
    set_isShowing: function(value) {
        this._isShowing = value;
    },
    set_progressBarValue: function(value) {
        if ((value >= 0) && (value <= 100)) {
            var pb = $get('divProgressBar');
            var pbc = $get('divProgressBarContainer');

            if (pb) {
                pb.style.width = (value * 2).toString() + 'px';
            }

            if (pbc) {
                pbc.style.display = '';
            }
        }
    }
}

ATODesign.SitoMastro.WebApp.ModalPopup.registerClass('ATODesign.SitoMastro.WebApp.ModalPopup', Sys.Component);

if (typeof (Sys) != 'undefined') {
    Sys.Application.notifyScriptLoaded();
}
