function displayalbum(album, page) {
  var maxpage = Math.ceil(albums[album].currentphotos.length / 6);
  // Page number
  var div = '<h2>Page ' + page + ' of ' + maxpage + ' | ';
  // Links to next and previous pages
  if (page == 1) {
    div = div + '<a href="javascript:displayalbum(' + album + ', ' + (page+1) + ');">Next Page</a>';
  } else if (page == maxpage) {
    div = div + '<a href="javascript:displayalbum(' + album + ', ' + (page-1) + ');">Previous Page</a>';
  } else {
    div = div + '<a href="javascript:displayalbum(' + album + ', ' + (page-1) + ');">Previous Page</a> | ' +
                    '<a href="javascript:displayalbum(' + album + ', ' + (page+1) + ');">Next Page</a>';
  }
  div = div + '</h2>'
  // Direct page links
  div = div + '<p>';
  for (var i = 1; i <= maxpage; i++) {
    if (i > 1) {
      div = div + ' ';
    }
    if (i != page) {
      div = div + '<a href="javascript:displayalbum(' + album + ', ' + i + ');">' + i + '</a>';
    } else {
      div = div + i;
    }
  }
  div = div + '</p>';
  // Photos
  div = div + '<table class="album"><tr>';
  for (var i = (page-1) * 6; i < page * 6 && i < albums[album].currentphotos.length; i++) {
    if (i % 6 == 3) {
      div = div + '</tr><tr>';
    }
    div = div + '<td class="album"><a href="javascript:displayphoto(' + album + ', ' + (i+1) + ');"><img class="album" src="' + albums[album].url + 'thumbs/' + albums[album].currentphotos[i].fn + '" width="' + albums[album].currentphotos[i].tw + '" height="' + albums[album].currentphotos[i].th + '"';
    if (albums[album].currentphotos[i].d) {
      div = div + ' alt=' + quotestr(albums[album].currentphotos[i].d);
    }
    div = div + '/></a>';
    if (albums[album].currentphotos[i].d) {
      div = div + '<br>' + albums[album].currentphotos[i].d;
    }
    div = div + '</td>';
  }
  div = div + '</tr></table>';
  // Display
  document.getElementById("dynamicalbum" + album).innerHTML = div;
}

function displayphoto(album, photo) {
  // Photo number
  var div = '<h2>Photo ' + photo + ' of ' + albums[album].currentphotos.length + ' | ';
  // Previous | Index | Next
  if (photo > 1) {
    div = div + '<a href="javascript:displayphoto(' + album + ', ' + (photo-1) + ');">Previous Photo</a> | ';
  } 
  div = div + '<a href="javascript:displayalbum(' + album + ', ' + (Math.floor((photo-1)/6)+1) + ');">Index</a>';
  if (photo < albums[album].currentphotos.length) {
    div = div + ' | <a href="javascript:displayphoto(' + album + ', ' + (photo+1) + ');">Next Photo</a>';
  }
  div = div + '</h2>';
  // Photo
  div = div + '<div class="album" style="width: ' + albums[album].currentphotos[photo-1].w + 'px;">';
  if (photo < albums[album].currentphotos.length) {
    div = div + '<a href="javascript:displayphoto(' + album + ', ' + (photo+1) + ');">';
  }
  div = div + '<img class="album" src="' + albums[album].url + albums[album].currentphotos[photo-1].fn + '" width="' + albums[album].currentphotos[photo-1].w + '" height="' + albums[album].currentphotos[photo-1].h + '"';
  if (albums[album].currentphotos[photo-1].d) {
    div = div + ' alt=' + quotestr(albums[album].currentphotos[photo-1].d);
  }
  div = div + '/>';
  if (photo < albums[album].currentphotos.length) {
    div = div + '</a>';
  }
  // Information
  div = div + '<br>';
  div = div + '<div class="albumfn" style="width: ' + albums[album].currentphotos[photo-1].w + 'px;">' + albums[album].currentphotos[photo-1].fn.match(/[^.]+/) + '</div>'
  div = div + albums[album].currentphotos[photo-1].cd;
  if (albums[album].currentphotos[photo-1].d) {
    div = div + '<br><br>' + albums[album].currentphotos[photo-1].d;
  }
  if (albums[album].currentphotos[photo-1].hs) {
    div = div + '<br>';
    var hs = albums[album].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 (albums[album].currentphotos[photo-1].l) {
    div = div + '<br><br>' + albums[album].currentphotos[photo-1].l.replace(/\|/g, ' > ');
  }
  div = div + '</div>';
  // Display
  document.getElementById("dynamicalbum" + album).innerHTML = div;
}

function quotestr(str) {
  return '"' + str.replace(/"/g, '\\"') + '"';
}
