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

  this.mainDiv = document.createElement('div');
  this.mainDiv.className = 'overlayGallery';
  this.overlay.appendChild(this.mainDiv);
  
  var closeBtn = document.createElement('div');
  closeBtn.className = 'close';
  closeBtn.onclick = function() { selfRef.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);

  var newImage = document.createElement('img');
  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.mainDiv.style.visibility = 'visible';
};

overlayGallery.prototype.hide = function() {
  this.mainDiv.style.visibility = 'hidden';
  this.overlay.hide();
};

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 = document.createElement('img');
    newImage.className = 'fullSize';
    newImage.style.visibility = 'hidden';
    newImage.src = this.imagePath + this.images[index];
    newImage.onclick = function() { selfRef.next(); };
    this.mainDiv.replaceChild(newImage, this.bigImage);
    this.bigImage = newImage;

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

    this.loader();
  }
};

overlayGallery.prototype.loader = function() {
  if (this.bigImage.complete) {
    var test = this.bigImage.width;
    this.mainDiv.style.width = this.bigImage.width+'px';
    this.bigImage.style.visibility = 'visible';
  } else {
    var selfRef = this;
    setTimeout(function() {selfRef.loader()}, 300);
  }
};