var Overlay = Class.create();

Overlay.prototype = {

  //
  // Initialisierung
  //
  initialize: function() { 
    //Optionen
    this.overlayDuration = 0.2;
    this.overlayTransparenz = 0.7;
    
    //Bauen
    var objBody = $$('body')[0];
		objBody.appendChild(Builder.node('div',{id:'overlay'}));
    objBody.appendChild(Builder.node('div',{id:'overlayInner'}));

    //Aktionen
    $('overlay').hide();
    $('overlay').observe('click', (function() { this.hideOverlay(); }).bind(this));

    //Elemente fassen
    var th = this;
    (function(){
      var ids = 
        'overlay overlayInner';   
      $w(ids).each(function(id){ th[id] = $(id); });
    }).defer();

  },
  
  //
  // Overlay einblenden
  //
  showOverlay: function () {
    var arrayPageSize = this.getPageSize();
    $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' })
    $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
    new Effect.Appear($('overlay'), { duration: this.overlayDuration, from: 0.0, to: this.overlayTransparenz });
  },
  
  //
  // Overlay ausblenden
  //  
  hideOverlay: function() {
    new Effect.Fade(this.overlay, { duration: this.overlayDuration });
    new Effect.Fade(this.overlayInner, { duration: this.overlayDuration });
    $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
        
  },
  
  //
  // HTML in Hilfscontainer Laden
  //
  inviteHTML: function(sourcebox) {
    $('overlayInner').innerHTML = $(sourcebox).innerHTML;
    $(sourcebox).remove();
    
    $('overlayClose').observe('click', (function() { 
      this.hideOverlay(); 
    }).bind(this));
  },
  
  //
  // Seitengröße ermitteln
  //
  getPageSize: function() {
    var xScroll, yScroll;		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;		
		if (self.innerHeight) {	
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
}

//Auslösen nach Laden der Seite
document.observe('dom:loaded', function () {
  if($('CrosssellingLayer')) {
    var over = new Overlay(); 
    over.inviteHTML('CrosssellingLayer');
    over.showOverlay();
  }
});
