// JavaScript code for the ImageSlideShow Component

var imageSlideShows = new Array();

// Internal data structure used for each Slide
function ImageSlide(imageUrl, title, slideUrl) {
  this.title = title;
  this.slideUrl = slideUrl;
  this.image = new Image();
  this.image.src = imageUrl;
}

function ImageSlideShow(key, interval) {
  this.key = key;
  this.idx = 0;
  this.timer = null;
  this.interval = interval;
  this.slides = new Array();
  this.btnImg = new Array();
  this.btnImg2 = new Array();
  this.pauseLabel = "||";
  this.playLabel = ">";
  if (!imageSlideShows[key]) {imageSlideShows[key] = this};


  this.addSlide = function (slideImg, slideTitle, slideURL) {
    this.slides[this.slides.length] = new ImageSlide(slideImg, slideTitle, slideURL);
  }
  
  this.addButtonImg = function (img1, img2) {
	this.btnImg[this.slides.length] = img1;
	this.btnImg2[this.slides.length] = img2;
  }

  this.updateDiv = function() {
    var fadeTime = IWOV_FX.fadeOut(this.key+"_div", 25, 10);
    setTimeout("imageSlideShows['"+key+"'].setSlide()", fadeTime);
    setTimeout("IWOV_FX.fadeIn('"+this.key+"_div', 25, 10)", fadeTime);
  }

  this.setSlide = function() {
    try {
      var slide = this.slides[this.idx];
      var d = document.getElementById(this.key+"_div");
      var a = d.getElementsByTagName("a")[0];
      var img = d.getElementsByTagName("img")[0];
      var bookTarget = d.getElementsByTagName("input")[0];
      if (!a || !img) {
        // Establish the initial content of the div, an <a><img/></a> structure where src=
        // and title= attributes will be replaced from now on.
        d.innerHTML = "<a "
          + "href='"+slide.slideUrl+"' "
          + "><img border='0' width='600' height='300'"
          + "title='"+slide.title+" ' "
          + "src='"+slide.image.src+"' "
          + "/></a><input type='hidden' name='bookTarget' value='"+slide.slideUrl+"'/>";
      }
      else {
        // Replace attributes in the existing <a><img/></a> content.
        a.href = slide.slideUrl;
        img.title = slide.title;
        img.src = slide.image.src;
		bookTarget.value = slide.slideUrl;
		var buttons = document.getElementById('buttons');
		var imgs = buttons.getElementsByTagName("img");
		for (var i = 0; i < this.slides.length; i++) {
			if (i == this.idx) {
				imgs[i].src = this.btnImg2[i];
			} else {
				imgs[i].src = this.btnImg[i];
			}
		}
      }
      document.getElementById(this.key+"_buttonDiv").style.visibility = "inherit";
    } catch (noSlidesException) {}
  }

  this.goNext = function() {
    if (this.idx < this.slides.length-1) {
      this.idx++;
    } else {
      this.idx = 0;
    }
    this.updateDiv();
  }

  this.goPrev = function() {
    if (this.idx > 0) {
      this.idx--;
    } else {
      this.idx = this.slides.length-1;
    }
    this.updateDiv();
  }

  this.setPauseBtnLabels = function(pauseLabel, playLabel) {
    this.pauseLabel = pauseLabel;
    this.playLabel = playLabel;
  }
  
  this.togglePause = function(pauseBtn, isGraphic) {
    if (this.timer!=null) {
      // pausing
      clearInterval(this.timer);
      this.timer = null;
      if (pauseBtn) {
        pauseBtn.title = pauseBtn.getAttribute("pausedTitle");
        if (isGraphic) {
          pauseBtn.style.backgroundImage = "url('" + this.playLabel +"')";
        } else {
          pauseBtn.innerHTML = this.playLabel;
        }
      }
    } else {
      // starting
      this.timer = setInterval("imageSlideShows['"+this.key+"'].goNext()", this.interval*1000);
      if (pauseBtn) {
        this.goNext();
        pauseBtn.title = pauseBtn.getAttribute("playingTitle");
        if (isGraphic) {
          pauseBtn.style.backgroundImage =  "url('" + this.pauseLabel +"')";
        } else {
          pauseBtn.innerHTML = this.pauseLabel;
        }
      }
    }
  }

  this.restartTimer = function() {
    if (this.timer!=null) {
      clearInterval(this.timer);
      this.timer = setInterval("imageSlideShows['"+this.key+"'].goNext()", this.interval*1000);
    }
  }

  this.clickNext = function() {
    this.goNext();
    this.restartTimer();
  }

  this.clickPrev = function() {
    this.goPrev();
    this.restartTimer();
  }

}

