function getPictureSize(img){
  var container = img.parentNode;
  var h_d = img.height/container.offsetHeight;
  var w_d = img.width/container.offsetWidth;
  var d = (h_d <= w_d)?h_d:w_d;
  
  img.parentNode.removeAttribute('href');
  img.parentNode.removeAttribute('target');
  
  var img_ = new Image();
  img_.src = img.src;
  
  container.setAttribute('h_d', h_d);
  container.setAttribute('w_d', w_d);
  
  container.setAttribute('w', img_.width);
  container.setAttribute('h', img_.height);
  container.setAttribute('d', d);
  container.setAttribute('src_', img.src);
  if(h_d <= w_d){
    container.setAttribute('p', 'height');
    try{img.style.height = parseInt(img.height/d) + "px"}catch(err){};
  }else{
    container.setAttribute('p', 'width');
    try{img.style.width = parseInt(img.width/d) + "px"}catch(err){};
  }
  img.style.visibility = "visible";
}

function seekPictures(ii){
  //alert(ii + "\n\n" + (ii >= 1 && ii <= zoomableImagesArray.length));
  if(ii >= 1 && ii <= zoomableImagesArray.length) getPictureRealSize(zoomableImagesArray[ii-1]);
  else if(ii > zoomableImagesArray.length) getPictureRealSize(zoomableImagesArray[0]);
}

function visHideShirm(key){
  var shirm = document.getElementById('realSizeContainerShirm');
  if(!shirm) return;
  if(key){
    shirm.style.height = document.body.getElementsByTagName('table')[0].offsetHeight + 'px';
    shirm.style.display = 'block';
  }else{
    shirm.style.height = '100%';
    shirm.style.display = 'none';
  }
}

function getPictureRealSize(obj){
  var w = obj.parentNode.getAttribute('w');
  var h = obj.parentNode.getAttribute('h');
  var d = obj.parentNode.getAttribute('d');
  var p = obj.parentNode.getAttribute('p');
  var src = obj.parentNode.getAttribute('src_');
  var ii = parseInt(obj.parentNode.getAttribute('ii'));
  //alert(ii);
  var timeout = null;//alert(obj + '\n\n' + w + '\n\n' +  h + '\n\n' +  d);
  if(w && h && d){
    if(!document.getElementById('realSizeContainer')){
      var div = document.createElement('div');
      div.id = 'realSizeContainerShirm';
      div.className = "container-shirm";
      document.body.appendChild(div);
      var div = document.createElement('div');
      div.id = 'realSizeContainer';
      div.className = "real-size-container";
      document.body.appendChild(div);
    }
    visHideShirm(true);
    var div = document.getElementById('realSizeContainer');
    var winWidth = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0; 
	  var winHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
    div.style.top = Math.round((parseInt(winHeight) - parseInt(h))/2) + parseInt(document.documentElement.scrollTop || document.body.scrollTop) + "px";
    div.style.left = Math.round((parseInt(winWidth) - parseInt(w))/2) + parseInt(document.documentElement.scrollLeft || document.body.scrollLeft) + "px";
    var innerStr = '<a class="close-cross" href="javascript:closeRealSizeContainer()">X</a><div class="paginator">';
    if(ii > 1) innerStr += '<a href="javascript:seekPictures(' + (ii - 1) + ')">&lt;&lt;</a>&nbsp;';
    innerStr +=  '(' + ii + '/' + zoomableImagesArray.length + ')';
    if(ii < zoomableImagesArray.length) innerStr += '&nbsp;<a href="javascript:seekPictures(' + (ii + 1) + ')">&gt;&gt;</a>';
    innerStr +=  '</div><a href="javascript:seekPictures(' + (ii + 1) + ')"><img id="realSizeImg" src="' + src + '" style="' + p + ':' + parseInt(((h <= w)?h:w)/d) + 'px; visibility: hidden;" onload="this.style.visibility=\'visible\'" border="0"></a>';
    div.innerHTML = innerStr;
    div.style.visibility = 'visible';
    clearInterval(timeout);
    timeout = null;
    var img = document.getElementById('realSizeImg');
    var param = parseInt((p == 'height')?img.offsetHeight:img.offsetWidth);
    if(d > 1){
      /*timeout = setInterval(function(){
        if(img.offsetWidth >= w || img.offsetHeight >= h){
          img.style.width = w + "px";
          clearInterval(timeout);
          timeout = null;
        }else{
          param = param + parseInt(d*10);
          img.style[p] = param + "px";
        }
      }, 10);*/
      img.style.width = w + "px";
      img.style.height = h + "px";
    }
  }
}

function closeRealSizeContainer(){
  div = document.getElementById('realSizeContainer')
  div.style.visibility = 'hidden';
  div.innerHTML = '';
  visHideShirm(false);
}

var timerID = null;
var afterLoadTime = 100;
var afterLoadIteration = 0;
var afterLoadRun = false;

function afterLoad(className){
  if(afterLoadRun) return;
  var body = document.getElementsByTagName('body');
  if(body && body[0]){
    clearInterval(timerID);
    timerID = null;
    getZoomableImages(className);
  }/*else if(afterLoadIteration >= 10){
    clearInterval(timerID);
    timerID = null;
  }else{
    afterLoadTime = 2*afterLoadTime;
    afterLoadIteration++;
  }*/
}

var zoomableImagesArray = new Array();

function getZoomableImages(){
  afterLoadRun = true;
  var img = document.getElementsByTagName('img');
  for(var i = 0; i < img.length; i++){
    if(img[i].className.indexOf('zoomable') != -1){
      //img[i].parentNode.removeAttribute('href');
      //img[i].parentNode.removeAttribute('target');
      if(img[i].complete) getPictureSize(img[i]);
      //else img[i].setAttribute('onload', 'getPictureSize(this)');
      else img[i].onload = function(){new getPictureSize(this)}
      img[i].onclick = function(){new getPictureRealSize(this)}
      try{
        img[i].setAttribute('alt', document.getElementById('message-zoom-in').innerHTML);
        img[i].setAttribute('title', document.getElementById('message-zoom-in').innerHTML);
      }catch(err){}
      zoomableImagesArray[zoomableImagesArray.length] = img[i];
      img[i].parentNode.setAttribute('ii', zoomableImagesArray.length);
    }
  }
}

timerID = setInterval("afterLoad()", afterLoadTime);
if(window.addEventListener) window.addEventListener('load', afterLoad, false);
else window.attachEvent('onload', afterLoad, false);

