var imgWndw, items, w, h, imgTitle;

function imgOpen(imgLink) {
  if (typeof(window.resizeBy) == 'undefined') return true;
  if (w > screen.availWidth - 55) w = screen.availWidth - 50;
  if (h > screen.availHeight - 55) h = screen.availHeight - 50;
  imgWndw = window.open('',imgLink.target,'width=' + w +
    ',height=' + h + ',toolbar=no,menubar=no,location=no,status=no,'+
    'resizable=yes,scrollbars=yes,left=5,top=5');
  with (imgWndw.document){
    open();
    write('<?xml version="1.0" encoding="windows-1251"?>\n'+
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' +
    '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">\n'+
    '<html><head><title>'+imgTitle+'</title>'+
    '</head><body leftmargin="0" topmargin="0" '+
    'rightmargin="0" bottommargin="0" marginwidth="0" '+
    'style="margin:0;padding:0;position:fixed;overflow:none;" '+
    'marginheight="0"><img src="'+imgLink.href+'" border="0" '+
    ' alt="'+imgTitle+'" title="'+imgTitle+'" class="r" /></body></html>');
    close();
  }
  return false;
}

function change(from, to) {
  var i;
  try {
    if (from < to) {
      for (i = from; i < to; i++) {
        move(i, i + 1);
      }
    } else {
      for (i = from; i > to; i--) {
        move(i, i - 1);
      }
    }
  } catch (e) {
    alert(e);
  }
  try {
    fill();
  } catch (e) {
    alert(e);
  }
}

function move(from, to) {
  var tmp = items[to];
  items[to] = items[from];
  items[from] = tmp;
}

function fill() {
  for(var i = 0; i < items.length; i++) {
    try {
      var image = document.getElementById("img" + i);
      if (undefined != image) {
        image.src = items[i][0];
        image.alt = items[i][1];
        if (items[i][3] > items[i][4]) {
          image.width = 80;
          image.height = items[i][4] * 80 / items[i][3];
        } else {
          image.width = items[i][3] * 80 / items[i][4];
          image.height = 80;
        }
      }
      var comment = document.getElementById("name" + i);
      if (undefined != comment) {
        comment.innerHTML = items[i][1];
      }
      var inputItem = document.getElementById("item" + i);
      if (undefined != inputItem) {
        inputItem.name = "values(" + items[i][2] + ")";
      }
    } catch (e) {
      alert(i + ' [' + e + ']');
    }
  }
}
