/*
  SwapperThing
  v1.0.2
  Tuesday, December 20, 2005
  by Wevah (Nate Weaver; http://www.derailer.org/)
  for http://www.caminobrowser.org/
*/


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._linkTo = new Array();
SwapperThing.prototype._timeout = null;
SwapperThing.prototype._delay = 2000;
SwapperThing.prototype._currentIndex = -1;
SwapperThing.prototype._imageElement = null;
SwapperThing.prototype._captionElement = null;
SwapperThing.prototype._linkElement = null;
SwapperThing.prototype._indexElement = null;
SwapperThing.prototype._varName = null;

SwapperThing.prototype.toString = function () {
  return '[object SwapperThing]';
};

SwapperThing.prototype.addImageAndCaption = function (img, caption, link) {
  this._images[this._images.length] = img;
  this._captions[this._captions.length] = caption;

  this._linkTo[this._linkTo.length] = (link ? link : '.');

};

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.setLink(this._linkTo[this._currentIndex]);
};

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.setLink = function (link) {
  this._linkElement.href = link;
};

SwapperThing.prototype.attach = function (elem) {
  if (typeof elem == 'string')
    elem = document.getElementById(elem);

  img_container = document.createElement('div');
  img_container.className = 'swapImg';

  this._linkElement = document.createElement('a');
  this._imageElement = document.createElement('img');

  this._linkElement.appendChild(this._imageElement);
  img_container.appendChild(this._linkElement);
  elem.appendChild(img_container);
  
  this._captionElement = document.createElement('div');
  this._captionElement.className = 'black_transparent';
  this._captionElement.style.width = '100%';
  this._captionElement.style.padding = "4px 10px 7px 10px"
  elem.appendChild(this._captionElement);  
  
  this.switchImage();
  
  this.startLoop();
};

SwapperThing.prototype.preloadImages = function () {
  for (img in this._images) {
    var temp = new Image();
    temp.src = this._images[img];
  }
};

SwapperThing.prototype.setDelay = function (secs) {
  this._delay = secs * 1000;
};

