/* Helferfunktionen
------------------------------------------------ */
function getWindowHeight() {
  var windowHeight = 0;
  if (typeof(window.innerHeight) == 'number') {
    windowHeight = window.innerHeight;
  }
  else {
    if (document.documentElement && document.documentElement.clientHeight) {
      windowHeight = document.documentElement.clientHeight;
    }
    else {
      if (document.body && document.body.clientHeight) {
        windowHeight = document.body.clientHeight;
      }
    }
  }
  return windowHeight;
}

function getWindowWidth() {
  var windowWidth = 0;
  if (typeof(window.innerWidth) == 'number') {
    windowWidth = window.innerWidth;
  }
  else {
    if (document.documentElement && document.documentElement.clientWidth) {
      windowWidth = document.documentElement.clientWidth;
    }
    else {
      if (document.body && document.body.clientWidth) {
        windowWidth = document.body.clientWidth;
      }
    }
  }
  return windowWidth;
}


/* Rechnen
------------------------------------------------ */
function resizeImage() {
  //---------------------
  // Benutzerdefinierte Randbreite
  var marginX = 10;
  var marginY = 12;
  //---------------------
  
  
  if (document.getElementById) {
    // Fenstergrösse
    var windowHeight = getWindowHeight();
    var windowWidth = getWindowWidth();
    
    // Bild finden
    var image = document.getElementById('resizedImage');

    // Physische Grösse des Bildes aus den HTML-Attributen auslesen
    // Diese können auch Strings sein ("100px"), darum zu Integer konvertieren
    
    if (image) {
      imgWidth = parseInt(image.getAttribute('width'));
      imgHeight = parseInt(image.getAttribute('height'));

      if (windowHeight > windowWidth * (imgHeight/imgWidth)) {
        image.style.height = windowHeight + 'px';
        image.style.width = windowHeight * (imgWidth/imgHeight) + 'px';
      } else {
        image.style.height = windowWidth * (imgHeight/imgWidth) + 'px';
        image.style.width = windowWidth + 'px';
      }
    
      // Bild zuschneiden
      var clipX = windowWidth - marginX;
      var clipY = windowHeight - marginY;
      image.style.clip = 'rect('+marginY+'px,'+clipX+'px,'+clipY+'px,'+marginX+'px)';
    }
  }
}

function setBodyHeight() {
  var windowHeight = getWindowHeight();
  document.body.style.height = windowHeight + "px";
}



/* Laden!
------------------------------------------------ */
window.onload = function() {
  resizeImage();
  setBodyHeight();
}
window.onresize = function() {
  resizeImage();
  setBodyHeight();
}
