/*
  SwapperThing
  v1.0.2
  Tuesday, December 20, 2005
  by Wevah (Nate Weaver; http://www.derailer.org/)
  for http://www.beeswees.com/
*/


function SwapperThing(varname) {
  // The varname pass-in is for Mac IE, which apparently doesn't
  // like the function-reference form of setInterval().
  if (varname)
    this._varname = varname;
}

SwapperThing.prototype._images = new Array();
SwapperThing.prototype._captions = new Array();
SwapperThing.prototype._timeout = null;
SwapperThing.prototype._delay = 2000;
SwapperThing.prototype._currentIndex = -1;
SwapperThing.prototype._imageElement = null;
SwapperThing.prototype._captionElement = null;
SwapperThing.prototype._indexElement = null;
SwapperThing.prototype._varName = null;

SwapperThing.prototype.toString = function () {
  return '[object SwapperThing]';
};

SwapperThing.prototype.addImageAndCaption = function (img, caption) {
  this._images[this._images.length] = img;
  this._captions[this._captions.length] = caption;
};

SwapperThing.prototype.startLoop = function () {
  this.stopLoop();
  if (this._varname)
    this._timeout = setInterval(this._varname + '.switchImage();', this._delay);
  else { // this branch doesn't work in Mac IE
    var me = this;
    this._timeout = setInterval(function () { me.switchImage(); }, this._delay);    
  }
};

SwapperThing.prototype.stopLoop = function () {
  if (this._timeout) {
    clearInterval(this._timeout);
    this._timeout = null;
  }
};

SwapperThing.prototype.switchImage = function () {
  this._currentIndex = (this._currentIndex + 1) % this._images.length;
  this.setImage(this._images[this._currentIndex]);
  this.setCaption(this._captions[this._currentIndex]);
  this.setIndex(this._currentIndex + 1);
};

SwapperThing.prototype.setImage = function (img) {
  this._imageElement.src = img;
};

SwapperThing.prototype.setCaption = function (text) {
  this._captionElement.innerHTML = text; // innerHTML, so we can use links and other markup
};

SwapperThing.prototype.setIndex = function (idx) {
  if (this._indexElement.firstChild)
    this._indexElement.replaceChild(document.createTextNode(idx), this._indexElement.firstChild);
  else
    this._indexElement.appendChild(document.createTextNode(idx));
};

SwapperThing.prototype.attach = function (elem, showIndex) {
  if (typeof elem == 'string')
    elem = document.getElementById(elem);

  this._imageElement = document.createElement('img');
  this._imageElement.className = 'swapImg';
  elem.appendChild(this._imageElement);
  
  this._containerElement = document.createElement('div');
  this._containerElement.className = 'swapContainer';
  elem.appendChild(this._containerElement);

  if (showIndex) {
    this._indexElement = document.createElement('p');
    this._indexElement.className = 'swapIndex';
    elem.childNodes[1].appendChild(this._indexElement);
  }
  
  this._captionElement = document.createElement('p');
  this._captionElement.className = 'swapCaption';
  elem.childNodes[1].appendChild(this._captionElement);  
  
  this.switchImage();
  
  this.startLoop();
};

SwapperThing.prototype.preloadImages = function () {
  for (img in this._images) {
    var temp = new Image();
    temp.src = img;
  }
};

SwapperThing.prototype.setDelay = function (secs) {
  this._delay = secs * 1000;
};

