// 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;
  
  //wxx  
  var targetString = "_self";
  var realUrl = slideUrl;
  if (slideUrl.indexOf("new_window_") > -1)
  {
     //alert("here");
     targetString = "_blank";
     realUrl = slideUrl.substr(11);
  }
  this.slideUrl = realUrl;
  this.target = targetString;
  
  this.myclass = "colourbox";
  //this.myclass = "";
}

function ImageSlideShow(key, interval) {
  this.key = key;
  this.idx = 0;
  this.timer = null;
  this.interval = interval;
  this.slides = 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.updateDiv = function() {
    var fadeTime = IWOV_FX.fadeOut(this.key+"_div", 25, 10);
    //alert(this.key+"_div");
    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];
      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 "
          + "class='"+slide.myclass+"' "
          + "href='"+slide.slideUrl+"' "
          + "target='"+slide.target+"' "
          + "><img border='0' "
          + "title='"+slide.title+" ' "
          + "src='"+slide.image.src+"' "
          + "/></a>";
          
          //alert("now1:"+d.innerHTML);
      }
      else {
        // Replace attributes in the existing <a><img/></a> content.
        
        //a.class = "colorbox";
        a.setAttribute("class", slide.myclass);
        a.href = slide.slideUrl;
        a.target = slide.target;
        img.title = slide.title;
        img.src = slide.image.src;
        
        //alert("now2:"+d.innerHTML);
      }
      document.getElementById(this.key+"_buttonDiv").style.visibility = "inherit";
      //alert(d.innerHTML);
    } catch (noSlidesException) {alert("no such slide");}
  }

  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();
  }
  
  //wxx  
  this.clickSpecific  = function(myInx) {
    this.goSpecific(myInx);
    this.restartTimer();
  }
  
  this.goSpecific = function(myInx) {
    this.idx = myInx;
    this.updateDiv();
  }

}

