/*
  public class TSlinkrotation {
    public static int id;

    public void addLink();
    public void rotateLink();
    public void start();
  }
*/

function TSlinkrotation () {
  // fill the linklist with your links
  this.addLink = addLink;
        
  // change the link
  this.rotateLink = rotateLink;

  // loop the rotateLink() function
  this.start = function() {

    // check if browser supports javascript and start rotation if browser does
    if (document.all || document.getElementById)
    {
      this.rotateLink();
      setInterval(this.objName+'.rotateLink()', this.refreshTime);
    }
  }
}



/*
  public class RandomRotation extends TSlinkrotation {
    public String objName;
    public String divName;
    public String bgAltColor;
    public int refreshTime;

    public void setCurrentIndex();
  }
*/


function RandomRotation(refreshTime, divName, bgColor) {

  this.linklist = new Array();

  // give every linkrotation object an unique name
  this.objName = "linkrotation" + (TSlinkrotation.id++);
  eval(this.objName + "=this");

  this.refreshTime = refreshTime * 1000;
  this.divName = divName;
  this.bgAltColor = bgColor;

  // generate a random number between 0 and the highest linklist index
  this.setCurrentIndex = function() {
    var n;

    do {
      n = Math.floor(Math.random() * (this.linklist.length));
    }
    while ( n == this.currentIndex);

    this.currentIndex = n;
  }
}

RandomRotation.prototype = new TSlinkrotation;


/*
  public class NormalRotation extends TSlinkrotation {
    public String objName;
    public String divName;
    public String bgAltColor;
    public int refreshTime;
    public int currentIndex;

    public void setCurrentIndex();
  }
*/

function NormalRotation(refreshTime, divName, bgColor) {

  this.linklist = new Array();

  // give every linkrotation object an unique name
  this.objName = "linkrotation" + (TSlinkrotation.id++);
  eval(this.objName + "=this");

  this.refreshTime = refreshTime * 1000;
  this.divName = divName;
  this.bgAltColor = bgColor;

  this.currentIndex = -1;

  this.setCurrentIndex = function() {
    if (this.currentIndex >= this.linklist.length - 1)
      this.currentIndex = 0;
    else this.currentIndex++;
  }
}

NormalRotation.prototype = new TSlinkrotation;


/*
    support functions
*/

function addLink(link) {
  this.linklist[this.linklist.length] = link;
}


function rotateLink() {

  this.setCurrentIndex();

  if (document.getElementById && document.createRange) {

    // Netscape 6+, Mozilla
    // we can use the W3C DOM

    var range = document.createRange();

    // search the <div id="divName"> in the webpage
    var div = document.getElementById(this.divName);
    range.setStartBefore(div);

    // prepare the new link
    var currentLink = range.createContextualFragment(this.linklist[this.currentIndex]);

    // remove the old link
    while (div.hasChildNodes())
      div.removeChild(div.lastChild);

    // write the new link into the webpage
    div.appendChild(currentLink);
  }


  else if (document.all) {

    // Internet Explorer 4+,  Opera (does not support DHTML)
    // we have to use the ms object model
    
    theDiv = document.all(this.divName)

    if (theDiv.filters)
    {
    	theDiv.filters[0].apply();
    }
    theDiv.runtimeStyle.backgroundColor = (theDiv.runtimeStyle.backgroundColor == "" ? this.bgAltColor : "");
    theDiv.innerHTML = this.linklist[this.currentIndex];
    
    if (theDiv.filters)
    {
    	theDiv.filters[0].play();
    }
 }
}

TSlinkrotation.id = 0;  

