// 	cycleImages class
//
//	Allows you to cycle images through a number of image view
//	portals. The number of images should not be less than the 
//	number of portals.
//
//	The user must create the HTML that displays the images.
//
//	Parameters:
//	  imageList	An array of URLs to the displayed images
//	  imgIds	The DOM Ids of the view portals (img tags)
//	  duration	The duration before starting the next cycle
//	  uniqueName	The variable name that holds the class information.
//			E.g.,  imageCycles = new cycleImages (Images, Ids, 3000, 'imageCycles');
//			This is a hack to make the class work (see below).
//
//
//	Note:
//	The name of the class variable is passed into the routine so that
//	the code can access the internal arrays and variables. This is necessary
//	because window.setTimeout does not pass objects or arrays (without
//	serializing them in the argument list first). The timeout function that
//	handles the cycling accesses the class information using
//	'window[<variable-name>]'. If the class information is stored in an
//	object, this class will not work.
//
//	The base level routine "pops" the images from one location to another.
//	A different transition routine could be written that scrolls, fades,
//	or otherwise performs a transition. This is saved for a future revision.
//	It would likely involve adding an argument to the class definition
//	that would select the transition type. Also, the timeout routine would
//	need to be modified to incorporate that routine to do the actual
//	transition (instead of the line 
//		element.src = that.preloadImages[ndx].src;
//
//	Author:
//		Leonard Daly, Leonard.Daly [AT] realism.com
//		Daly Realism - http://realism.com/



function _popImages(uniqueName) {
  that = window[uniqueName];
  for (ii=0; ii<that.viewports; ii++) {
    ndx = ii + that.imageOffset;
    if (ndx >= that.preloadImages.length) {
      ndx = ndx - that.preloadImages.length;
    }
//alert (uniqueName + ': ' + ii + ', ' + that.imgIds[ii]);
    element = document.getElementById(that.imgIds[ii]);
    if (that.preloadImages[ndx].complete && element != null) {
      element.src = that.preloadImages[ndx].src;
    }
  }
  that.imageOffset ++;
  if (that.imageOffset >= that.preloadImages.length) {
    that.imageOffset = 0;
  }
  if (that.duration > 0) {
    exec = '_popImages("'+uniqueName+'");';
    that.timeid = window.setTimeout (exec, that.duration);
  }
}
//tmp = new cycleImages (new Array(0), new Array(0), -1, 'tmp');
//cycleImages.prototype.popImages = _popImages;
//tmp = null;


function cycleImages (imageList, imgIds, duration, uniqueName, startIndex) {
  this.imageList = imageList;
  this.imgIds	 = imgIds;
  this.duration  = duration;
  this.viewports = imgIds.length;
  this.uniqueName = uniqueName;
  if (cycleImages.arguments.length > 4) {
    this.imageOffset = startIndex;
   } else {
    this.imageOffset = 1;
  }

  this.preloadImages = new Array (imageList.length);
  for (ii=0; ii<imageList.length; ii++) {
    this.preloadImages[ii] = new Image();
    this.preloadImages[ii].src = imageList[ii];
  }
  if (this.duration > 0) {
    exec = '_popImages("'+uniqueName+'");';
    this.timeid = window.setTimeout (exec, 0);
  }
}

