/*
  file:   js/slideshow.js
  vers:   1.1 (2010-02-13)
  desc:   slideshow handling to initialize and change the
          slideshow images

  copyright 2010 Geoffrey Whaite, all rights reserved
*/


var ss_timeout = 7500;        //  slideshow image switch timeout in mS
var ss_retry_timeout = 1000;  //  slideshow timeout to retry when no image available
var ss_current_index = -1;    //  slideshow current image index
var ss_max_height = 0;        //  maximum scaled image height height

var ss_img_width = 350;   //  slideshow image width


/*
  ss_init():  initializes the slideshow by preloading images
              and starting the initial switch timeout
*/
function ss_init()
{
  var t, i, j, w, h, l = slideshow_data.length;
  for (i=0; i < l; i++)
  {
    j = Math.floor(Math.random()*l);
    if (j == l)  j--;
    if (j != i)
    {
      t = slideshow_data[i];
      slideshow_data[i] = slideshow_data[j];
      slideshow_data[j] = t;
    }
  }
  ss_max_height = 0;
  for (i=0; i < l; i++)
  {
    slideshow_data[i].img = document.createElement("img");
    slideshow_data[i].img.src = slideshow_data[i].src;

    w = slideshow_data[i].width;  h = slideshow_data[i].height;
    if (w > ss_img_width)
    {
      h = Math.floor(h * ss_img_width / w);
      w = ss_img_width;
    }
    slideshow_data[i].w = w;  slideshow_data[i].h = h;
    if (h > ss_max_height)  ss_max_height = h;
  } // for (i...)
  ss_current_index = -1;
} // ss_init()

ss_init();

/*
  ss_start():
*/
function ss_start()
{
  var td = document.getElementById("td_ss");
  if (td)
  {
    var h = ss_max_height + 10;
    var div = document.getElementById("div_ss");
    if (div)  h += div.offsetHeight;
    td.style.height = h + "px";
  }
  setTimeout("ss_next()", ss_retry_timeout);
} // ss_start()


/*
  ss_next():
*/
function ss_next()
{
  var div_element = document.getElementById("div_ss"),
      img_element = document.getElementById("img_ss"),
      i, timeout = ss_retry_timeout;
  if (img_element)
  {
    for (i=0; i < slideshow_data.length; i++)
    {
      if (++ss_current_index >= slideshow_data.length)  ss_current_index -= slideshow_data.length;

      if (slideshow_data[ss_current_index].width == slideshow_data[ss_current_index].img.width  &&
          slideshow_data[ss_current_index].height == slideshow_data[ss_current_index].img.height)
      {
        img_element.src = slideshow_data[ss_current_index].img.src;
        img_element.width = slideshow_data[ss_current_index].w;
        img_element.height = slideshow_data[ss_current_index].h;
        if (div_element)
        {
          var txt = slideshow_data[ss_current_index].title;
          if (txt == "")  txt = "\240";
          var new_node = document.createTextNode(txt),
              first_child = div_element.firstChild;
          if (first_child)
            div_element.replaceChild(new_node, first_child);
          else
            div_element.appendChild(new_node);
        }
        timeout = ss_timeout;
        break;
      } // if (w==...)
    } // for (i...)
  } // if (img_element)
  setTimeout("ss_next()", timeout);
} // ss_next()

