function Slide(src) {
  this.src = src;

  if (document.images)
  {
    this.image = new Image();
  }

  this.loaded = false;

  this.load = function()
  {
    if (!document.images)
    {
    	return;
    }

    if (!this.loaded) {
      this.image.src = this.src;
      this.loaded = true;
    }
  }
}

function SlideShow( slideshowname )
{
  this.name = slideshowname;
  this.repeat = true;
  this.prefetch =3;
  this.image;	//to be specified by the HTML page after the construction of the image object.
  this.timeout = 4000;

  this.slides = new Array();
  this.current = 0;
  this.timeoutid = 0;
  this.thumbnailInstance = null;
  this.prefix = '';
  this.suffix = '';
  this.startingSlide=0;
  this.userActioned=false;

  this.initImagePath = function(prefix, suffix)
  {
  	this.prefix = prefix;
  	this.suffix = suffix;
  }
  
  this.addSlide = function(slide)
  {
    var i = this.slides.length;
  
    if (this.prefetch == -1) {
      slide.load();
    }

    this.slides[i] = slide;
  }

  this.addSlideAided = function (basename)
  {
  	this.addSlide(new Slide(this.prefix + basename + this.suffix));
  }

  this.addSlideAndThumbnail = function (basename)
  {
  	this.addSlideAided(basename);
  	tn.addThumbnail(basename);
  }

  this.play = function(timeout)
  {
    this.pause();
	this.startingSlide = this.current;  
    if (timeout) {
      this.timeout = timeout;
    }
  
    this.loop();
  }

  this.pause = function()
  {
    if (this.timeoutid != 0)
    {
	  clearTimeout(this.timeoutid);
      this.timeoutid = 0;
    }
  }


  this.update = function()
  {
  	this.userActioned = true;
  	this.updateInternal(0.5);
  }

  this.quickUpdate = function()
  {
  	this.userActioned = true;
  	this.updateInternal(0.0);
  }

  this.updateInternal = function(duration)
  {
    if (! this.validImage())
    	return;
  
    var slide = this.slides[ this.current ];

    var dofilter = false;
    if (this.image &&
        typeof this.image.filters != 'undefined' &&
        typeof this.image.filters[0] != 'undefined') {

      dofilter = true;

    }

    slide.load();
  
  	if (dofilter)
  	{
		//if (slide.filter &&
		//	this.image.style &&
		//	this.image.style.filter)
		//{
		//	this.image.style.filter = slide.filter;
		//}
		this.image.filters[0].apply();
    }

    this.image.src = slide.image.src;

	if (this.thumbnailInstance)
		this.thumbnailInstance.mouseOver(this.current);

    if (dofilter)
    {
      this.image.filters[0].Duration=duration;
      this.image.filters[0].play();
    }
    if (this.prefetch > 0)
    {
	  var next, prev, count;

      next = this.current;
      prev = this.current;
      count = 0;
      do {

        if (++next >= this.slides.length)
        	next = 0;
        
        if (--prev < 0)
        	prev = this.slides.length - 1;
        
        this.slides[next].load();
        this.slides[prev].load();

      } while (++count < this.prefetch);
    }
  }

  this.thumbnailClicked = function (n)
  {
  	this.pause();
  	this.gotoSlide(n);
  }

  this.gotoSlide = function(n)
  {
    this.userActioned = true;
    if (n == -1) {
      n = this.slides.length - 1;
    }
  
    if (n < this.slides.length && n >= 0) {
      this.current = n;
    }
  
    this.quickUpdate();
  }

  this.next = function()
  {
	if (this.timeoutid != 0)
	{
		clearTimeout(this.timeoutid);
		this.startTimer();
	}
	
    if (this.current < this.slides.length - 1)
      this.current++;
    else
      this.current = 0;

    this.quickUpdate();
  }

  this.previous = function()
  {
    if (this.timeoutid != 0)
	{
		clearTimeout(this.timeoutid);
		this.startTimer();
	}

    if (this.current > 0)
      this.current--;
    else
      this.current = this.slides.length - 1;
  
    this.quickUpdate();
  }

  this.gotoNext = function()
  {
    if (this.current < this.slides.length - 1)
        this.current++;
    else 
        this.current = 0;
        
    this.update();
  }

  this.savePosition = function(cookiename) {
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
  
    document.cookie = cookiename + '=' + this.current;
  }

  this.restorePosition = function(cookiename)
  {
    if (!cookiename) {
      cookiename = this.name + '_slideshow';
    }
  
    var search = cookiename + "=";
  
    if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search);
      if (offset != -1) { 
        offset += search.length;
        end = document.cookie.indexOf(";", offset);
        if (end == -1) end = document.cookie.length;
        this.current = parseInt(unescape(document.cookie.substring(offset, end)));
        }
     }
  }

  this.loop = function()
  {
    if (this.current < this.slides.length - 1)
    {
      next_slide = this.slides[this.current + 1];
      if (next_slide.image.complete == null || next_slide.image.complete)
      {
        this.gotoNext();
      }
    }
    else
    {
      this.gotoNext();
    }
  
  	if (this.current != this.startingSlide || this.repeat)
  	{
		this.startTimer();
	}
  }

  this.startTimer = function()
  {
  	this.timeoutid = setTimeout( this.name + ".loop()", this.timeout);
  }

  this.selfStart = function(secondsBeforeStart)
  {
  	this.userActioned = false;
  	this.timeoutid = setTimeout( this.name + ".selfStartCallback()", 1000 * secondsBeforeStart);
  }

  this.selfStartCallback = function()
  {
  	if (!this.userActioned)
  	{
  		this.play();
  	}
  }

  this.validImage = function() {
    if (!this.image)
    {
      return false;
    }
    else {
      return true;
    }
  }
  
  this.getThumbnailInstance = function (thumbnail)
  {
  	this.thumbnailInstance = thumbnail;
  
  }
  
  this.reportCurrent = function ()
  {
 	return this.current; 
  }
}  


