var Overlay = function() {
    this.el = null;
    this.__init__();
};
update(Overlay.prototype, {

    id: "overlay",
    
    effect: "appear",
    duration: 1.0,
    opacity: 1.0,
    color: "black",
    zindex: 1,

    __init__: function() {
        this.el = this._createOverlayElement(this.id);
        this.parentNode = document.body;
    },

    _createOverlayElement: function(id) {
        var el = DIV({"id": id});
        setElementDimensions(el, new Dimensions(100, 100), "%");
        setElementPosition(el, new Coordinates(0, 0));
        setStyle(el, {
            "background": this.color,
            "position": "fixed",
            "z-index": this.zindex,
            // This is for IE to simulate fixed position
            "_position": "absolute",
            "_top": "expression(eval(document.body.scrollTop))"
        });
        setOpacity(el, 0);
        return el;
    },

    show: function(duration) {
        var d = new Deferred();
        this.bodyOverflow = getStyle(document.body, "overflow");
        setStyle(document.body, {
            "overflow": "hidden"
        });
        if (/(msie|MSIE 6)/.test(navigator.userAgent)) {
            forEach(document.body.childNodes, function(el) {
                fade(el);
            });
            this.parentNode = document.body;
        }
        appendChildNodes(this.parentNode, this.el);
        appear(this.el, {
            "to": this.opacity,
            "duration": duration || this.duration,
            "afterFinish": bind(function() {
                d.callback(true);
                signal(this.id, "shown");
            }, this)
        });
        return d;
    },
    
    hide: function(duration) {
        var d = new Deferred();
        fade(this.el, {
            "duration": duration || this.duration,
            afterFinish: bind(function(event) {
                removeElement(this.el);
                setStyle(document.body, {
                    "overflow": this.bodyOverflow
                });
                // This needed with Safari
                this._makeScrollbarsAppearAgain();
                d.callback(true);
                signal(this.id, "hidden");
            }, this)
        });
        return d;
    },
    
    _makeScrollbarsAppearAgain: function() {
        var curPos = getViewportPosition();
        var altPos = clone(curPos);
        altPos.x += (curPos.x > 0) ? -1 : +1;
        altPos.y += (curPos.y > 0) ? -1 : +1;
        scrollTo(altPos.x, altPos.y);
        scrollTo(curPos.x, curPos.y);
    }
});

