/*
Image rotator for CONMEXFLOR AC
If you want to use this script check the LGPL Licence
*/

function rotator() {	this.globalname = "";	this.timeout  = 0; // (msecs) time after redirectior occurs 
	this.redirect = ""; // (url) where to redirect on .timeout
	// to commit a redirect needs a timeout > 0 and redirect != ""
	this.maximages = 0; // (num) set how many images are	this.minimages = 0; // (num) set how many mininal images are for the start (you can set to zero, maximum is this.maximages-1)
	this.interval = 0; // (msecs) time after change an image
	this.imageid = ""; // (string) id for the image tag
	this.imageprefix = "./rotator/image-"; // (string) prefix for the image name
	this.imagesufix = ".jpg"; // (string) su for the image name
	this.randomize = true; // set if want a randomized order
		   
	this._img = null;
	this._arr = null;
	this._loadedimages = 0;
	this._iswaiting = false;
	this._started = false;}rotator.prototype.start = function () {	this._img = document.getElementById(this.imageid);	if (this.globalname == "") return false;
	if (this._img == null) return false;
	if (this.maximages <= 0) return false;
	if (this.timeout > 0 && this.redirect != "")
		window.setTimeout("window.location.assign(\"" + this.redirect + "\")", this.timeout * 1000);
	this._started = false;
	this._arr = new Array();
	for (i = 0 ; i < this.maximages ; i++) {		this._arr[i] = new pimg(this, this.imageprefix + (i + 1) + this.imagesufix);	}
	this._sort();
	this._started = true;
	this.makerotation();}

rotator.prototype._sort = function () {	if (this.randomize) for (i = 0 ; i < this.maximages - 1 ; i++) this._arr.sort(this._randomorder);
}
rotator.prototype.makerotation = function () {	if (this._loadedimages == 0 || this._loadedimages < this.minimages || !this._started || this._iswaiting) return false;
	this._iswaiting = true;
	somedisplayed  = false;	for (i = 0 ; i < this.maximages ; i++) {		if (this._arr[i].display()) {			somedisplayed = true;
			if (this.interval > 0)				window.setTimeout(this.globalname + "._nextrotation();", this.interval * 1000);
			break;
		}
	}
	if (!somedisplayed) {
		reordered = false;
		for (i = 0 ; i < this.maximages ; i++) {
			if (this._arr[i]._state == 3) {
				this._arr[i]._state = 1;
				reordered = true;
			}
		}
		if (reordered) {
			this._sort();			this._nextrotation();		}
	}
}
	   
rotator.prototype._nextrotation = function () {
	this._iswaiting = false;
	this.makerotation();
}
	   
rotator.prototype._randomorder = function  (a, b) {
	return ((parseInt(Math.random() * 2) == 0) ? -1 : 1) ;
}
	
function pimg(pRotator, pSource) {
	this._rotator = pRotator;	this._img = new Image();	this._img.pimg = this;
	this._state = 0;	this._img.onload = pimg.prototype.onload;
	this._img.onerror = pimg.prototype.onerror;
	this._img.onabort = pimg.prototype.onerror;	this._img.src = pSource;
}
	
pimg.prototype.onload = function () {
	this.pimg._state = 1;	this.pimg._rotator._loadedimages++;	this.pimg._rotator.makerotation();
}

pimg.prototype.onerror = function () {
	this.pimg._state = 2; //dismissed
}

pimg.prototype.display = function () {
	if (this._state == 1) {
		this._state = 3;
		this._rotator._img.src = this._img.src;
		return true;
	}
	return false;
}