/*
  Simple "slideshow" and "scrolling headlines" using prototype and scriptaculous.
  v1.0 May 10, 2007
  Based on original done by R.W. van 't Veer
  See: http://blog.remvee.net/post/17
*/

function Slideshow(slideshow, timeout) {
  this.slides = [];
  var nl = $(slideshow).getElementsByTagName('div');
  for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'slide')) {
      this.slides.push(nl[i]);
    }
  }
  this.timeout = timeout;
  this.current = 0;

  for (var i = 0; i < this.slides.length; i++) {
    this.slides[i].style.zIndex = this.slides.length - i;
  }

  Element.show(slideshow);
  setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
}

Slideshow.prototype = {
  next: function() {
    for (var i = 0; i < this.slides.length; i++) {
      var slide = this.slides[(this.current + i) % this.slides.length];
      slide.style.zIndex = this.slides.length - i;
    }

    Effect.Fade(this.slides[this.current], {
      duration: 2.0,
      afterFinish: function(effect) {
        effect.element.style.zIndex = 0;
        Element.show(effect.element);
        Element.setOpacity(effect.element, 1);
      }
    });
    
    this.current = (this.current + 1) % this.slides.length;
    setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
  }
}

function Headlines(headlinesDiv, dwellT, gapT) {
  this.headline = [];
  var nl = $(headlinesDiv).getElementsByTagName('div');
  for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'headline')) {
      this.headline.push(nl[i]);
    }
  }
  this.dwell = dwellT;
  this.gap = ( gapT + dwellT ) * 1000;
  this.current = 0;

  Element.show(headlinesDiv);
  setTimeout((function(){this.next();}).bind(this), 3000);  // sets delay for initial headline drop
}

Headlines.prototype = {
  next: function() {

    Effect.SlideDown(this.headline[this.current], { queue: {position:'front', scope:'headlinescope'}});
    Effect.DropOut(this.headline[this.current], { delay: this.dwell , queue:   {scope:'headlinescope'}});
    
    this.current = (this.current + 1) % this.headline.length;
    setTimeout((function(){this.next();}).bind(this), this.gap + 500);
  }
}
