// The implementor is expected to define a variable named "gallery" that looks like
// this:
//   var gallery={
//     title:"Gloria! Gloria!",
//     photos:[
//       { url:"/photogallery/12.jpg",caption:"<p>A bunch of mugs.</p>",width:300,height:225},
//       { url:"/photogallery/14.jpg",caption:"",width:225,height:300}
//     ],
//     current:-1,
//     window:null,
//     auto:null
//   };

// The "openSlideshow()" function should be an action connected to a link which invites
// the user to open a slideshow window. The HTML of the window is inside the Javascript
// to limit cross-site-scripting restrictions which the implementor might unintentionally
// introduce by using an external HTML file, and which one browser may forgive, while
// another may not.
function openSlideshow() {
  gallery.window = window.open('about:blank','SlideShow','width=500,height=460');
  gallery.window.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\
<head>\
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />\
  <title></title>\
  <link rel="shortcut icon" href="/favicon.ico" />\
  <link rel="stylesheet" href="/includes/slideshow.css" />\
<body>\
\
<div class="header">\
  <div class="headerfade">\
    <h1 id="title"></h1>\
    <div class="links"><a href="#" onclick="opener.lastPhoto(); return false;">PREV</a> | <a href="#" onclick="opener.autoPhoto(); return false;">AUTO</a> | <a href="#" onclick="opener.nextPhoto(); return false;">NEXT</a></div>\
    <div style="clear: left"></div>\
  </div>\
</div>\
\
<div id="transition" style="filter:progid:DXImageTransform.Microsoft.fade(duration=5.0)">\
  <div id="caption">\
  </div>\
  \
  <div class="photofield">\
    <div><img id="photo" src="" width="" height="" alt="" /></div>\
  </div>\
</div>\
\
</body>\
</html>');

  // I find if I try to use DOM methods immediately, they fail, but if I wait a little,
  // they succeed. Consigning the assignment to the inside of an onload method seems
  // to solve my problem in Mozilla.
  if (typeof(gallery.window.onload)=="undefined") {
    gallery.window.onload=function() {
      var title=gallery.window.document.getElementById('title');
      title.innerHTML=gallery.title;
      autoPhoto();
    };
    gallery.window.document.close();

  // But in IE I have the opposite problem. Luckily, the state of the "window.onload"
  // function is a bellweather.
  } else {
    gallery.window.document.close();
    var title=gallery.window.document.getElementById('title');
    title.innerHTML=gallery.title;
    autoPhoto();
  }
}

// Shows the next photo in the series, wrapping around if necessary.
// nextPhoto and lastPhoto cancels autoPhoto.
function nextPhoto() {
  if (gallery.auto) {
    clearTimeout(gallery.auto);
    gallery.auto=null;
  }

  gallery.current++;
  if (gallery.current >= gallery.photos.length) {
    gallery.current=0;
  }
  showPhoto();
}

function lastPhoto() {
  if (gallery.auto) {
    clearTimeout(gallery.auto);
    gallery.auto=null;
  }

  gallery.current--;
  if (gallery.current < 0) {
    gallery.current=gallery.photos.length-1;
  }
  showPhoto();
}

function showPhoto() {
  if (!gallery.window) { return false; }
  if ((gallery.current < 0) || (gallery.current >= gallery.photos.length)) { return false; }

  try {
    var D=gallery.window.document;
    var caption=D.getElementById('caption');
  }
  catch (e) { return; }
  var photo=D.getElementById('photo');
  var transition=D.getElementById('transition');

  if (transition && transition.filters && (transition.filters.length > 0)) {
    transition.filters[0].apply();
  }

  caption.innerHTML=gallery.photos[gallery.current].caption;

  if (gallery.photos[gallery.current].caption == "") {
    caption.style.display='none';
  } else {
    caption.style.display='block';
  }

  photo.src=gallery.photos[gallery.current].url;
  photo.width=gallery.photos[gallery.current].width;
  photo.height=gallery.photos[gallery.current].height;

  photo=photo.parentNode;
  photo.style.width=""+gallery.photos[gallery.current].width+"px";
  photo.style.height=""+gallery.photos[gallery.current].height+"px";

  if (transition && transition.filters && (transition.filters.length > 0)) {
    transition.filters[0].play();
  }

  return false;
}

// Starts an automatic slideshow.
function autoPhoto() {
  // If the window is closed while it's auto-photo-ing, there will be a javascript error.
  // In this case, don't keep looping. This is the only place I can test. Setting
  // the onunload event is unreliable across different browsers.

  // IE craps out setting D. Mozilla craps out setting caption.

  try {
    var D=gallery.window.document;
    var caption=D.getElementById('caption');
  }
  catch (e) { return; }

  nextPhoto();
  gallery.auto=setTimeout("autoPhoto()",5000);
}