// slideshow.js

var UNDEF='undefined';

var d = false; 
if (typeof console == UNDEF){ 
	window.console = new Object();
	window.console.log = function() {};
}

function dbg(txt)
{
	console.log(txt);
}


var slideshowArr = new Array();


function slideshowLoop(i){

	var s = slideshowArr[i];


	var nslide = s.slideArr[ s.current ];
	if (nslide.image.complete == false){
if(d)dbg('<li>still waiting for slide.'+s.current);
    		s.timer = setTimeout('slideshowLoop('+i+')',100)
		return;
	}

	if (!nslide.alreadyShown) s.advanceSlide(0);
	else s.advanceSlide();
		
	s.keepGoing();
}


//==================================================
function slide(src) 	// constructor
{

  this.src = src;	// url of image

  // Create an image object for the slide
  if (document.images) {
    this.image = new Image();
  }

  this.alreadyPreloaded = false;
  this.alreadyShown = false;


  //--------------------------------------------------
  this.preload = function(n) {

    if (document.images && !this.alreadyPreloaded){

if(d)dbg(' ... preloading slide.'+n);

      this.image.src = this.src;
      this.alreadyPreloaded = true;
    }
  }
}


function Slideshow( slideshowname ) 	// constructor
{

  // store this in global array - for easier handling of timeouts
  this.index = slideshowArr.length;
  this.id = slideshowArr.length;
  slideshowArr.push(this);

  this.name = slideshowname;

  this.repeat = true;	// auto-play 1-n-1-n

  // Number of images to pre-fetch.
  // -1 = preload as slide is added to slideshow
  //  0 = do not prefetch - load each image is it is used.
  //  n = preload n images ahead of the current image.
  this.preloadCnt = 1;

  this.imgEl;	// element shere image is to be placed
  this.txtEl;	// element where slide text is to be placed

  this.playSpeed = 3000; // msecs

  // Hook functions to be called before and after updating the slide
  // this.updateCb = function() { }	/* custom 

  // These are private variables
  this.slideArr = new Array();
  this.current = 0;
  this.timer = 0;

  //--------------------------------------------------
  // Public methods
  //--------------------------------------------------
  this.add_slide = function(slide) {

    // ss.add_slide(new slide("s1.jpg", "link.html"))
  
    var i = this.slideArr.length;
    this.slideArr[i] = slide;
  
    if (this.preloadCnt == -1) slide.preload(i);

  }


  //--------------------------------------------------
  this.start = function() {
	this.current = 0;
	this.getSlide(0).preload(0);
	this.keepGoing(100);
  }

  //--------------------------------------------------
  this.keepGoing = function(msec) {

    // make sure there are not timeouts in progress
    if (this.timer != 0) {
      clearTimeout(this.timer);
      this.timer = 0;
    }

    if (typeof msec == UNDEF) msec = this.playSpeed;
  
    // If the current slide has a custom playSpeed, use it;
    // otherwise use the default timeout
    if (typeof this.slideArr[ this.current ].playSpeed != UNDEF ) 
      msec = this.slideArr[ this.current ].playSpeed;

    this.timer = setTimeout('slideshowLoop('+this.index+')',msec)

if(d)dbg('<li>keepGoing new.tid='+this.timer + ' in ' + msec + ' msec.');
	

  }


  //--------------------------------------------------
  this.update = function() {


	this.imgEl       = document.getElementById('ss_img');
	this.titleEl     = document.getElementById('ss_title');
	this.legendEl    = document.getElementById('ss_legend');
	this.copyrightEl = document.getElementById('ss_copyright');
	this.hrefEl      = document.getElementById('ss_href');


	if (!this.imgEl){ 
if(d)dbg('<li>imgEl is not defined = cannot update');
		return;
	}

  
    	var slide = this.getSlide(this.current); 
    	var n = slide.n;

//if(d)dbg(' . . . slide.'+n+'.complete='+slide.image.complete + ' :: ' + slide.image.src);
//if(d)dbg(slide.image);

  
    	if (!slide.image.complete){
if(d)dbg('<li>cannot update. waiting for image.'+n+' to complete');
		return;
    	}


	// todo - need safari fix here

    	this.imgEl.src = slide.image.src;
    	slide.alreadyShown = true;

if(d)dbg('<li>updated imgEl.src to ' + slide.image.src);


    	this.imgEl.alt = slide.alt;
    	this.hrefEl.href = slide.href;


    	if (typeof this.updateCb == 'function') this.updateCb();

    	// Update the text
	var el = this.titleEl;
	if (el) el.innerHTML = slide.title;

	el = this.legendEl;
	if (el) el.innerHTML = slide.legend;

	el = this.copyrightEl;
	if (el) el.innerHTML = slide.copyright;

if(d)dbg('<li>typeof copyrightEl ' + typeof copyrightEl); 
if(d)dbg('<li> txt='+slide.copyright);

  }

  //--------------------------------------------------
  this.getSlide = function(n) {

	var n0 = n;

	if (typeof n == UNDEF) n = this.current;

	if (n >=  this.slideArr.length)   n =0;
	if (n < 0) 			  n =0;

if(d)dbg('<li>getSlide n='+n0 + ' ... ' +n);
	var slide = this.slideArr[n];
	slide.n = n;
	return slide;
  }


  //--------------------------------------------------
  this.advanceSlide = function(delta) {


	if (typeof delta == UNDEF) delta = 1 ;

	var nslide = this.getSlide(this.current+delta);
	n = nslide.n;

	// preload a few slides while we are here
	var nx = this.current;
	for (j=0; j<this.preloadCnt; j++){
		nx += 1; this.getSlide(nx).preload(nx);
	}


if(d)dbg('<li>in advanceSlide from '+this.current + ' to'+n);

    	// Make sure the new slide image has finished loading
    	var bool = false;

	if (nslide.image.complete == null 
			|| nslide.image.complete) bool = true;

    	if(bool){
		this.current = n;
	
		this.update();
	}
	else { 
if(d)dbg('<br>... still waiting for slide.'+n+' to complete--'+nslide.image.src);
	}


  }

}
  
// ======================================================================


var photoViewer = null;

function mkSlide(href,title,img,legend,copyright,alt)
{
	if (!photoViewer) photoViewer = new Slideshow();

	var obj = new slide();

	obj.title = title;
	obj.src = img;
	obj.legend = legend;
	obj.copyright = copyright;
	obj.alt = alt;
	obj.href = href;

	photoViewer.add_slide(obj);

}
	
