Rotator.restartDelay = 500; // delay onmouseout before call to rotate
Rotator.col=[];

// arguments: image id, rotation speed, (optional) path to images
function Rotator(id, btype, speed, path, tgt) {
	this.id = id;
	this.btype = btype || "rotating";
	this.speed = speed || 4000;
	this.path = path || "";
	this.tgt = tgt;
	this.ctr = 0;
	this.timer = 0;
	this.imgs = [];
	this.addImages = addImages;
	this.rotate = rotate;
	this.random = random;
	this.actions = [];
	this.index = Rotator.col.length;
	Rotator.col[this.index] = this;
	this.animString = "Rotator.col["+this.index+"]";
}

function addImages() {
    var img;
    for (var i = 0; arguments[i]; i++) {
        img = new Image();
        img.src = this.path + arguments[i];
        this.imgs[this.imgs.length] = img;
    }
}
    
function rotate() {
    clearTimeout(this.timer);
    this.timer = null;
    if (this.ctr < this.imgs.length -1) {
        this.ctr++;
    } else {
        this.ctr = 0;
    }
    var imgObj = document.getElementById(this.id);
    if (imgObj) {
        imgObj.src = this.imgs[this.ctr].src;
        this.timer = setTimeout(this.animString + ".rotate()", this.speed);
    }
}

function random() {
    clearTimeout(this.timer);
    this.timer = null;
    var p = this.imgs.length;
    var whichImage = Math.round(Math.random()*(p-1));
    var imgObj = document.getElementById(this.id);
    if (imgObj) {
        imgObj.src = this.imgs[whichImage].src;
    }
}

Rotator.start= function() {
	var len = Rotator.col.length;
	for (var i = 0; i < len; i++) {
		obj = Rotator.col[i];
		if (obj && obj.id)
			if (obj.btype == "random") {
				eval(obj.animString + ".random()");
			} else {
				obj.timer = setTimeout(obj.animString + ".rotate()", obj.speed);
			}
	}
};

Rotator.pause = function(n) {
    Rotator.clearTimers(n);
};

Rotator.clearTimers = function(n) {
    var obj = Rotator.col[n];
    if (obj) {
        clearTimeout(obj.timer);
        obj.timer = null;
    }
};

Rotator.resume = function(n) {
    Rotator.clearTimers(n);
    var obj = Rotator.col[n];
    if (obj) {
        obj.timer = setTimeout(obj.animString + ".rotate()", Rotator.restartDelay);
    }
};

/*
function initRotator() {
    var rotator1 = new Rotator('bannerimage1', 3000, "images/banners/home/");
    rotator1.addImages("99jcp1030205022.jpg", "0704042raspberrytu1.jpg", "p1010113.jpg", "p9110021.jpg", "wca-v2.jpg");
    Rotator.start();
}
*/

var nowOnload = window.onload; // Let's save the existing assignment, if any
window.onload = function () {
    try {
        initRotator();
    } catch (err) {
    	// no initRotator() function exists
    }

    // Now we call old function which was assigned to onLoad, thus playing nice
    if (nowOnload != null && typeof(nowOnload) == 'function') {
        nowOnload();
    }
}