function displayalbum(page) {
  var maxpage = Math.ceil(currentphotos.length / 6);
  // Page number
  var div = '<h2>Page ' + page + ' of ' + maxpage + '</h2>';
  // Links to next and previous pages
  if (page == 1) {
    div = div + '<h3><a href="javascript:displayalbum(' + (page+1) + ');">Next Page</a></h3>';
  } else if (page == maxpage) {
    div = div + '<h3><a href="javascript:displayalbum(' + (page-1) + ');">Previous Page</a></h3>';
  } else {
    div = div + '<h3><a href="javascript:displayalbum(' + (page-1) + ');">Previous Page</a> | ' +
                    '<a href="javascript:displayalbum(' + (page+1) + ');">Next Page</a></h3>';
  }
  // Direct page links
  div = div + '<h4>';
  for (var i = 1; i <= maxpage; i++) {
    if (i > 1) {
      div = div + ' ';
    }
    if (i != page) {
      div = div + '<a href="javascript:displayalbum(' + i + ');">' + i + '</a>';
    } else {
      div = div + i;
    }
  }
  div = div + '</h4>';
  // Photos
  div = div + '<table><tr>';
  for (var i = (page-1) * 6; i < page * 6 && i < currentphotos.length; i++) {
    if (i % 6 == 3) {
      div = div + '</tr><tr>';
    }
    div = div + '<td><a href="javascript:displayphoto(' + (i+1) + ');"><img src="thumbs/' + currentphotos[i].fn + '" width="' + currentphotos[i].tw + '" height="' + currentphotos[i].th + '"';
    if (currentphotos[i].d) {
      div = div + ' alt=' + quotestr(currentphotos[i].d);
    }
    div = div + '/></a>';
    if (currentphotos[i].d) {
      div = div + '<br>' + currentphotos[i].d;
    }
    div = div + '</td>';
  }
  div = div + '</tr></table>';
  // Display
  document.getElementById("dynamicalbum").innerHTML = div;
}

function displayphoto(photo) {
  // Photo number
  var div = '<h2>Photo ' + photo + ' of ' + currentphotos.length + '</h2>';
  // Previous | Index | Next
  div = div + '<h3>';
  if (photo > 1) {
    div = div + '<a href="javascript:displayphoto(' + (photo-1) + ');">Previous Photo</a> | ';
  } 
  div = div + '<a href="javascript:displayalbum(' + (Math.floor((photo-1)/6)+1) + ');">Index</a>';
  if (photo < currentphotos.length) {
    div = div + ' | <a href="javascript:displayphoto(' + (photo+1) + ');">Next Photo</a>';
  }
  div = div + '</h3>';
  // Photo
  div = div + '<div class="photo" style="width: ' + currentphotos[photo-1].w + 'px;">';
  if (photo < currentphotos.length) {
    div = div + '<a href="javascript:displayphoto(' + (photo+1) + ');">';
  }
  div = div + '<img src="' + currentphotos[photo-1].fn + '" width="' + currentphotos[photo-1].w + '" height="' + currentphotos[photo-1].h + '"';
  if (currentphotos[photo-1].d) {
    div = div + ' alt=' + quotestr(currentphotos[photo-1].d);
  }
  div = div + '/>';
  if (photo < currentphotos.length) {
    div = div + '</a>';
  }
  // Information
  div = div + '<br>';
  div = div + '<div class="fn" style="width: ' + currentphotos[photo-1].w + 'px;">' + currentphotos[photo-1].fn.match(/[^.]+/) + '</div>'
  div = div + currentphotos[photo-1].cd;
  if (currentphotos[photo-1].d) {
    div = div + '<br><br>' + currentphotos[photo-1].d;
  }
  if (currentphotos[photo-1].hs) {
    div = div + '<br>';
    var hs = currentphotos[photo-1].hs;
    var re = /([^,]+)(?:, )?/g;
    var m = re.exec(hs);
    while (m != null) {
      div = div + '<br>' + m[1].replace(/\|/g, ' > ');
      m = re.exec(hs);
    }
  }
  if (currentphotos[photo-1].l) {
    div = div + '<br><br>' + currentphotos[photo-1].l.replace(/\|/g, ' > ');
  }
  div = div + '</div>';
  // Display
  document.getElementById("dynamicalbum").innerHTML = div;
}

function quotestr(str) {
  return '"' + str.replace(/"/g, '\\"') + '"';
}