/* Photo gallery script
 * Copyright (C) 2007, Christopher Wolfe
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Required HTML elements:
 *   id="gallery_form": control form, events will be set automatically.
 *   id="image": img in which to place images.
 *   id="caption": children will be replaced with caption.
 *
 * Required variables:
 *   gallery_images: Array of image paths.
 *   gallery_captions: Array of image captions.
 */

var gallery_default_image;
var gallery_default_caption;
var gallery_loading_image;
var gallery_loading_timeout;
var gallery_error_image;
var gallery_index = 0;
var gallery_auto_timeout = -1;
var gallery_full_size = 0;
var gallery_preload;

function gallery_init() {
  var form = document.getElementById("gallery_form");
  if (form) {
    form.onsubmit = function() { return false; }
  
    for (var i = 0; i < form.elements.length; ++i) {
      if (form.elements[i].type == "submit") {
        var name = form.elements[i].name;
        form.elements[i].onclick = new Function("gallery_" + name + "(this.form);");
      }
    }

    var image = document.getElementById("image");
    image.onclick = gallery_full_size_toggle;

    gallery_preload = new Image();

    gallery_preload.onload = gallery_show_image;
    gallery_preload.onerror = gallery_show_error;

    gallery_loading_image = new Image();
    gallery_loading_image.src = 'loading.png';
    gallery_loading_timeout = 10;

    gallery_error_image = new Image();
    gallery_error_image.src = 'error.png';

    gallery_default_image = document.getElementById('image').src;
    gallery_default_caption = document.getElementById('caption').firstChild;
    gallery_update();

    window.onresize = gallery_update;
  }
}

function gallery_first(form) {
  gallery_auto_stop(form);
  gallery_index = 0;
  gallery_update();
}

function gallery_previous(form) {
  gallery_auto_stop(form);
  gallery_index -= 1;
  gallery_update();
}

function gallery_next(form) {
  gallery_auto_stop(form);
  gallery_index += 1;
  gallery_update();
}

function gallery_last(form) {
  gallery_auto_stop(form);
  gallery_index = gallery_images.length - 1;
  gallery_update();
}

function gallery_update() {
  if (gallery_index < 0)
    gallery_index += gallery_images.length;
  if (gallery_index >= gallery_images.length)
    gallery_index -= gallery_images.length;

  var image = document.getElementById("image");

  if (gallery_images[gallery_index]) {
    gallery_preload.src = gallery_images[gallery_index]; 
  }
  else {
    gallery_preload.src = gallery_default_image;
  }

  setTimeout("gallery_show_loading();", gallery_loading_timeout);
}

function gallery_show_image() {
  var image = document.getElementById("image");

  image.width = 0;
  image.height = 0;

  var gallery_max_width = image.parentNode.clientWidth;
  var gallery_max_height = image.parentNode.clientHeight - 16; /* leave room for caption */
  var maxScale = Math.min(gallery_max_width / this.width,
                              gallery_max_height / this.height);
  if (maxScale > 1 || gallery_full_size) maxScale = 1;

  image.width = this.width * maxScale;
  image.height = this.height * maxScale;
  image.src = this.src;
 
  var caption = document.getElementById("caption");

  if (gallery_captions[gallery_index]) {
    while (caption.firstChild) { caption.removeChild(caption.firstChild); }
    caption.appendChild(document.createTextNode(gallery_captions[gallery_index]));
  }
  else {
    while (caption.firstChild) { caption.removeChild(caption.firstChild); }
    if (gallery_default_caption) caption.appendChild(gallery_default_caption);
  }

  /* Start preloading the next and previous images. */
  new Image().src = gallery_images[(gallery_index + 1) % gallery_images.length];
  new Image().src = gallery_images[(gallery_index - 1 + gallery_images.length) % gallery_images.length];
}

function gallery_show_loading() {
  if (!gallery_preload.complete) {
    var image = document.getElementById("image");
    image.src = '';
    image.width = gallery_loading_image.width;
    image.height = gallery_loading_image.height;
    image.src = gallery_loading_image.src;
  }
}

function gallery_show_error() {
  var image = document.getElementById("image");
  if (gallery_error_image.complete && gallery_error_image.width > 0) {
    image.src = '';
    image.width = gallery_error_image.width;
    image.height = gallery_error_image.height;
    image.src = gallery_error_image.src;
  }
  else {
    image.src = '';
    image.width = 64;
    image.height = 64;
    image.src = '';
  }
}

function gallery_full_size_toggle() {
  if (gallery_full_size) {
    gallery_full_size = 0;
  }
  else {
    gallery_full_size = 1;
  }
  gallery_update();
}

function gallery_auto_next() {
  var form = document.getElementById("gallery_form");
  
  gallery_index += 1;
  gallery_update(); 

  var delay = parseInt(form.auto_delay.value) * 1000;
  if (isNaN(delay) || delay <= 0) {
    alert("Invalid delay time");
    form.auto_delay.value = "5";
    delay = 5000;
  }

  gallery_auto_timeout = setTimeout("gallery_auto_next()", delay);
}

function gallery_auto_start(form) {
  if (gallery_auto_timeout == -1) {
    gallery_auto_next();
  }
  if (form.auto_toggle) {
    form.auto_toggle.value = "Stop Slide Show";
  }
}

function gallery_auto_stop(form) {
  if (gallery_auto_timeout >= 0) {
    clearTimeout(gallery_auto_timeout);
  }
  gallery_auto_timeout = -1;
  if (form.auto_toggle) {
    form.auto_toggle.value = "Start Slide Show";
  }
}

function gallery_auto_toggle(form) {
  if (gallery_auto_timeout >= 0) {
    gallery_auto_stop(form);
  } 
  else {
    gallery_auto_start(form);
  }
}

