/*
 *******************************************************************
 *******************************************************************
 ***                      Fading Slideshow                       ***
 ***                   By Aurelien Marchand                      ***
 ***                  artaxerxes2@iname.com                      ***
 ***  Licence: Creative Commons with attribution and ShareAlike  ***
 ***            Known to work on: IE6, FF2, Safari3              ***
 *******************************************************************
 *******************************************************************
 */

var fadein = function(opac,i) {
  var elnt=document.getElementById('Slideshow2');
  var passed = parseInt(opac);
  var newOpac = parseInt(passed+10);
  if ( newOpac <= 90 ) {
    elnt.style.opacity = '.'+newOpac;
    elnt.style.filter = "alpha(opacity="+newOpac+")";
    setTimeout("fadein('"+newOpac+"','"+i+"')",50);
  } else {
    elnt.style.opacity = '1';
    elnt.style.filter = "alpha(opacity=100)";
    // Slideshow2 is in front now, replace the back image
    document.getElementById("Slideshow1").style.backgroundImage = "url(" + document.my_images[i].src + ")";
//    document.getElementById("SlidePlaceholder").alt = document.my_images[(i + 1) % document.my_images.length].alt;
  }
};

var fadeout = function(opac,i) {
  var elnt=document.getElementById('Slideshow2');
  var passed = parseInt(opac);
  var newOpac = parseInt(passed-10);
  if ( newOpac >= 10 ) {
    elnt.style.opacity = '.'+newOpac;
    elnt.style.filter = "alpha(opacity="+newOpac+")";
    setTimeout("fadeout('"+newOpac+"','"+i+"')",50);
  } else {
    elnt.style.opacity = '0';
    elnt.style.filter = "alpha(opacity=0)";
    // Slideshow2 is not showing anymore, replace it with the next image
    document.getElementById("Slideshow2").style.backgroundImage = "url(" + document.my_images[i].src + ")";
//    document.getElementById("SlidePlaceholder").alt = document.my_images[(i + 1) % document.my_images.length].alt;
  }
};

var fadePlaceholder = function(opac){
  var elnt=document.getElementById('SlidePlaceholder');
  var passed = parseInt(opac);
  var newOpac = parseInt(passed-10);
  if ( newOpac >= 10 ) {
    elnt.style.opacity = '.'+newOpac;
    elnt.style.filter = "alpha(opacity="+newOpac+")";
    setTimeout("fadePlaceholder('"+newOpac+"')",50);
  } else {
    elnt.style.opacity = '0';
    elnt.style.filter = "alpha(opacity=0)";
  }
};
  
/*
var slideshow_debug = function(i,txt){
  var d = document.getElementById("slideshow_debug");
  if(d){
    d.innerHTML += "<br />At " + Date() + ", i = " + i + " in " + txt;
  }
}
*/

document.my_images = new Array();

function preloadimages(){
  m = document.my_images;
  m[0] = new Image();
  m[0].src = "images/calgary.png";
  m[0].alt = "Calgary";
  m[1] = new Image();
  m[1].src = "images/vancouver.png";
  m[1].alt = "Vancouver" ;
  m[2] = new Image();
  m[2].src = "images/toronto.png";
  m[2].alt = "Toronto";
  m[3] = new Image();
  m[3].src = "images/montreal.png";
  m[3].alt = "Montreal";
  setTimeout("prepSlideshow()",5000);
}

function prepSlideshow(){
  document.getElementById("Slideshow1").style.backgroundImage = "url(" + document.my_images[1].src + ")"; 
  document.getElementById("Slideshow2").style.backgroundImage = "url(" + document.my_images[0].src + ")";
  fadePlaceholder(100);
  var ss = setTimeout("evenSlideshow(0)",8000);
  // uncomment next line to stop slideshow when clicking on the placeholder
  // document.getElementById("SlidePlaceholder").onclick=function a(){ clearTimeout(ss)};
}

function evenSlideshow(i){
//  slideshow_debug(public_i,'evenSlideshow');
  if(i >= document.my_images.length) i = 0;
  fadeout(100,public_i);
  public_i = ( public_i + 1 ) % document.my_images.length;
  setTimeout("oddSlideshow(" + (i + 1) + ")",8000);
}

function oddSlideshow(i){
//  slideshow_debug(public_i,'oddSlideshow');
  if(i >= document.my_images.length) i = 1;
  fadein(0,public_i);
  public_i = ( public_i + 1 ) % document.my_images.length;
  setTimeout("evenSlideshow(" + (i + 1) + ")",8000);
}

var public_i = 0;
/* this overrides any previously set body->onload="..." functions */
window.onload = preloadimages;
