
function PopUpBox(wWidth, wHeight) {
  if (typeof document.getElementById == 'undefined' || typeof document.createElement == 'undefined') { return; }

  //this.maxsize = parseFloat(maxsize);
  this.wWidth = parseFloat(wWidth);
  this.wHeight = parseFloat(wHeight);

  this.isie = typeof document.uniqueID != 'undefined';
  this.quirksmode = ( this.isie || typeof window.opera != 'undefined')
                 && (typeof document.compatMode == 'undefined' || document.compatMode != 'CSS1Compat');
  this.isgecko = navigator.product == 'Gecko' && navigator.vendor != 'Apple Computer, Inc.' && navigator.vendor != 'KDE';

  this.body = document.getElementsByTagName('body')[0];
  this.screen = null;
  this.window = null;
  this.links = [];
  var links = document.getElementsByTagName('a');
  for (var i=0; i<links.length; i++) {
    if ( links[i].className && links[i].className.indexOf('popupbox') != -1 && links[i].href ) {
      this.links[this.links.length] = links[i];
    }
  }
  var self = this;
  for(i=0; i<this.links.length; i++) {
    this.links[i].onclick = function () {
                              if (self.screen){
                                self.removeLoadingScreen();
                              } else {
                                self.showLoadingScreen();
                                self.showDivWindow(this.href);
                              }
                              return false;
                            };
  }
}


PopUpBox.prototype.showLoadingScreen = function () {
  if (this.screen) { return; }
  this.screen = document.createElement('div');
  this.screen.id = 'popupbox-loadingscreen';
  this.screen.appendChild(document.createTextNode('Loading ...'));
  this.body.appendChild(this.screen);
  var canvas = this.getCanvasSize();
  var scrolling = this.getScrollingOffset();
  this.screen.style.width = canvas.width + 'px';
  this.screen.style.paddingTop = (canvas.height / 2) + 'px';
  this.screen.style.height = (canvas.height / (this.quirksmode && this.isie ? 1 : 2)) + 'px';
  //this.screen.style.left = scrolling.left + 'px';
  //this.screen.style.top = scrolling.top + 'px';
  this.screen.style.left = '0px';
  this.screen.style.top = '0px';
  this.screen.style.position = 'fixed';
  var self = this;
  this.screen.onclick = function(){self.removeImageWindow(); self.removeLoadingScreen();};
  this.screen.style.visibility = 'visible';
};

PopUpBox.prototype.removeLoadingScreen = function () {
  if(!this.screen) { return; }
  this.screen.parentNode.removeChild(this.screen);
  this.screen = null;
};

PopUpBox.prototype.showDivWindow = function (link) {
  if (this.window) { return; }
  this.window = document.createElement('div');
  this.window.id = 'popupbox-mainwindow';
  var canvas = this.getCanvasSize();
  var scrolling = this.getScrollingOffset();
  //var divwidth = canvas.width * this.maxsize;
  var divwidth = this.wWidth;
  var divheight = canvas.height * this.wHeight;
  var contentheight = divheight - 50;
  this.window.setAttribute('style', 'width:' + divwidth + 'px;height:'+ divheight + 'px;');
  //this.window.setAttribute('style', 'width:' + divwidth + 'px;');

  var content = document.createElement('div');
  content.id = 'popupbox-contentwindow';
  content.setAttribute('style', 'height:'+ contentheight + 'px;');
  new Ajax.Updater('popupbox-contentwindow', link, { method: 'get', parameters : { asynchronous: false }, encoding : 'ISO-8859-1' });

  var controls = document.createElement('ul');
  var li = controls.appendChild(document.createElement('li'));
  li.className = 'close';
  var close = li.appendChild(document.createElement('a'));
  close.setAttribute('href', 'javascript:void("Ansicht schliessen")');
  close.setAttribute('title', 'Ansicht schliessen');
  close.appendChild(document.createTextNode('Schliessen'));


  this.window.appendChild(content);
  this.window.appendChild(controls);

  var self = this;
  close.onclick = function () {
                    self.removeImageWindow();
                    self.removeLoadingScreen();
                  };

  this.body.appendChild(this.window);

  if (this.isgecko || this.isie) {
    if (this.isgecko) {
      //controls.setAttribute('style', 'width:' + divwidth + 'px; float:left;');
    } else {
      controls.style.width = divwidth + 'px';
    }
    if (controls.offsetWidth < 200) {
      controls.style.width = '200px';
    }
  }
  //this.window.style.left = (((canvas.width - this.window.offsetWidth) / 2) + scrolling.left) + 'px';
  //this.window.style.top = (((canvas.height - this.window.offsetHeight) / 2) + scrolling.top) + 'px';
  this.window.style.left = (((canvas.width - this.window.offsetWidth) / 2)) + 'px';
  this.window.style.top = (((canvas.height - this.window.offsetHeight) / 2)) + 'px';
  this.window.style.position = 'fixed';
  this.screen.style.backgroundImage = 'none';
  this.screen.removeChild(this.screen.firstChild);
  this.window.style.visibility = 'visible';
};

PopUpBox.prototype.removeImageWindow = function () {
  if (!this.window) { return; }
  this.window.parentNode.removeChild(this.window);
  this.window = null;
};

PopUpBox.prototype.getCanvasSize = function () {
  if (typeof window.innerWidth != 'undefined') {
    var canvas = { 'width' : window.innerWidth, 'height' : window.innerHeight };
    if (this.isgecko) {
      if (this.body.offsetHeight > canvas.height) {
        canvas.width -= 17;
      } else {
        canvas.width -= 1;
      }
      canvas.height -= 1;
    }
  } else if (this.quirksmode) {
    canvas = {'width' : document.body.clientWidth,'height' : document.body.clientHeight};
  } else {
    canvas = {'width' : document.documentElement.offsetWidth,
              'height' : document.documentElement.offsetHeight};
  }
  return canvas;
};

PopUpBox.prototype.getScrollingOffset = function() {
  if (typeof window.pageXOffset != 'undefined') {
    var scrolling = {'left' : window.pageXOffset,'top' : window.pageYOffset};
  } else if (this.quirksmode) {
    scrolling = {'left' : document.body.scrollLeft,'top' : document.body.scrollTop};
  } else {
    scrolling = {'left' : document.documentElement.scrollLeft,'top' : document.documentElement.scrollTop};
  }
  return scrolling;
};

new PopUpBox('800','0.6');

