var classRef;
var initDelay = 3000;
var fadeIncDelay = 30;

var picRotate=function (motherDiv, width, height, dLink) {
	this.images = new Array();
	this.links = new Array();
	this.divs = new Array();
	this.height=height;
	this.width=width;
	this.num=0;
	this.numLoad=0;
	this.numDone=0;
	this.divID=motherDiv;
	this.div = document.getElementById(motherDiv);
	this.divLeft = getTotalOffset(this.div,"left");
	this.divTop = getTotalOffset(this.div,"top");
	this.currentDiv=null;
	this.currentOpac=0;
	this.currentImg=1;
	classRef=this;
	this.dLink = (typeof(dLink) != "undefined") ? dLink:null;
}

picRotate.prototype.addImage = function(imageURI,dLink) {
	this.images.push(imageURI);
	this.links.push((typeof(dLink) != "undefined") ? dLink:null);
	this.num++;
}

picRotate.prototype.preload = function() {
	if (this.num < 3) {
		alert("You need at least 3 images in order for the Images Rotator to work.");
		return false;
	}
	var i,img;
	for (i=0;i<this.images.length;i++) {
		img = new Image(this.width,this.height);
		img.onload=picRotate.prototype.imgLoaded;
		img.onerror=picRotate.prototype.imgError;
		img.onabort=picRotate.prototype.imgAbort;
		img.dClass=this;
		img.dIndex=i;
		img.src = this.images[i];
		this.images[i] = img;
	}
}

picRotate.prototype.imgLoaded = function() {
	this.dClass.numLoad++;
	this.dClass.imgDone();
}

picRotate.prototype.imgError = function() {
	this.dClass.images.splice(this.dIndex,1);
	this.dClass.imgDone();
}

picRotate.prototype.imgAbort = function() {
	this.dClass.images.splice(this.dIndex,1);
	this.dClass.imgDone();
}

picRotate.prototype.imgDone = function() {
	this.numDone++;
	if (this.numDone == this.num) {
		this.setupDivs();
	}
}

picRotate.prototype.setupDivs = function() {
	var i,oDiv,img;
	
	for (i=0;i<2;i++) {
		oDiv = document.createElement("div");
		this.div.appendChild(oDiv);
		oDiv.style.position="absolute";
		oDiv.style.width = this.width+"px";
		oDiv.style.height = this.height+"px";
		oDiv.style.left = "0px";
		oDiv.style.top = "0px";
		this.divs.push(oDiv);
		img = document.createElement("img");
		img.src=this.images[i].src;
		oDiv.appendChild(img);
		img.width=this.width;
		img.height=this.height;
		if (this.dLink != null) {
			img.style.cursor="pointer";
			img.onclick = function () { var l; if ((l=getLink(classRef.currentImg)) != null) document.location.href=l; };
		}
	}
	
	this.currentDiv = 1;
	setOpacity(classRef.divs[classRef.currentDiv],classRef.currentOpac);
	window.setTimeout("go()",initDelay);
}

var go = function() {
	var debug = "currentDiv = " + classRef.currentDiv + "\n" + "currentPic = " + classRef.divs[classRef.currentDiv].firstChild.src;
	debug += "\n" + "opacity = " + classRef.currentOpac + "\n" + "cd zIndex = " + classRef.divs[classRef.currentDiv].style.zIndex;
	//alert(debug);
	setOpacity(classRef.divs[classRef.currentDiv],classRef.currentOpac);
	if (classRef.currentOpac == 100) {
		classRef.currentDiv = Math.abs(classRef.currentDiv-1);
		setOpacity(classRef.divs[classRef.currentDiv],0);
		classRef.currentOpac = 0;
		classRef.divs[classRef.currentDiv].style.zIndex=1;
		classRef.divs[Math.abs(classRef.currentDiv-1)].style.zIndex=0;
		classRef.currentImg = (classRef.currentImg == classRef.images.length-1) ? 0:classRef.currentImg+1;
		classRef.divs[classRef.currentDiv].firstChild.src=classRef.images[classRef.currentImg].src;
		window.setTimeout("go()",initDelay);	
	}
	else {
		classRef.currentOpac +=5;
		window.setTimeout("go()",fadeIncDelay);
	}
}

var setOpacity = function (obj,opac) {
	if (isGecko) obj.style.opacity = opac/100;
	else obj.style.filter="alpha(opacity="+opac+")"
	
}

var getTotalOffset = function (htmlElement,dir) {
	var tot=0,prop="offset"+dir.substr(0,1).toUpperCase()+dir.substr(1).toLowerCase();
	if (typeof(eval("htmlElement." + prop)) == "number") {
		while (htmlElement.offsetParent) {
			tot += eval("htmlElement." + prop);
			htmlElement = htmlElement.offsetParent;
		}
		return tot;
	}
	else return null;
}

var rotatorObj = function () {
	this.dAlpha = 0;
	this.dIndex = 0;
}

var getLink = function() {
	var ci;
	if (classRef.currentImg == 0) ci = classRef.images.length-1;
	else ci = classRef.currentImg-1;
	return classRef.links[ci];
}