//------------------------------------------------------------------------------------------------
// Javascript voor diashow
// Javascript : cop. Reinier van der Neut, 2006 (voor de wijzigingen, verdere info zie hieronder)
//------------------------------------------------------------------------------------------------

/*================================================================================================*
 Javascript based on:
 - $Id: slideshow.js,v 1.16 2003/10/14 12:39:00 pat Exp $
 - Copyright 2000-2003 Patrick Fitzgerald
 - http://slideshow.barelyfitz.com/
 Modified by Reinier van der Neut, 2005/02/09, 2006/07/23, 2010/01/05  http://reisfoto.neutvd.nl
 - modified: allows only automatic playing
 - modified for slides shown without text captions
 - modified: an automatically running slideshow always loops back
 - modified: an automatically running slideshow cannot be stopped by action of the user 
 *===============================================================================================*/
  
// There are two objects defined in this file:
// "slide" - contains all the information for a single slide
// "slideshow" - consists of multiple slide objects and runs the slideshow



//==================================================
// slide object
//==================================================
function slide(src) {
  // This is the constructor function for the slide object.
  // It is called automatically when you create a new slide object.
  // For example:  s = new slide();
  // Image URL
  this.src = src;
  // Create an image object for the slide
  this.image = new Image();
  // Flag to tell when load() has already been called
  this.loaded = false;
  // This method loads the image for the slide
  this.load = function() {
    if (!this.loaded) {
      this.image.src = this.src;
      this.loaded = true;
    }
  }
}

//==================================================
// slideshow object
//==================================================
function slideshow( slideshowname ) {
  // This is the constructor function for the slideshow object.
  // It is called automatically when you create a new object.
  // For example:   ss = new slideshow("ss");
  // Argument "slideshowname" is required and must equal the object's name (due to function PlayCont)
  
  // Name of this object
  this.name = slideshowname;
  // Number of images to pre-fetch.
  // -1 = preload all images.
  //  0 = load each image as it is used.
  //  n = pre-fetch n images ahead of the current image.
  this.prefetch = 2;
  // IMAGE element on HTML page.
  this.image;
  // Milliseconds to pause between slides.
  this.timeout = 4000;
  // These are private variables
  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;
  this.playing = false;
  
  //--------------------------------------------------
  // Public methods
  //--------------------------------------------------
  this.add_slide = function(slide) {
    // Add a slide to the slideshow.
    // For example:
    // SLIDES1.add_slide(new slide("s1.jpg", "caption"))
    var i = this.slides.length;
    // Prefetch the slide image if necessary
    if (this.prefetch == -1) {slide.load();}
    this.slides[i] = slide;
  }

  //--------------------------------------------------
  this.play = function() {
    // This method starts/stops the slideshow.
    if (!this.playing) {
	  this.playing=true;
	  this.loop();
	} else {
	  this.stop();
    }
  }

  //--------------------------------------------------
  this.stop = function() {
    // This method stops the slideshow.
	this.playing=false;
    clearTimeout(this.timeoutid);
    this.timeoutid = 0;
  }

  //--------------------------------------------------
  this.playcont = function() {
    // This method continues the automatically running slideshow.
    // After the timeout, call this.loop()
    this.timeoutid = setTimeout( this.name + '.loop()', this.timeout);
  }

  //--------------------------------------------------
  this.loop = function() {
    // This method gets called automatically by a JavaScript timeout.
    // It advances to the next slide, then sets the next timeout.
	this.loopnext();
    this.playcont( );
  }

  //--------------------------------------------------
  this.loopnext = function() {
    // This method advances to the next slide at automatic playing
    if (this.current < this.slides.length - 1) {
      this.current++;
    } else {
      this.current = 0;
    }
    this.update();
  }

  //--------------------------------------------------
  this.update = function() {
    // This method updates the slideshow image on the page
    // Convenience variable for the current slide
    var slide = this.slides[ this.current ];
    // Load the slide image if necessary
    slide.load();
    // Update the image.
	if (browserIE()) {
		this.image.style.filter = 'progid:DXImageTransform.Microsoft.GradientWipe(duration=1)';
		this.image.filters[0].Apply();
	}
    this.image.src = slide.image.src;
	if (browserIE()) { this.image.filters[0].Play() };
    // Do we need to pre-fetch images?
    if (this.prefetch > 0) {
      var next, prev, count;
      // Pre-fetch the next slide image(s)
      next = this.current;
      prev = this.current;
      count = 0;
      do {
        // Get the next and previous slide number
        // Loop past the ends of the slideshow if necessary
        if (++next >= this.slides.length) next = 0;
        if (--prev < 0) prev = this.slides.length - 1;
        // Preload the slide image
        this.slides[next].load();
        this.slides[prev].load();
        // Keep going until we have fetched
        // the designated number of slides
      } while (++count < this.prefetch);
    }
  }
 
}


// Onderzoeken of browser IE is ivm. afbeeldingsovergang
function browserIE() {
    if (window.navigator.appName.indexOf('Microsoft') >= 0) {
      return true;
    } else {
      return false;
    }
}



