var currImg = 0;
var squares;
var mainImg;
imageslength = images.length;
var squares = new Array(imageslength);


function init() {
var imageslength = images.length;
var squarediv = document.getElementById('squares');

for (var i = 0; i < imageslength; i++) {
  newSquare = document.createElement('img');
  newSquare.setAttribute('id', 'square'+i);
  newSquare.onmouseover = switchSquare;
  newSquare.oncontextmenu = function() { return false; }
  newSquare.onclick = getFolio;
  squares[i] = newSquare;
  squarediv.appendChild(newSquare);
}
    squareslength = i;

  mainImg = document.getElementById('mainImage');  
  mainImg.onmouseover = function() { this.style.cursor='pointer';}
  mainImg.onclick = getFolio;

  var lindex = document.getElementById('LindexArrow');
  lindex.onmouseover = function() { this.src='/images/graphics/arrow_left1.gif'; }
  lindex.onmouseout = function() { this.src='/images/graphics/arrow_left2.gif'; }
  lindex.oncontextmenu = function() { return false; }
  lindex.onclick = function() { currImg--; changePhoto(); }

  var rindex = document.getElementById('RindexArrow');
  rindex.onmouseover = function() { this.src='/images/graphics/arrow_right1.gif'; }
  rindex.onmouseout = function() { this.src='/images/graphics/arrow_right2.gif'; }
  rindex.oncontextmenu = function() { return false; }
  rindex.onclick = function() { currImg++; changePhoto(); }

  changePhoto();
}

function switchSquare(e) {
  for (var i = 0; i < squareslength; i++) {
    if (squares[i].id == this.id) {
      currImg=i;
      changePhoto();
    }
  }
}

function changePhoto() {
if (currImg < 0) { currImg = squareslength - 1; }
if (currImg == squareslength) { currImg = 0; }
  for (var i = 0; i < squareslength; i++) {
   squares[i].src = '/images/graphics/square.gif';
  }
    squares[currImg].src = '/images/graphics/square1.gif';
    mainImg.src = images[currImg];

}


function getFolio() {
      var imgSrc = images[currImg];
      var lastSlash= imgSrc.lastIndexOf("/");
      var penulSlash = imgSrc.substring(0, lastSlash).lastIndexOf("/");
      var dirname = imgSrc.substring(penulSlash + 1, lastSlash);
      location.href='/cgi-bin/portfolio.cgi?folio=' + dirname; 
}