function Thumbnails(prefix)
{
	this.lastFocus = 0;
	this.imagesNormal = new Array();
	this.imagesOver = new Array();
	this.slideshowInstance = null;
	this.imageprefix = prefix; 
	this.prefix = '';
	this.suffix = '';
	this.normal = '';
	this.over = '';
	
	
	this.initImagePath = function (prefix,suffix,normal,over)
	{
		this.prefix = prefix;
		this.suffix = suffix;
		this.normal = normal;
		this.over = over;
	}
	
	this.addThumbnail = function (basename)
	{
		i = this.imagesNormal.length;

		tmp = new Image;
		tmp.src = this.prefix + basename + this.normal + this.suffix;
	    this.imagesNormal[i] = tmp;

		tmp = new Image;
		tmp.src = this.prefix + basename + this.over + this.suffix;
		this.imagesOver[i] = tmp;
	}

	this.mouseOver = function (thumbnailID)
	{
		this.mouseOut(this.lastFocus);
		document[this.imageprefix + thumbnailID].src = eval('this.imagesOver[' + thumbnailID + '].src');
		this.lastFocus = thumbnailID;
	}

	this.mouseOut = function (thumbnailID)
	{
		document[this.imageprefix + thumbnailID].src = eval('this.imagesNormal[' + thumbnailID + '].src');
	}
	
	this.getSlideShowInstance = function (slideshow)
	{
		this.slideshowInstance = slideshow;
	}
	
	this.display = function()
	{
		for (i = 0; i < this.imagesNormal.length; i++)
			this.mouseOut(i);
	}
	
	this.initControl = function ()
	{
		this.addThumbnail("play");
		this.addThumbnail("stop");
		this.addThumbnail("previous");
		this.addThumbnail("next");
	}	
}