var images = new Array, imgContainer = null, imgSelector = null, imgTitle = null;

function AdjustImageSizeAndPos()
{
  var
    W = imgContainer.clientWidth,
    w = this.realWidth,
    Ww = (W - 20) / w,
    H = imgContainer.clientHeight,
    h = this.realHeight,
    Hh = (H - 20) / h;

// размер картинки
  if (Ww < Hh) {
    this.style.width = (W - 20) + 'px';
    this.style.height = 'auto';
  } else {
    this.style.height = (H - 20) + 'px';
    this.style.width = 'auto';
  }

// положение картинки
//  this.style.left = parseInt(W - this.width) / 2 + 'px';
  this.style.top = parseInt(H - this.height) / 2 + 'px';
}

function AfterLoad()
{
  this.realWidth = this.width;
  this.realHeight = this.height;
  this.removeAttribute('width');
  this.removeAttribute('height');
  //this.adjustSizeAndPos();
  this.style.display = 'none';
  this.style.position = 'relative';
  //this.onclick = new Function('imgContainer.nextImage();');
  imgContainer.appendChild(this);
  this.loaded = true;
  if (this.index == currentImage) {
    this.style.display = 'inline';
    this.adjustSizeAndPos();
    this.selector.className = 'active';
  }
}

function NextImage()
{
  if (currentImage > -1) {
    var newCurrentImage = currentImage;
    do {
      newCurrentImage++;
      if (newCurrentImage == images.length) newCurrentImage = 0;
    } while (!imgContainer.selectImage(newCurrentImage) && newCurrentImage != currentImage);
  }
}

function PrevImage()
{
  if (currentImage > -1) {
    var newCurrentImage = currentImage;
    do {
      newCurrentImage--;
      if (newCurrentImage < 0) newCurrentImage = images.length - 1;
    } while (!imgContainer.selectImage(newCurrentImage) && newCurrentImage != currentImage);
  }
}

function SelectImage(imageIndex)
{
  if (currentImage > -1 && imageIndex > -1 && imageIndex < images.length
      && imageIndex != currentImage && images[imageIndex].loaded) {
    var image = images[currentImage];
    image.style.display = 'none';
    image.selector.className = '';
    image = images[imageIndex];
    image.style.display = 'inline';
    image.adjustSizeAndPos();
    image.selector.className = 'active';
    image.selector.blur();
    currentImage = imageIndex;
    if (image.alt) {
      imgTitle.innerHTML = image.alt;
      imgTitle.style.display = 'block';
    } else
      imgTitle.style.display = 'none';
    return true;
  }
  return false;
}

function InitializeImages()
{
  var image, sel, i;
  imgContainer = document.getElementById('imgcont');
  imgTitle = document.getElementById('imgtitle');
  imgSelector = document.getElementById('imgsel');
  var selectors = imgSelector.getElementsByTagName('a');

  window.onresize = new Function('if (currentImage > -1) images[currentImage].adjustSizeAndPos();');

  if (imgContainer && imgSelector) {
    imgContainer.nextImage = NextImage;
    imgContainer.prevImage = PrevImage;
    imgContainer.selectImage = SelectImage;
    imgContainer.onclick = new Function('this.nextImage();');
    imgContainer.style.cursor = 'pointer';
    for (i=0; i<imagesFiles.length; i++) {
      image = document.createElement('img');
      image.index = i;
      image.loaded = false;
      image.adjustSizeAndPos = AdjustImageSizeAndPos;
      image.afterLoad = AfterLoad;
      image.onload = new Function('this.afterLoad();');
      image.src = imagesFiles[i];
      image.alt = imagesTitles[i];
      images.push(image);

      sel = selectors[i];
      sel.parentNode.onclick = new Function('imgContainer.selectImage(' + i + ');');
      sel.onclick = new Function('return false;');
      image.selector = sel;

      if (i == currentImage && image.alt) {
        imgTitle.innerHTML = image.alt;
        imgTitle.style.display = 'block';
      }
    }
  }

}

