// album pic.js
// Copyright (C) 2007, Hoylen Sue

var SLIDESHOW_INTERVAL = 1000 * 10; // milliseconds

var g_url_prev;
var g_url_image_next_0;
var g_url_image_next_1;
var g_url_image_next_loading_0;
var g_url_image_next_loading_1;
var g_url_image_play;
var g_url_image_pause;

var g_loading_next = false;

var g_slideshow_next_URL;
var g_slideshow_running = false; // indicates whether slideshow is running
var g_onscreen_done = false;
var g_next_image_ready = false;
var g_startdelay_waiting = 0;

function album_set_root(n0, n1, nL0, nL1, play0, play1, pause0, pause1) {
  g_url_image_next_0 = n0;
  g_url_image_next_1 = n1;
  g_url_image_next_loading_0 = nL0;
  g_url_image_next_loading_1 = nL1;
  g_url_image_play_0 = play0;
  g_url_image_play_1 = play1;
  g_url_image_pause_0 = pause0;
  g_url_image_pause_1 = pause1;
}

var preload_done = function()
{

}

var preload_prev = function()
{
  g_loading_next = false;

  // Next image is ready, try to advance

//  g_next_image_ready = true;
//  slideshow_try_to_advance();

  // Preload previous image (if any)

  if (g_url_prev != null) {
    var i = new Image;
    i.onload = preload_done;
    i.src = g_url_prev;
  } else {
    preload_done();
  }
}

/* Preload next and previous images */

function pload(url_prev, url_next) {

  // Current image is now displayed, so can advance after slideshow interval
  //setTimeout("slideshow_onscreen_done()", SLIDESHOW_INTERVAL);

  if (document.images) {

    g_url_prev = url_prev;
    
    if (url_next != null) {
      // Preload next image

      var i = new Image;
      i.onload = preload_prev;
      i.src = url_next;
      g_loading_next = true;
    } else {
      preload_prev();
    }
  }
}

// Slideshow functions

function album_slideshow_button(url, is_last)
{
  var text = (is_last) ? "Restart slideshow" : "Slideshow";

  g_slidshow_next_URL = url;

  document.write("<div id=\"slideshow-button\">");
  document.write("<a title=\"" + text + "\" onclick=\"slideshow_click()\">");
  document.write("<img id=\"slideshow-button-image\" src=\"" +
    g_url_image_play_0 + "\" alt=\"" + text + "\"" +
    " onmouseover=\"slideshow_mouseover()\"" +
    " onmouseout=\"slideshow_mouseout()\" />");
  document.write("</a>");
  document.write("</div>");

  // Detect if page was loaded by automatic advancement during a slideshow

  g_next_image_ready = false;

  if (! is_last) {
    g_slideshow_running = (window.location.search == "?slideshow");
  } else {
    g_slideshow_running = false;
  }

  slideshow_mouseout();
}

function slideshow_onscreen_done() {
  g_onscreen_done = true;
  slideshow_try_to_advance();
}

function slideshow_startdelay_done() {
  g_startdelay_waiting--;
  slideshow_try_to_advance();
}

function slideshow_try_to_advance() {
  if (g_slideshow_running &&
      g_onscreen_done &&
      g_next_image_ready &&
      g_startdelay_waiting == 0) {
    document.location = g_slidshow_next_URL + "?slideshow";
  }
}

function slideshow_click()
{
  g_slideshow_running = ! g_slideshow_running;
  slideshow_mouseover();

  if (g_slideshow_running) {
    g_startdelay_waiting++;
    setTimeout("slideshow_startdelay_done()", SLIDESHOW_INTERVAL);
  }
}

//EOF

