
var ImageRotator = function(interval, element) {

   // set rotation interval
   if(typeof(interval) != 'number')
      interval = 5000;
   this.interval = interval;

   // get image element
   if(typeof(element) == 'string')
      element = document.getElementById(element);
   this.element = element;

   // the current image
   this.current = 0;

   // images to rotate
   this.images = [];

   /// Add an image to rotate
   this.addImage = function(path, alt, link) {
      this.images[this.images.length] = {path: path, link: link, alt: alt};
   }

   /// Rotate the image
   this.rotate = function() {
      this.makeHtmlLink();
      this.current++;
      if(this.current >= this.images.length)
         this.current = 0;
   }

   /// Start the rotator
   this.start = function() {
      this.rotate();
      var _this = this;
      this.intervalId = setInterval(function() {
         _this.rotate();
      },this.interval);
   }

   /// Pause the rotator
   this.pause = function() {
       clearInterval(this.intervalId);
   }
   
   // Pick the first image to show
   this.pickFirstImage = function() {
	   if(this.images.length > 0) {
		   this.current = Math.floor(Math.random() * this.images.length); 
		   this.makeHtmlLink();
	   }
   }
   
   this.makeHtmlLink = function() {
	   this.element.innerHTML = (this.images[this.current].link ? '<a href="' + this.images[this.current].link + '">' : '') +
            '<img src="' + this.images[this.current].path + '"' +
            (this.images[this.current].alt ? ' alt="' + this.images[this.current].alt + '" ' : '') + ' />' +
            (this.images[this.current].link ? '</a>' : '');
   }

}

