var Geometry;
if(!Geometry) { Geometry = {}; }
else if(typeof Geometry != 'Object') { throw new Error('can not create module Geometry'); }

// определение размеров клиентской области окна и смещения по горизонтали/вертикали
function fillGeometry(){
  if (window.innerWidth) {
    Geometry.getViewportWidth = function(){
      return window.innerWidth;
    };
    Geometry.getViewportHeight = function(){
      return window.innerHeight;
    };
    Geometry.getHorizontalScroll = function(){
      return window.pageXOffset;
    };
    Geometry.getVerticalScroll = function(){
      return window.pageYOffset;
    };
  }
  else 
    if (document.documentElement && document.documentElement.clientWidth) {
      Geometry.getViewportWidth = function(){
        return document.documentElement.clientWidth;
      };
      Geometry.getViewportHeight = function(){
        return document.documentElement.clientHeight;
      };
      Geometry.getHorizontalScroll = function(){
        return document.documentElement.scrollLeft;
      };
      Geometry.getVerticalScroll = function(){
        return document.documentElement.scrollTop;
      };
    }
    else 
      if (document.body.clientWidth) {
        Geometry.getViewportWidth = function(){
          return document.body.clientWidth;
        };
        Geometry.getViewportHeight = function(){
          return document.body.clientHeight;
        };
        Geometry.getHorizontalScroll = function(){
          return document.body.scrollLeft;
        };
        Geometry.getVerticalScroll = function(){
          return document.body.scrollTop;
        };
      }
}

var ImageView = {};
ImageView.ISOPENED = false;

if(window.attachEvent) {
	window.attachEvent("onload", prepareImageView);
}
else if(window.addEventListener) {
	window.addEventListener("load", prepareImageView, false);
}

function prepareImageView()
{ 
  fillGeometry();
  
  var imgs = document.getElementsByTagName("img");
  for(var i=0; i<imgs.length; i++) {
    var img = imgs[i];
    if(img.className == "imageView") {
      img.onclick = function() {
        var src = "";
        if (this.parentNode.nodeName.toLowerCase() == "a") {
          src = this.parentNode.getAttribute("href");
          this.parentNode.onclick = function(){
            return false;
          };
        }
        else {
          src = this.src;
        }
        ImageView.Open(src);
      };
    }
  }
}

ImageView.Open = function(src) { 
  if(ImageView.ISOPENED) { ImageView.Close(); }
  
  try {
    var cont = document.createElement("div");
    var img = document.createElement("img");
    var span = document.createElement("span");
    var oi = new Image();
    oi.src = src;
    
    span.appendChild(document.createTextNode("Закрыть"));
    span.id = "imageViewClose";
    cont.id = "imageView";
    cont.style.position = "absolute";
    cont.style.visibility = "hidden";
    cont.style.left = "0";
    cont.appendChild(span);
    cont.appendChild(img);
    document.body.appendChild(cont);
    
    if(oi.width) { fillAndMoveImage(img, src); }
    else { oi.onload = function() { fillAndMoveImage(img, src); }; }
    
    span.onclick = function() { ImageView.Close(); };
    //ImageView.MoveTo();
  } catch(e) { alert(e.message); }
  
  ImageView.ISOPENED = true;
  return true;
};

ImageView.Close = function() {
  if(!ImageView.ISOPENED) { return false; }
  
  var cont = document.getElementById("imageView");
  if(cont) {
    try {
      cont.removeChild(cont.lastChild);
      document.body.removeChild(cont);
      ImageView.ISOPENED = false;
    } catch(e) { alert(e.message); }
    return true;
  }
  
  return false;
};

ImageView.MoveTo = function() { 
  var cont = document.getElementById("imageView");
  if(cont) { //alert("Try to move image to center\ncont width = " + cont.offsetWidth + "\ncont height = " + cont.offsetHeight);
    try {
      //cont.style.width = "400px";
      //cont.style.height = "300px";
      cont.style.top = (Geometry.getViewportHeight() / 2 - cont.offsetHeight / 2 + Geometry.getVerticalScroll()) + "px";
      cont.style.left = (3 * Geometry.getViewportWidth() / 8 - cont.offsetWidth / 2 + Geometry.getHorizontalScroll()) + "px";
      cont.style.visibility = "visible";
    } catch(e) { alert(e.message); }
    return true;
  }
  
  return false;
};

function fillAndMoveImage(img, src) 
{
  img.src = src;
  ImageView.MoveTo();
}


