/************************************************
*                                               *
*  TSlinkrotation 1.0                           *
*  Programming Language: Java Script            *
*                                               *
*  by Thomas Schuster                           *
*  http://www.TSinter.net                       *
*                                               *
*  This is an object oriented link rotator,     *
*  which is designed for webmaster who wants    *
*  to earn money by displaying advertising on   *
*  their website. All banner and text links     *
*  are processed without changing a character,  *
*  so every click has to be accepted by the     *
*  ad partner.                                  *
*                                               *
************************************************/


/*
  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)
			setInterval(this.objName+'.rotateLink()', this.refreshTime);
	}
}



/*
    public class RandomRotation extends TSlinkrotation {
        public String objName;
        public String divName;
        public int refreshTime;

        public void setCurrentIndex();
    }
*/

function RandomRotation(refreshTime, divName) {

	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;

    // 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 int refreshTime;
        public int currentIndex;

        public void setCurrentIndex();
    }
*/

function NormalRotation(refreshTime, divName) {

	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.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

        eval('document.all.'+this.divName+'.innerHTML = this.linklist[this.currentIndex]');
    }
}

TSlinkrotation.id = 0;        