/*******************************************************************************
 * Sitepark.Common.Geometry
 *
 * object, containing PORTABLE functions for querying window
 * and document geometry
 *
 * getWindowX/Y(): return the position of the window on the screen
 * getViewportWidth/Height(): return the size of the browser viewport area
 * getDocumentWidth/Height(): return the size of the document.
 * getHorizontalScroll(): return the position of the horizontal scrollbar
 * getVerticalScroll(): return the position of the vertical scrollbar
 *
 * Note that there is no portable way to query the overall size of the
 * browser window, so there are no getWindowWidth/Height() functions.
 *
 * IMPORTANT: This object must be defined in the <body> of a document
 *            instead of the <head> of the document.
 ******************************************************************************/
if (typeof Sitepark == 'undefined') {
	Sitepark = {};
}
if (typeof Sitepark.Common == 'undefined') {
	Sitepark.Common = {};
}

Sitepark.Common.Geometry = {};

if (window.screenLeft) { // IE and others
  Sitepark.Common.Geometry.getWindowX = function() { return window.screenLeft; };
  Sitepark.Common.Geometry.getWindowY = function() { return window.screenTop; };
} else if (window.screenX) { // Firefox and others
  Sitepark.Common.Geometry.getWindowX = function() { return window.screenX; };
  Sitepark.Common.Geometry.getWindowY = function() { return window.screenY; };
}

if (window.innerWidth) { // All browsers but IE
  Sitepark.Common.Geometry.getViewportWidth = function() { return window.innerWidth; };
  Sitepark.Common.Geometry.getViewportHeight = function() { return window.innerHeight; };
  Sitepark.Common.Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
  Sitepark.Common.Geometry.getVerticalScroll = function() { return window.pageYOffset; };
} else if (document.documentElement && document.documentElement.clientWidth) { // These functions are for IE6 when there is a DOCTYPE
  Sitepark.Common.Geometry.getViewportWidth = function() { return document.documentElement.clientWidth; };
  Sitepark.Common.Geometry.getViewportHeight = function() { return document.documentElement.clientHeight; };
  Sitepark.Common.Geometry.getHorizontalScroll = function() { return document.documentElement.scrollLeft; };
  Sitepark.Common.Geometry.getVerticalScroll = function() { return document.documentElement.scrollTop; };
} else if (document.body.clientWidth) { // These are for IE4, IE5, and IE6 without a DOCTYPE
  Sitepark.Common.Geometry.getViewportWidth = function() { return document.body.clientWidth; };
  Sitepark.Common.Geometry.getViewportHeight = function() { return document.body.clientHeight; };
  Sitepark.Common.Geometry.getHorizontalScroll = function() { return document.body.scrollLeft; };
  Sitepark.Common.Geometry.getVerticalScroll = function() { return document.body.scrollTop; };
}

// These functions return the size of the document. They are not window
// related, but they are useful to have here anyway.
if (document.documentElement && document.documentElement.scrollWidth) {
  Sitepark.Common.Geometry.getDocumentWidth = function() { return document.documentElement.scrollWidth; };
  Sitepark.Common.Geometry.getDocumentHeight = function() { return document.documentElement.scrollHeight; };
} else if (document.body.scrollWidth) {
  Sitepark.Common.Geometry.getDocumentWidth = function() { return document.body.scrollWidth; };
  Sitepark.Common.Geometry.getDocumentHeight = function() { return document.body.scrollHeight; };
}
      
var iframeWidth   = 850;
var iframeHeight  = 630;
var borderWidths  = 0;
var borderHeights = 0;

function openPopup(url, may) {
	if(!may) {
    alert("Sie müssen eingeloggt sein, um mitspielen zu können.");
  } else {
    if(obj = document.getElementById('overlay')) {
      strIframe     = "<div class=\"background\"></div><iframe src=\"" + url + "\" scrolling=\"no\" width=\"850\" height=\"630\" frameborder=\"0\"></iframe>";
      obj.innerHTML = strIframe;
      iframeHeight  = Math.min(iframeHeight,Sitepark.Common.Geometry.getViewportHeight());
      iframeWidth   = Math.min(iframeWidth,Sitepark.Common.Geometry.getViewportWidth());
      intWidth  = Math.max(Sitepark.Common.Geometry.getViewportWidth(),document.getElementsByTagName('html')[0].offsetWidth,document.getElementsByTagName('body')[0].offsetWidth);
      intHeight = Math.max(Sitepark.Common.Geometry.getViewportHeight(),document.getElementsByTagName('html')[0].offsetHeight,document.getElementsByTagName('body')[0].offsetHeight);
      intXPos   = Math.round(0.5*(intWidth-iframeWidth));
      intYPos   = Math.round(0.5*(Sitepark.Common.Geometry.getViewportHeight()-iframeHeight)+Sitepark.Common.Geometry.getVerticalScroll());
      
      if (obj.lastChild != null) {
         obj.lastChild.style.width  = iframeWidth + "px";
         obj.lastChild.style.height = iframeHeight + "px";
         obj.lastChild.style.left   = Math.max(0,intXPos) + "px";
         obj.lastChild.style.top    = Math.max(0,intYPos) + "px";
      }
      obj.style.display = "block";
    }
  }
}

function resizePopup() {
   if (obj = document.getElementById('overlay')) {
      intWidth      = Math.max(Sitepark.Common.Geometry.getViewportWidth(),document.getElementsByTagName('html')[0].offsetWidth,document.getElementsByTagName('body')[0].offsetWidth);
      intHeight     = Math.max(Sitepark.Common.Geometry.getViewportHeight(),document.getElementsByTagName('html')[0].offsetHeight,document.getElementsByTagName('body')[0].offsetHeight);
      intXPos       = Math.round(0.5*(intWidth-iframeWidth));
      intYPos       = Math.round(0.5*(Sitepark.Common.Geometry.getViewportHeight()-iframeHeight)+Sitepark.Common.Geometry.getVerticalScroll());
      obj.style.height  = (intHeight) + "px";
      obj.style.width   = (intWidth)  + "px";
      
      if(obj.lastChild != null) {
         obj.lastChild.style.left   = Math.max(0,intXPos) + "px";
         if (document.all) {
         	 obj.lastChild.style.left   = Math.max(0,intXPos-12) + "px";
         }
         obj.lastChild.style.top    = Math.max(0,intYPos) + "px";
      }
   }

}

function scrollPopup() {}

function closePopup() {
   if (obj = document.getElementById('overlay')) {
      obj.style.display = "none";
      obj.innerHTML     = "";
   }
}

window.onresize = function() { resizePopup(); }
window.onscroll = function() { scrollPopup(); }

function changeView(which) {
   document.getElementById(which).style.display = (document.getElementById(which).style.display=='none') ? 'block' : 'none';
}
