
var Crossfader = Class.create();
Crossfader.prototype = {
  interval: 45, // pause between fades (in seconds)
  
  images: [],
  currentImageIndex: 0,
  lastImage: null,
  
  initialize: function(){
    this.container = $('crossfade');
	
	// the first map is already on the page
	this.images.push(this.container.down());
	
    for(var i = 0, arg; arg = arguments[i]; i++)
	{
      	var html = '<img src="' + arg + '" class="padded" alt="AIDS.gov" />';
	  	new Insertion.Top(this.container, html);
		this.images.push(this.container.down().hide());
    }
    
    // set up an interval
    setInterval(this.poll.bind(this), this.interval*1000);
  },
  
  // timer function
  poll: function(){
    // reset to beginning if at end
    if (++this.currentImageIndex >= this.images.length) this.currentImageIndex = 0;
    this.reveal(this.images[this.currentImageIndex]);
  },
  
  reveal: function(image){
    // before revealing, we must hide if applicable
    if (this.lastImage){ this.fade(this.lastImage); }

    image.style.zIndex = 2;
    new Effect.Appear(image, { duration: 2.0 });

    this.lastImage = image;
  },
  
  fade: function(image){
      image.style.zIndex = 1;
      new Effect.Fade(image, { duration: 2.0 });
  }
}

Event.observe(window, 'load', function() {
		if($('crossfade'))
		{
	new Crossfader('images/map_02.gif', 'images/map_03.gif', 'images/map_04.gif', 'images/map_05.gif');
		}
});