var maxHeight = 550;
var maxListHeight = 120;

function init() {
    if (document.getElementById("singleimage")) 
{
single();
initClickForImg();
}
    /*else if (document.getElementById("pictures")) list();*/

}

function list() {
    imageDivs = document.getElementsByName("listimage");
    for (i=0; i < imageDivs.length; i++) {
        currentHeight = imageDivs[i].offsetHeight;
        currentWidth = imageDivs[i].offsetWidth;

        if (currentHeight > maxListHeight) {
            ratio = maxListHeight / currentHeight;
            imageDivs[i].style.width = (currentWidth*ratio) + 'px';
            imageDivs[i].style.height = (currentHeight*ratio) + 'px';
            imageDivs[i].firstChild.style.height = '100%';
            imageDivs[i].firstChild.style.width = '100%';
        }
    }
}

function single() {
    var imageDiv = document.getElementById("singleimage");

    if (imageDiv) {
        currentHeight = imageDiv.offsetHeight;
        currentWidth  = imageDiv.offsetWidth;

        if (currentHeight > maxHeight) {
            ratio = maxHeight / currentHeight;
            imageDiv.style.width = (currentWidth*ratio) + 'px';
            imageDiv.style.height = (currentHeight*ratio) + 'px';

        }
    }
}

window.onload = init;
//window.addEventListener("DOMContentLoaded", initDom, false);
// for img click as go to next using
  var imgDiv;
        var nextA;
        var lastScrollTop = parseUrlQuery().scrollTop;
          
function initDom()
{

if (lastScrollTop) document.body.scrollTop = lastScrollTop;

}

 function initClickForImg()
       {
      if (lastScrollTop) document.body.scrollTop = lastScrollTop;
 
       imgDiv  = document.getElementById("singleimage");
       nextA = document.getElementsByClassName ('next');
      // confirm(nextA[0].href);

       imgDiv.onclick = gotoNextImg;

       
        }

function gotoNextImg()
{
if  (nextA[0].href)
 //alert(document.body.scrollTop);

 //location.href =  nextA[0].href+'&scrollTop='+document.body.scrollTop;


var newPath = nextA[0].href+'&scrollTop='+getBodyScrollTop();
//alert(newPath );
 location.href = newPath;

    return false;
}
// возвращает значение перёменной из урла по её имени

function parseUrlQuery() {
    var data = {};
    if(location.search) {
        var pair = (location.search.substr(1)).split('&');
        for(var i = 0; i < pair.length; i ++) {
            var param = pair[i].split('=');
            data[param[0]] = param[1];
        }
    }
    return data;
}
function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
