/*-------------------------------------------
Slideshow functionality
---------------------------------------------

Code to enable the rotating news slideshows. To alter the slide timing,
change the "slide_delay" variable.

Author: Ben Sturmfels, Boojum, January 2009

------------------------------------------*/

/*

Changes by Marty Friedel
February 9 2009 (Monday)

Reason for change: Bush Fires "slide" (position 1) to remain on screen
Changes have been made to turn off the auto play on initialisation.

See:
  this.slideshow_init = function() {...}
Changes have been commented and documented

*/

function Slideshow() {
  var slide_delay = 8; // seconds between slides
  var pause_button_content = '<img src="/images/template/icon-slideshow-pause.gif" alt="" />';
  var play_button_content = '<img src="/images/template/icon-slideshow-play.gif" alt="" />';

  var cur_slide = 0;
  var auto_play;
  var auto_play_status = "paused";

  var buttons = $$("#jump-buttons span.button");
  var slides = $$(".slideshow .slide");
 
  this.slideshow_init = function() {
    //slides.each(function(s){s.hide();});
    slides.each(function(s) {
		//s.setStyle({position: 'absolute', top: '0', left: '0'}); 
		s.hide(); 
		s.removeClassName('defaultOFF');
	});
    slides[cur_slide].show();
    
    buttons[cur_slide].addClassName('current');
	
	// CHANGE 9 FEB 2009 - BUSH FIRES
	// The following two lines have been commented out to stop the automation
	// The user can trigger the automation by hitting play
    auto_play = new PeriodicalExecuter(slideshow_rotate, slide_delay);
    auto_play_status = "playing";
	
	// CHANGE 9 FEB 2009 - BUSH FIRES
	// The following line has been added to make the 'pause' button (on by default)
	// appear as a 'play' button given that automation has been turned off
	//$("button-pause").update(play_button_content);
	
    $('slideshow-controls').setStyle({display: 'block'});
  };

  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');

      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);
