
var photos = new Array();
var photo_index = -1;
var photo_delay = 10000;
var fade_fromOpacity = 0;
var fade_toOpacity = 100;
var fade_time = 1000;
var fade_fps = 12;

function init_cyclephoto(id)
{
  var parentObj = document.getElementById(id);

  if (parentObj)
  {
    findPhotos(parentObj);
    if (photos.length > 0) cycle_photo();
  }
}

function findPhotos(parentObj)
{
  for (var i = 0; i < parentObj.childNodes.length; i++)
  {
    if (parentObj.childNodes[i].tagName == "DIV")
      photos[photos.length] = parentObj.childNodes[i].id;
  }
}

function cycle_photo()
{
  if (photo_index >= 0)
  {
    var obj = document.getElementById(photos[photo_index]);
    obj.style.display = 'none';
    obj.style.visibility = 'hidden';
  }

  photo_index++;

  if (photo_index >= photos.length) photo_index = 0;
  if (photos.length > 0)
  {
    var obj = document.getElementById(photos[photo_index]);
    obj.style.display = 'inline';
    obj.style.visibility = 'visible';

    FadeOpacity(photos[photo_index], fade_fromOpacity, fade_toOpacity, fade_time, fade_fps, false);
    setTimeout("cycle_photo()", photo_delay);
  }
}

function SetOpacity(elem, opacityAsInt)
{
  var opacityAsDecimal = opacityAsInt;
    
  if (opacityAsInt > 100)
    opacityAsInt = opacityAsDecimal = 100; 
  else if (opacityAsInt < 0)
    opacityAsInt = opacityAsDecimal = 0; 

  opacityAsDecimal /= 100;
  if (opacityAsInt < 1)
    opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0

  elem.style.opacity = (opacityAsDecimal);
  if (elem.style.filter)
    elem.style.filter = "alpha(opacity=" + opacityAsInt + ")";
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep, hideAfter)
{
  var obj = document.getElementById(elemId);
  SetOpacity(obj, Math.round(parseInt(fromOpacity) + (delta * stepNum)));

  // set child image opacity
  SetOpacity(obj.childNodes[0].childNodes[0], Math.round(parseInt(fromOpacity) + (delta * stepNum)));

  if (stepNum < steps)
    setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum + 1) +
      ", " + steps + ", " + fromOpacity + ", " +
      delta + ", " + timePerStep + ", " + hideAfter + ");", 
      timePerStep);
  else if (hideAfter)
  {
    obj.style.visibility = "hidden";
    obj.style.display = "none";
  }
}

function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps, hideAfter)
{
  var steps = Math.ceil(fps * (time / 1000));
  var delta = (toOpacity - fromOpacity) / steps;

  FadeOpacityStep(elemId, 0, steps, fromOpacity,
    delta, (time / steps), hideAfter);
}
