function fade(id,from,to,step,delay,code)
{
  var elem = document.getElementById(id)
  var seed = Math.floor(Math.random()*(9999-1000+1))+1000;
  elem.style.opacity = from/100;
  if(elem.filters) { elem.style.filter = 'alpha(opacity=' + from + ')'; elem.filters.alpha.opacity = from; }
  if(from > to) step *= -1;
  function go()
  {
    elem.style.opacity = elem.style.opacity*1 + step/100;
    if(elem.filters) elem.filters.alpha.opacity = elem.filters.alpha.opacity*1 + step;
    if(step>0) { if((elem.filters && elem.filters.alpha.opacity >= to) || (elem.style.opacity*1 >= to/100)) { eval('window.clearInterval(timer'+seed+')'); if(code) eval(code); } }
    else { if((elem.filters && elem.filters.alpha.opacity <= to) || (elem.style.opacity*1 <= to/100)) { eval('window.clearInterval(timer'+seed+')'); if(code) eval(code); } }
  }
  if(!step*1 || !delay*1)
  {
    elem.style.opacity = to/100;
    if(elem.filters) elem.filters.alpha.opacity = to;
    if(code) eval(code);
  }
  else eval('timer'+seed+' = window.setInterval(go,delay);')
  return false;
}

function loadImage(curr,prev,next,text,pos,count)
{
  ge('dlkp_img').innerHTML = count > 1 ? '<img src="'+ urlencode(unescape(curr)) +'" style="cursor: pointer;" alt="" onmousedown="this.style.top = \'0px\'; this.style.left = \'0px\';" onmouseout="this.style.top = \'-2px\'; this.style.left = \'-1px\';" onmouseup="if(event.ctrlKey && !event.button) { showImage(\'' + unescape(prev).split("'").join("\\'") + '\'); } else if(event.shiftKey && !event.button) { window.open(\'' + urlencode(unescape(curr)) + '\') } else if(event.button<2) { showImage(\'' + unescape(next).split("'").join("\\'") + '\'); }" onload="imageLoaded(\'' + curr + '\',\'' + prev + '\',\'' + next + '\',\'' + text + '\',' + pos + ',' + count + ')" title="Щелчок мышью — следующее изображение, Ctrl+щелчок — предыдущее изображение, Shift+щелчок — открыть в новом окне">' : '<img src="'+ urlencode(unescape(curr)) +'" style="cursor: pointer;" alt="" onload="imageLoaded(\'' + curr + '\',\'' + prev + '\',\'' + next + '\',\'' + text + '\',' + pos + ',' + count + ')" onclick="if(event.shiftKey && !event.button) { window.open(\'' + urlencode(unescape(curr)) + '\') } else if(!event.button) hideImage();" title="Щелчок мышью — закрыть, Shift+щелчок — открыть в новом окне">';
  return false;
}

