﻿var WindowTranspBg = Class.create({
    initialize: function(bgColour, opacity, content, options) {    
        this.content = $(content);
        this.bgBody = $(document.createElement("div"));
        
        this.bgBody.setStyle({position: 'absolute', top: '0px', left: '0px', width: (document.documentElement['clientWidth'] - 20) + 'px', height: document.documentElement['scrollHeight'] + 'px'});
        
        bgColour = bgColour || 'White';
        this.opacity = opacity || 0.75;
        this.bgBody.setStyle({backgroundColor: bgColour, zIndex: GetNextHighestZindex(document.body)});
        this.bgBody.addClassName('windowTranspBg');
        this.bgBody.setOpacity(0);
                      
        document.body.appendChild(this.bgBody);
        if (this.content) {
            this.content.setStyle({position: 'absolute'});
            document.body.appendChild(this.content);
        }
        
        // hide all flash objects
        var embeds = document.getElementsByTagName('embed');
        for(i = 0; i < embeds.length; i++) {
            $(embeds[i]).hide();
        }
        
        var bg = this.bgBody;
        var cnt = this.content;
        
        Event.observe(window, 'resize' , function() {RepositionNotifWindowContent(bg)});
        Event.observe(window, 'scroll' , function() {RepositionNotifWindowContent(bg)});
        Event.observe(window, 'resize' , function() {RepositionNotifWindowContent(cnt)});
        Event.observe(window, 'scroll' , function() {RepositionNotifWindowContent(cnt)});
        
        this.positionContent();
        this.content.hide();
        
        Effect.Appear(this.bgBody, {duration:0.5, from:0.0, to: this.opacity});
        Effect.Appear(this.content, {duration:0.5, from: 0.0, to: 1.0, afterFinish: function() {
            if (options && typeof(options.afterFinish) != 'undefined') options.afterFinish();
            }
        });    
    },
    positionContent: function() {
        if(this.content) this.content.setStyle({zIndex: GetNextHighestZindex(document.body), top: document.documentElement['clientHeight']/2 - this.content.offsetHeight/2 + document.documentElement['scrollTop'] + 'px', left: document.documentElement['clientWidth']/2 - this.content.offsetWidth/2 + 'px'});    
    },
    repositionElements: function(content) {
        if (content) content.setStyle({top: document.documentElement['clientHeight']/2 - content.offsetHeight/2 + document.documentElement['scrollTop'] + 'px', left: document.documentElement['clientWidth']/2 - content.offsetWidth/2 + 'px'});    
    },
    remove: function(options) {     
        var bg = this.bgBody;
        var cnt = this.content;
        
        Effect.Fade(this.bgBody, {duration:0.5, from: this.opacity, to: 0.0, afterFinish: function() {
            document.body.removeChild(bg);
            }
        });
        Effect.Fade(this.content, {duration:0.5, from: this.opacity, to: 0.0, afterFinish: function() {
            document.body.removeChild(cnt);
            if (options && typeof(options.afterFinish) != 'undefined') options.afterFinish();            
            }
        });
        
        // show all flash objects back again
        var embeds = document.getElementsByTagName('embed');
        for(i = 0; i < embeds.length; i++) {
            $(embeds[i]).show();
        }
    }    
});

function RepositionNotifWindowContent(content) {
    if (content) content.setStyle({top: document.documentElement['clientHeight']/2 - content.offsetHeight/2 + document.documentElement['scrollTop'] + 'px', left: document.documentElement['clientWidth']/2 - content.offsetWidth/2 + 'px'});    
}

function GetNextHighestZindex(obj){
   var highestIndex = 2000;
   var currentIndex = 0;
   var elArray = Array();
   if(obj){ elArray = obj.getElementsByTagName('*'); }else{ elArray = document.getElementsByTagName('*'); }
   for(var i=0; i < elArray.length; i++){
      if (elArray[i].currentStyle){
         currentIndex = parseFloat(elArray[i].currentStyle['zIndex']);
      }else if(window.getComputedStyle){
         currentIndex = parseFloat(document.defaultView.getComputedStyle(elArray[i],null).getPropertyValue('z-index'));
      }
      if(!isNaN(currentIndex) && currentIndex > highestIndex){ highestIndex = currentIndex; }
   }
   return(highestIndex+1);
}