
Event.observe(window, 'load', init);

var DELAY = 6000;

function init()
{
  $$('.slideshow').each(function(e){
    startSlideshow(e);
  });
}


function startSlideshow(img)
{
  setTimeout(switchSlides.bind(img, img, 1), DELAY);
}


//takes in the image object to update and the current active image array key
function switchSlides(img, key)
{
  var dir = img.src.match(/uploads\/([^\/]+)/)[1];
  try
  {
    key += 1;
    var next_pic = eval( dir + '_' + (key));
  }
  catch(ex)
  {
    key = 1;
  }
  var next_pic = eval( dir + '_' + (key));
  var overlay = img.cloneNode(true);
  Element.addClassName(overlay, 'overlay');
  //Element.setStyle(overlay, { left: img.offsetLeft + 'px', top: img.offsetTop + 'px' });
  Element.setOpacity(overlay, 0);
  img.parentNode.appendChild(overlay);
  overlay.src = next_pic;
  new Effect.Opacity(overlay, {duration: 2, from: 0, to: 1.0, afterFinish:
    function(){ img.src = next_pic; Element.remove(overlay); setTimeout(switchSlides.bind(img, img, key), DELAY); } 
  });

}



