/**
 * FullScreenImage(src)
 * Calculates the width & height of which to display an image
**/
function FullScreenImage(src) {
  if (!src) throw new Error("You must provide an image source.");
  
  function element(parent, tag, atts) {
    var el = document.createElement(tag);
    for (var at in atts) {

      if (at == 'style' && 'cssText' in el.style) {
      	// This block is a fix for shitty IE7, which does not support setAttribute on the "style" attribute.
      	el.style.cssText = atts[at];
      } else {
	  	el.setAttribute(at, atts[at]);
      }
    }
    if (parent) parent.appendChild(el);
    
    return el;
  }
  
  var realWidth, realHeight;
  var wrapper = element(document.body, "div", {
    "style": "z-index: 0; position:absolute; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden;"
  });
  
  wrapper.id = "full-screen-image";
  
  var img = element(wrapper, "img", {
    style: "visibility: hidden;"
  });
  
  function size() {
    var width =   wrapper.offsetWidth,
        height =  wrapper.offsetHeight,
        rx =      width / realWidth,
        ry =      height / realHeight,
        scale =   rx > ry ? rx : ry;
    
    img.width = parseInt(realWidth * scale, 10);
    img.height = parseInt(realHeight * scale, 10);
  }
  
  img.onload = function() {
    realWidth = img.offsetWidth;
    realHeight = img.offsetHeight;
    size();
    img.style.visibility = "visible";
  };
  
  window.onresize = function() {
    size();
  };
  
  img.src = src;
}

