var dPDF = Class.create({
    
    options: {
        opacity: 0.6,
        zIndex: 9999,
        holderCls: 'holder'
    },
    backgroundDiv: null,
    holderDiv: null,
    
    init: function(options) {
        if (this.holderDiv) return;
        
        Object.extend(this.options, options);
        
        if (Prototype.Browser.IE) {
            if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
                Prototype.BrowserFeatures['Version'] = new Number(RegExp.$1);
            }
        }
        if (Prototype.BrowserFeatures['Version'] < 7) {
            this.backgroundDiv = this.initBackgroundOverlay('absolute');
            this.holderDiv = this.initHolder('absolute');
        }
        else {
            this.backgroundDiv = this.initBackgroundOverlay('fixed');
            this.holderDiv = this.initHolder('fixed');
        }
        
        Element.extend(document);
        Element.extend(document.documentElement);
    },
    

    initHolder : function(position) {
        var holder = document.createElement('div');
        Element.extend(holder);
        holder.identify();

        holder.style.position = position;

        /*
        var opacity = this.option('opacity');
        if (opacity)
          holder.setOpacity(opacity);
        */

        holder.hide();
        document.body.appendChild(holder);
        
        return holder;
    },
    
    initBackgroundOverlay: function(position) {
        
        var background = document.createElement('div');
        Element.extend(background);
        
        background.style.position = position;
        background.style.left = 0;
        background.style.top = 0;
        background.style.backgroundColor = '#000';
        background.setOpacity(this.options.opacity);
    
        background.hide();
        document.body.appendChild(background);
    
        return background;
    },
    
    positionHolder: function(force_reposition) {
        var holder = this.holderDiv;
        
        var holder_dims = holder.getDimensions();
        
        // NOTE: hack for Opera 9.5 (needs testing with other versions)
        //       needed by Prototype 1.6.2 (may be fixed later)
        if (Prototype.Browser.Opera) {
            var viewport_dims = {
                width: window.innerWidth,
                height: window.innerHeight
            };
        }
        else {
            var viewport_dims = document.viewport.getDimensions();
        }
        
        var viewport_scroll_offsets = document.viewport.getScrollOffsets();
        
        var tmpLeft = (viewport_dims.width - holder_dims.width) / 2;
        var tmpTop = (viewport_dims.height - holder_dims.height) / 2;
        
        if (Prototype.BrowserFeatures['Version'] < 7) {
            tmpLeft += viewport_scroll_offsets.left;
            tmpTop += viewport_scroll_offsets.top;
        }
        holder.style.left = tmpLeft + 'px';
        holder.style.top = tmpTop + 'px';
    },

    positionAndSizeBackground : function() {
        var document_dims = document.documentElement.getDimensions();
        var document_offsets = document.documentElement.cumulativeOffset();
        var viewport_dims = document.viewport.getDimensions();
        var viewport_scroll_offsets = document.viewport.getScrollOffsets();
        
        var width = Math.max(document_dims.width + document_offsets.left, viewport_dims.width + viewport_scroll_offsets.left);
        var height = Math.max(document_dims.height + document_offsets.top, viewport_dims.height + viewport_scroll_offsets.top);
        
        this.backgroundDiv.style.width = width + 'px'; 
        this.backgroundDiv.style.height = height + 'px'; 
        this.backgroundDiv.style.left = 0;
        this.backgroundDiv.style.top = 0;
    },
    
    update: function(content) {
        this.holderDiv.className = this.options.holderCls;
        this.holderDiv.style.zIndex = this.options.zIndex;
        this.backgroundDiv.style.zIndex = this.options.zIndex - 1;
        
        if (typeof content == typeof "") {
            this.holderDiv.update(content);
        }
        else {
            /*
            this.content_element = content;
            this.content_parent_element = content.parentNode;
            
            this.holderDiv.update(null);
            this.holderDiv.appendChild(content);
            */
        }
        
        this.positionHolder(true);
        this.positionAndSizeBackground();
    },
    
    showMsg: function(content) {
        Overlay.instances.push(this);
        
        this.init();
        this.update(content);
        this.holderDiv.show();
        this.backgroundDiv.show();
    },
    
    hideMsg: function() {
        this.holderDiv.hide();
        this.backgroundDiv.hide();
    },
    
    createPdf: function(url, pdflink) {
        
        
        this.showMsg('<img src="typo3/gfx/spinner.gif" /> PDF wird erstellt. Bitte warten.');
        var dPdfObj = this;
        
        new Ajax.Request(url, {
            asynchronous:true, 
            evalScripts:false,
            method: 'get',
            parameters: 'URL='+escape(pdflink),
            onComplete:function(request){
                var link = new Element('a', {
                    'href': request.responseText,
                    'target': '_top',
                    'style': 'text-decoration: underline; font-weight: light-bold;'
                    
                }).update('PDF Herunterladen');
                
                dPdfObj.showMsg('<img src="typo3/gfx/icon_ok.gif" />');
                dPdfObj.holderDiv.appendChild(link);
				
				// MW 2010-04-30 Sofortiges öffnen der Datei
				dPdfObj.hideMsg();								
				location.href=request.responseText;
				// ENDE MW 2010-04-30 Sofortiges öffnen der Datei
				
                link.observe('click', function() {
                    dPdfObj.hideMsg();
                });
            }, 
            onLoading:function(request){
                
            }
        });
    }
});
