Function.prototype.bind = function(scope, args)
{
  scope = scope || null;
  args = args || [];
  var fn = this;
  return function() {
    return fn.apply(scope, args);
  };
};

function ImageSwitcher(id)
{
  this.id = id;
  this.images = [];
  
  var src = this.getImage().src;
  
  if (src != "") {
    this.addImage(src);
  }
}
ImageSwitcher.prototype = {
  
  images : null,
  currentImage : 0,
  id : "",
  interval : 3,
  timer : null,
  
  addImage : function(src)
  {
    return this.images.push( this.createImage(src) ) - 1;
  },
  
  change : function()
  {
    this.currentImage = this.getNextImage();
    this.getImage().src = this.images[ this.currentImage ].src;
  },
  
  createImage : function(src)
  {
    var img = document.createElement("img");
    img.src = src;
    return img;
  },
  
  destroy : function()
  {
    this.stop();
    this.images = null;
  },
  
  getImage : function()
  {
    return document.getElementById(this.id);
  },
  
  getNextImage : function()
  {
    var i = this.currentImage + 1;
    
    if (i == this.images.length) {
      i = 0;
    }
    
    return i;
  },
  
  start : function()
  {
    if (this.timer) {
      return;
    }
    this.timer = setInterval(this.change.bind(this), this.interval * 1000);
  },
  
  stop : function()
  {
    if (!this.timer) {
      return;
    }
    clearInterval(this.timer);
    this.timer = null;
  }
  
}; 