function showImage()
{
  var tags = document.getElementsByTagName('dfn');
  var images = new Array;
  var currImage = false; var prevImage = false; var nextImage = false; var tempImage = false;
  var nextFlag = 0; var imagesCount = 0; var currPos = 0; var firstFlag = 0;
  for(i in tags)
  {
    url = tags[i].innerHTML;
    text = tags[i].title;
    if(text == undefined) continue;
    imagesCount++;
    images[url] = text;
    if(url == arguments[0]) { firstFlag = 1; currImage = url; currPos = imagesCount; currText = text; prevImage = tempImage; nextFlag = 1; }
    else if(nextFlag) { nextImage = url; nextFlag = 0; }
    tempImage = url;
  }
  if(!currImage && arguments[0])
  {
    currImage = arguments[0];
    currText = arguments[1] || '';
    imagesCount = 0;
  }
  else
  {
    if(!firstFlag)
    {
      currImage = tags[0].innerHTML;
      currText = tags[0].title;
      currPos = 1;
      if(imagesCount > 1) { if(tags[1].innerHTML) nextImage = tags[1].innerHTML; }
    }
    if(!prevImage && imagesCount > 1) prevImage = tags[imagesCount-1].innerHTML;
    if(!nextImage && imagesCount > 1) nextImage = tags[0].innerHTML;
  }
  currImage = escape(currImage);
  if(prevImage) prevImage = escape(prevImage);
  if(nextImage) nextImage = escape(nextImage);
  if(currText) currText = escape(currText);
  if(!ge('dlkp_fog'))
  {
    var div = document.createElement('div');
    document.body.appendChild(div);
    div.innerHTML = '<div id="dlkp_fog" style="z-index: 10000; display: none; opacity: 0; filter: alpha(opacity=0); position: fixed; width: 100%; height: 100%; top: 0px; left: 0px; //position: absolute; top: expression(eval(document.body.scrollTop) + \'px\'); width: expression(eval(document.body.scrollLeft + document.body.clientWidth) + \'px\'); background-color: white;" onclick="hideImage();"><span id=dlkp_oldtitle style="display: none;"></span><div id=dlkp_notice style="font: 11px Trebuchet MS; color: black; text-align: center; margin-top: 5px;"></div></div>';
    div.innerHTML += '<div id="dlkp_img" style="z-index: 10002; display: none; visibility: hidden; opacity: 0; filter: alpha(opacity=0); position: fixed; top: 50%; left: 50%; //position: absolute; //top: expression(eval(document.body.scrollTop + (document.body.offsetHeight/2)) + \'px\'); margin-top: 0px; margin-left: 0px;"></div>';
    div.innerHTML += '<style type="text/css"> div#dlkp_txt a { color: #059; } div#dlkp_txt a:hover { color: #950; } </style><div id="dlkp_txt" style="z-index: 10003; display: none; opacity: 0; filter: alpha(opacity=0); /*border: 2px solid red;*/ position: fixed; width: 100%; top: 50%; left: 0px; //position: absolute; //top: expression(eval(document.body.scrollTop + (document.body.offsetHeight/2)) + \'px\'); margin-top: -0px; margin-left: 0px; font: 13px Trebuchet MS; text-align: center; color: black;"></div>';
  }
  else ge('dlkp_img').style.visibility = 'hidden';
  ge('dlkp_txt').style.opacity = 0;
  if(ge('dlkp_txt').filters) { ge('dlkp_txt').style.filter = 'alpha(opacity=0)'; ge('dlkp_txt').filters.alpha.opacity = 0; }
  ge('dlkp_txt').innerHTML = '';
  var flagFirst = ge('dlkp_fog').style.display == 'none' ? true : false;
  ge('dlkp_fog').style.display = 'block';
  ge('dlkp_fog').style.cursor = 'wait';
  ge('dlkp_img').style.display = 'block';
  ge('dlkp_txt').style.display = 'block';
  if(flagFirst) fade('dlkp_fog',0,100,0,20,'loadImage(\'' + currImage +'\',\'' + prevImage + '\',\'' + nextImage + '\',\'' + currText + '\',\'' + currPos + '\',\'' + imagesCount + '\');');
  else loadImage(currImage,prevImage,nextImage,currText,currPos,imagesCount);
  window.timerOut = setTimeout("dlkpTimeout('" + currImage + "','" + nextImage + "')",5000);
  return false;
}

function dlkpTimeout(curr,next)
{
  ge('dlkp_fog').style.cursor = 'default';
  if(!next || next == 'false') { alert(curr + '\n\nНе удается загрузить это изображение.'); hideImage(); }
  else if(confirm(curr + '\n\nНе удается загрузить это изображение. Перейти к следующему?')) showImage(next);
  else hideImage();
  return false;
}

