// By Kyrre Amanaborg (kyrre$innovitae.no)


// The image item stores the url to the image,
// and is later used to store the Image used to preload the image.
// Until the image is preloaded, the value isLoaded is set to false.
function imageItem(slideURL) {
   this.src = slideURL;
   this.isLoaded = false;
   // This function sets the parameter image item's isLoaded value to true.
   this.setAsLoaded = function() {
        this.isLoaded = true;
   } 
}


function slideshow(slideshowName, locationName) {
   // The interval between slides.
   this.interval = 2500;
   this.timeout = 0;
   this.locationName = locationName;
   this.name = slideshowName;
   //this.isPlaying = false; // unused?

   // The image array.
   this.imageArray = new Array();
   // A pointer to the active spot in the image array.
   this.currentImage = 0;
   timeoutid = 0;
   
   // This function is used to add new slides to the slideshow (i.e
   // image items to the image array.   
   this.addSlide = function(slide_url) { 
        // A new image item is added to the image array.    
      if( isNaN(this.currentImage) ){
          if(this.imageArray.length == 0)
          {
              this.currentImage = 0;
              this.imageArray = new Array();
          }
          else {
              this.currentImage = this.imageArray.length + 1;
          }
          
      }
      this.imageArray[this.currentImage] = new imageItem(slide_url);
      // Preloads the first two images that are added to the slideshow.
      if(this.currentImage<0) {
          // The image item is given a Image.
         this.imageArray[this.currentImage].image_item = new Image();
         // The Image is given a source url, thus initiating the loading
         // of the image.
         this.imageArray[this.currentImage].image_item.src =
                           this.imageArray[this.currentImage].src;
         // Once the image is loaded, the image item's isLoaded value
         // is set to true.                           
         this.imageArray[this.currentImage].image_item.onLoad = 
         this.imageArray[this.currentImage].setAsLoaded();
     }
     // Points to the next available spot in the image array.
      this.currentImage++;
   }
   
   // This function begins the process of switching slides.
   this.play = function(timeout) {
        // First stop the slideshow, if already running.
      this.stop();
  
      // If the timeout argument was specified (optional)
      // then make it the new default
      if (timeout) {
          this.interval = timeout;
      }

      // After the timeout, call for the switching of the slides.
      this.timeout = setTimeout(this.name + ".switchSlides()", this.interval);
   }

   
   // This method stops the slideshow.
   this.stop = function() {
      if (this.timeout != 0) {
          clearTimeout(this.timeout);
          this.timeout = 0;
      }
   }
   
   // This method switces the slides.
   // It advances to the next slide, then sets the next timeout.
   // If the next slide image has not completed loading yet,
   // then do not advance to the next slide yet.
   this.switchSlides = function() {
        // First the slideshow is paused.
      this.stop();

      // Make sure the next slide image has finished loading
      //if (this.currentImage < this.imageArray.length - 1) {
          //next_slide = this.imageArray[this.currentImage + 1]; 
          next_slide = (this.currentImage+1) % this.imageArray.length;
          // Check to see if the next slide is loaded.
          if (!next_slide.isLoaded) {
              // If so, the slides are switched.
              this.nextImage();
          }
      //} else { // we're at the last slide
      //  this.nextImage();              
      //}
    
      // Start the precess for the next swicth.
      this.play( );
   }

   // Displays the next slide in the slideshow.
   this.nextImage = function() {
      // Retreive the url.    
      var new_image_url = this.getNextImageURL();
      // Display the image.
      document[this.locationName].src = new_image_url;   
      // Preload next slide.  
      this.preloadNextImage();
   }

   // This function gets the next image url in the image array.
   this.getNextImageURL = function() {
        // Moves the pointer to the next slide.
      this.currentImage = (this.currentImage+1) % this.imageArray.length;
      // Extracts and returns the url for that slide.
      var new_image_url = this.imageArray[this.currentImage].src;      
      return(new_image_url);
      
   }
   
   // Displays the previous slide in the slideshow.
   this.prevImage = function() {  
      // Retreive the url.    
      var prev_image_url = this.getPrevImageURL();
      // Dispaly the image.
      document[this.locationName].src = prev_image_url;
   }

   // This function gets the previous image url in the image array.
   this.getPrevImageURL = function() {
        // Moves the pointer to the previous slide.
      this.currentImage = (this.currentImage-1) % this.imageArray.length;
      // If the pointer is to before the beginning of image array,
      // we move it to the end of it instead.
      if(this.currentImage == -1) this.currentImage = this.imageArray.length-1;
      // Extracts and returns the url for that slide.
      var prev_image_url = this.imageArray[this.currentImage].src;
      return(prev_image_url);
   }
   
   // This function preloads the next slide, if it's not already loaded.
   this.preloadNextImage = function() {
        // Set the pointer to the next slide (or back to the beginning).
      var nextImage = (this.currentImage+1) % this.imageArray.length;
      // Check to see if the image exists and is already loaded.
      if(this.imageArray[nextImage]!=null
         && !this.imageArray[nextImage].isLoaded) {
        // Create a new Image for the image_item.
        this.imageArray[nextImage].image_item = new Image();
        // Give the Image an url, thus beginning the loading process.
        this.imageArray[nextImage].image_item.src = this.imageArray[nextImage].src;
        // As soon as the image is loaded, set the image_item's isLoaded value
        // to true.
        this.imageArray[nextImage].image_item.onLoad = 
         this.imageArray[nextImage].setAsLoaded();
      }
   }

}


