/*-------------------------------------------
Slideshow functionality
---------------------------------------------

Code to enable the rotating news slideshows. To alter the slide timing,
change the "slide_delay" variable.

Original author: Ben Sturmfels, Boojum, January 2009

------------------------------------------*/

function Slideshow() {
  var slide_delay = 12; // seconds between slides
  var pause_button_content = '<img src="image/icon-slideshow-pause.png" alt="" />';
  var play_button_content = '<img src="image/icon-slideshow-play.png" alt="" />';

  var cur_slide = 0;
  var auto_play;
  var auto_play_status = "paused";

  var buttons = $$("#jump-buttons span.button");
  var slides = $$(".slideshow .slide");
  var text = $$(".slideshow .text");
//  var left_pads = $$(".slideshow .left-pad")
  var slide_buttons = $$("#slideshow-controls span.button");
 
  this.slideshow_init = function() {
    slides.each(function(s){s.hide();});
    slides[cur_slide].show();
    
    buttons[cur_slide].addClassName('current');
    auto_play = new PeriodicalExecuter(slideshow_rotate, slide_delay);
    auto_play_status = "playing";
    $('slideshow-controls').setStyle({display: 'inline'});

  };

  this.slideshow_jump = function(c) {
    /* Jump to a particular slide using an index starting from one. Also
       pause the auto-play. */
    if (auto_play_status == "playing") {
      slideshow_pause();
    }
    slideshow_switch(cur_slide, (c - 1));
  };

  this.slideshow_next = function() {
    if (cur_slide + 1 < slides.length) {
      slideshow_pause();
      slideshow_switch(cur_slide, cur_slide + 1);
    }
  };

  this.slideshow_prev = function() {
    if (cur_slide - 1 >= 0) {
      slideshow_pause();
      slideshow_switch(cur_slide, cur_slide - 1);
    }
  };

  this.slideshow_play_pause = function() {
    if (auto_play_status == "playing") {
      slideshow_pause();
    }
    else {
      slideshow_play();
    }
  };

  var slideshow_rotate = function() {
    slideshow_switch(cur_slide, 
                          (cur_slide + 1) % slides.length);
  };

  var slideshow_pause = function() {
    auto_play.stop();
    auto_play_status = "paused";
    $("button-pause").update(play_button_content);
  };

  var slideshow_play = function() {
    slideshow_rotate();
    $("button-pause").update(pause_button_content);
    auto_play_status = "playing";
    auto_play = new PeriodicalExecuter(slideshow_rotate, slide_delay);
  };

  var slideshow_switch = function(c,d) {
    /* Switch the slides and update the status information. */
    if (c != d) {
      //slides[c].hide();
      //slides[d].show();
      Effect.Fade(slides[c], { duration: 0.2 });
      Effect.Appear(slides[d], { duration: 0.2 });

      buttons[c].removeClassName('current');
      buttons[d].addClassName('current');
      //hemang - added initially to allow dynamic color change of text and left padding
      //text[d].setStyle({background: slides[d].getStyle('background-color')});
      //left_pads[0].setStyle({background: slides[d].getStyle('background-color')});
     // $('slideshow-controls').setStyle( { background: slides[d].getStyle('background-color') } );
      cur_slide = d;
    }
  };
}

var addLoadEvent = function(func) {
  /* Chain onload functions. Taken from SuperSleight.
     24ways.org/2007/supersleight-transparent-png-in-ie6
  */
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
};

function slideshow_init() {
  slideshow = new Slideshow;
  slideshow.slideshow_init();
}

//window.addEventListener("load", slideshow_init, false);
//window.onload = slideshow_init;
addLoadEvent(slideshow_init);
