function overlayGallery(images, captions, imagePath, thumbPath) {
  if (images.length == 0) {
    return;
  }
  
  var selfRef = this;
  
  this.images = images;
  this.captions = captions;
  this.currentIndex = 0;
  this.imagePath = imagePath;
  this.thumbPath = thumbPath;
  
  this.overlay = getOverlay();

  this.wrapper = document.createElement('div');
  this.wrapper.className = 'galleryWrapper';
  document.body.appendChild(this.wrapper);

  this.mainDiv = document.createElement('div');
  this.mainDiv.className = 'overlayGallery';
  this.wrapper.appendChild(this.mainDiv);
  hideOnOverlayClose.push(this);
  
  var closeBtn = document.createElement('div');
  closeBtn.className = 'close';
  closeBtn.onclick = function() { selfRef.overlay.hide(); };
  this.mainDiv.appendChild(closeBtn);

  var nav = document.createElement('div');
  nav.className = 'nav';

  this.nextBtn = document.createElement('div');
  this.nextBtn.className = 'next';
  this.nextBtn.onclick = function() { selfRef.next(); };
  nav.appendChild(this.nextBtn);
  
  this.prevBtn = document.createElement('div');
  this.prevBtn.className = 'prev';
  this.prevBtn.onclick = function() { selfRef.prev(); };
  nav.appendChild(this.prevBtn);
  
  var indexDiv = document.createElement('div');
  this.indexText = document.createElement('span');
  this.indexText.appendChild(document.createTextNode("1"));
  indexDiv.appendChild(this.indexText);
  indexDiv.appendChild(document.createTextNode(" / " + this.images.length));
  indexDiv.className = 'imageNo';
  nav.appendChild(indexDiv);
  
  this.mainDiv.appendChild(nav);

  this.captionDiv = document.createElement('div');
  this.captionDiv.className = 'caption';
  this.mainDiv.appendChild(this.captionDiv);
  this.changeCaption();

  var newImage = new Image();
  newImage.className = 'fullSize';
  newImage.style.visibility = 'hidden';
  newImage.src = this.imagePath + this.images[0];
  newImage.onclick = function() { selfRef.next(); };
  this.mainDiv.appendChild(newImage);
  this.bigImage = newImage;
};

overlayGallery.prototype.show = function(index) {
  this.overlay.show();
  this.switchImage(index);
  this.reposition();
  this.wrapper.style.display = 'block';
};

overlayGallery.prototype.hide = function() {
  this.wrapper.style.display = '';
};

overlayGallery.prototype.next = function() {
  if (this.images[this.currentIndex + 1]) {
    this.switchImage(this.currentIndex + 1);
  } else {
    this.switchImage(0);
  }
};

overlayGallery.prototype.prev = function() {
  if (this.images[this.currentIndex - 1]) {
    this.switchImage(this.currentIndex - 1);
  } else {
    this.switchImage(this.images.length - 1);
  }
};

overlayGallery.prototype.switchImage = function(index) {
  if (this.images[index]) {
    this.currentIndex = index;
    
    var selfRef = this;

    var newImage = new Image();
    newImage.className = 'fullSize';
    newImage.style.visibility = 'hidden';
    newImage.onload = function() {
      selfRef.changeCaption();
      this.style.visibility = '';
      selfRef.mainDiv.style.width = this.width+'px';
    }
    newImage.onclick = function() { selfRef.next(); };
    this.captionDiv.style.display = 'none';
    this.mainDiv.replaceChild(newImage, this.bigImage);
    this.bigImage = newImage;

    newImage.src = this.imagePath + this.images[index];

    this.indexText.firstChild.data = index + 1;
  }
};

overlayGallery.prototype.changeCaption = function() {
  if (this.captions[this.currentIndex]) {
    this.captionDiv.style.display = 'block';
    this.captionDiv.innerHTML = this.captions[this.currentIndex];
  }
}

overlayGallery.prototype.reposition = function() {
  // offset from top
  var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	this.wrapper.style.top = (yScroll+10)+'px';
}