﻿
function TextRotator(id){
   this.id = id;
   this.textItems = new Array();         
   this.speed = 1000 * 3;

   //helper memembers
   this.timer;
   this.nextIndex;
   this.rotator;

   //public methods
   this.SetSpeed = SetSpeed;
   this.AddTextItem = AddTextItem;
   this.TextItemsCount = TextItemsCount;
   this.Start = Start;
   this.Stop = Stop;
   this.CreateRotator = CreateRotator;

   //internal helper methods
   this.NextTextItem = NextTextItem;
   this.ChangeText = ChangeText;

   function Start(){
      if(this.textItems.length < 1) return;
      this.nextIndex = 0;
      if(this.textItems.length == 1){      
         ChangeText(this.textItems[0]);
      }
      var thisVar = this;
      this.timer = setInterval(function() { 
         NextTextItem(thisVar) 
      }, this.speed);
   }

   function Stop(){
      clearInterval(this.timer);
   }

   function NextTextItem(rotatorInstance){
      if(rotatorInstance.nextIndex == rotatorInstance.textItems.length) rotatorInstance.nextIndex = 0;
      rotatorInstance.ChangeText(rotatorInstance.textItems[rotatorInstance.nextIndex++]);  
   }

   function ChangeText(newText){
      this.rotator.innerHTML = newText;
   }

   function TextItemsCount(){
      return this.textItems.length;
   }

   function AddTextItem(htmlString){
      this.textItems.push(htmlString);
   }

   function SetSpeed(newSpeed){
      this.speed = newSpeed;
   }

   function CreateRotator(){
      document.write('<span id="' + this.id + '"></span>');
      this.rotator = document.getElementById(this.id);
   }
}  

