var Overlay = new Class({
    Implements:[Options, Events],
    
    options:{
        overlayId:"overlay_container",
        transparency:.25,
        backgroundColor:"#000",
        heightOffset:0,
        widthOffset:0,
        onCreated:$empty,
        onRemove:$empty, 
        onRemoved:$empty,
        onShown:$empty
    },
    
    overlay:null,
    isOpened:false,
    
    initialize:function(options)
    {
        if($chk(options)){ this.setOptions(options); }
    },
    
    show:function()
    {
        this.overlay = this.__add_overlay();
        this.register_keypress(this.overlay);
        return this.overlay;
    },
    
    hide:function()
    {
        if(this.isOpened && $chk(this.overlay))
        {
            this.__remove_overlay(this.overlay);
        }
    },
    
    register_keypress:function(overlay)
    {
        var self = this;
        $(window).addEvent("keyup", function(e){ self.__keypress.call(self, overlay, e); });
    },
    
    __keypress:function(sender, e)
    {
        if(this.isOpened)
        {
            if(!$chk(e)){ return; }
            if(e.key == "esc")
            {
                this.__remove_overlay(sender);
                return false;
            }
        }
    },
    
    __document_height:function()
    {
        var db = document.body;
        var dde = document.documentElement;

        return Math.max(db.scrollHeight, dde.scrollHeight, db.offsetHeight, dde.offsetHeight, db.clientHeight, dde.clientHeight);
    },
    
    __document_width:function()
    {
        var db = document.body;
        var dde = document.documentElement;

        return Math.max(db.scrollWidth, dde.scrollWidth, db.offsetWidth, dde.offsetWidth, db.clientWidth, dde.clientWidth);
    },
    
    __add_overlay:function()
    {
        var self = this;
        
        var viewport = Window.getViewport();
        var scroll = Window.getScroll();
        
        var overlay = new Element("div", {"id":this.options.overlayId});
        
        var hgt = 0;
        var wdt = 0;
        var shadow_container = this.container;
        
        if($chk(shadow_container))
        {
            var coords = shadow_container.getCoordinates();
            if($chk(coords))
            {
                hgt = coords.height;
            }
        }
        
        wdt = Math.min(viewport.width, this.__document_width());
        hgt = Math.max(hgt, this.__document_height());
        
        overlay.setStyles({
            position:"absolute",
            padding:0,
            margin:0,
            background:this.options.backgroundColor,
            width:wdt + this.options.widthOffset,
            height:hgt + this.options.heightOffset,
            top:0,
            left:0,
            opacity:0,
            "z-index":10000
            });
            
        $(document.body).grab(overlay, "top");
        
        overlay.addEvent('click', function(){ 
            self.__remove_overlay(overlay);
        });
        
        this.fireEvent("created", overlay);
        
        this.isOpened = true;
        
        setTimeout(function(){
            overlay.fade(1 - self.options.transparency);
            setTimeout(function(){ self.fireEvent("shown", overlay);}, 500);
            }, 500);
        return overlay;
    },
    
    __remove_overlay:function(elem)
    {
        var self = this;
        
        var overlay = $(elem);
        if(!$chk(overlay)){ return; }
        
        self.fireEvent("remove");
        overlay.fade(0);
        
        this.isOpened = false;
        
        setTimeout(function(){ overlay.destroy(); self.fireEvent("removed"); }, 1000);
    }
});