function imageLoaded(currImage,prevImage,nextImage,currText,currPos,imagesCount)
{
  clearTimeout(window.timerOut);
  currImage = unescape(currImage);
  prevImage = unescape(prevImage);
  nextImage = unescape(nextImage);
  currText = unescape(currText);
  var div = ge('dlkp_img');
  var img = ge('dlkp_img').getElementsByTagName('img')[0];
  var txt = ge('dlkp_txt');
  if(!img) return false;
  var winWidth = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth);
  var winHeight = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight);
  var srcWidth = img.width;
  var srcHeight = img.height;
  var imgWidth = img.width;
  var imgHeight = img.height;
  var currZoom = 100;
  if(imgWidth+100 > winWidth || imgHeight+100 > winHeight)
  {
    if(imgWidth/imgHeight > winWidth/winHeight)
    {
      var tempWidth = winWidth-100;
      var tempHeight = tempWidth*imgHeight/imgWidth;
      currZoom = Math.round(tempWidth*100/imgWidth);
      imgWidth = tempWidth;
      imgHeight = tempHeight;
    }
    else
    {
      var tempHeight = winHeight-100;
      var tempWidth = tempHeight*imgWidth/imgHeight;
      currZoom = Math.round(tempWidth*100/imgWidth);
      imgWidth = tempWidth;
      imgHeight = tempHeight;
    }
  }
  img.width = imgWidth;
  img.height = imgHeight;

  img.style.border = '1px dotted #939195';

  div.style.width = imgWidth + 10 + 'px';
  div.style.height = imgHeight + 10 + 'px';
  div.style.marginTop = '-' + imgHeight/2 - (currText?10:0) + 'px';
  div.style.marginLeft = '-' + imgWidth/2 + 'px';
  txt.style.marginTop = imgHeight/2 + 'px';
  div.style.visibility = 'visible';
  ge('dlkp_txt').innerHTML = currText;
  fade('dlkp_img',0,100,0,20,"ge('dlkp_fog').style.cursor = 'default';" + (currPos == 1 && imagesCount > 1 ? "ge('dlkp_notice').innerHTML = 'Это первое изображение в серии.'; setTimeout(\"ge('dlkp_notice').innerHTML = '';\",1000);" : '') + " fade('dlkp_txt',0,100,0,20);");
  if(ge('dlkp_oldtitle').innerHTML == '') ge('dlkp_oldtitle').innerHTML = document.title;
  var newTitle = imagesCount > 1 ? '[' + currPos + '/' + imagesCount + '] ' : '';
  newTitle += currImage.replace(/^.*[\/\\]/g,'') + ' (';
  switch(currImage.substr(currImage.lastIndexOf('.')+1).toLowerCase())
  {
    case 'gif': newTitle += 'GIF-изображение'; break;
    case 'jpg': newTitle += 'JPEG-изображение'; break;
    case 'png': newTitle += 'PNG-изображение'; break;
    default: newTitle += currImage.substr(currImage.lastIndexOf('.')+1).toUpperCase(); break;
  }
  var imageSize = (img.fileSize > 0 ? (img.fileSize >= 1048576 ? Math.round(img.fileSize/1048576*10)/10 + ' МБ, ' : Math.round(img.fileSize/1024*10)/10 + ' КБ, ') : '');
  newTitle += ', ' + srcWidth + String.fromCharCode(215) + srcHeight + ', ' + imageSize.split('.').join(',') + 'масштаб ' + currZoom + '%)';
  document.title = newTitle;
  return false;
}

function hideImage()
{
  if(window.timerOut) clearTimeout(window.timerOut);
  var winWidth = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth);
  var winHeight = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight);
  var imgWidth = ge('dlkp_img').getElementsByTagName('img')[0].width;
  var imgHeight = ge('dlkp_img').getElementsByTagName('img')[0].height;
  fade('dlkp_img',100,0,0,20,"ge('dlkp_fog').style.cursor = 'default'; ge('dlkp_txt').style.display = 'none'; ge('dlkp_img').innerHTML = ''; fade('dlkp_fog',100,0,0,20,\"ge('dlkp_fog').style.display = 'none'; ge('dlkp_img').style.display = 'none';\");");
  if(ge('dlkp_oldtitle').innerHTML) document.title = ge('dlkp_oldtitle').innerHTML;
  return false;
}

function urlencode(str)
{
  var trans = [];
  for (var i=0x410; i<=0x44F; i++) trans[i] = i-0x350;
  trans[0x401] = 0xA8;
  trans[0x451] = 0xB8;
  var ret = [];
  for (var i=0; i<str.length; i++)
  {
    var n = str.charCodeAt(i);
    if(typeof trans[n] != 'undefined') n = trans[n];
    if(n <= 0xFF) ret.push(n);
  }
  return escape(String.fromCharCode.apply(null,ret));
}

