//requires jb_DHTMLLib.js; jb_sequence.js if sequencing
/* rewrote on 7/11/06 to use actual time instead of some arbitrary framerate. The earlier version made the speed of the animation dependent upon processor speed, but now the animation will take a consistent amount of time (though it'll be jerkier on slower computers)
*/


function jb_fader(id, obj, seqRef, startOp, endOp, totalTime, callback) {
//	this.imageName = id; //this is required to work around Safari bug
	this.id = id;
	this.obj = getRawObject(obj);
	this.startOp = startOp;
	this.endOp = endOp;
	this.currentOp = startOp;
	this.totalTime = totalTime * 1000;
	this.callback = callback;
	this.fps = 24;
	//this.delta = (endOp - startOp) / (totalTime * this.fps);
	this.gRef = "jb_fader_"+this.id;
	eval(this.gRef+"=this");
	this.seqRef = seqRef;//somehow the seq has to pass its ref to this object, in case of multiple sequences...
}

jb_fader.prototype.init = function() {
	var d = new Date();
	this.startTime = d.getTime();
	this.setOp(this.startOp);
	this.currentOp = this.startOp;
	this.animate();
}

jb_fader.prototype.animate = function() {
	var dDelta = new Date();
	this.deltaTime = dDelta.getTime() - this.startTime;
	this.delta = (this.endOp - this.startOp) * (this.deltaTime / this.totalTime);
//	alert(this.deltaTime + " " + this.totalTime + " " + this.endOp + " " + this.startOp + " " + this.delta);
	this.currentOp += this.delta;
	if ((this.startOp < this.endOp && this.currentOp >= this.endOp) || (this.startOp > this.endOp && this.currentOp <= this.endOp)) {
		//setOpacity(this.obj, this.endOp);
		this.setOp(this.endOp);
		clearTimeout(this.timer);
		if (this.seqRef) {
			eval(this.seqRef + ".nextStep()");
		}
		if (this.callback) {
			eval(this.callback);
		}
	} else {
		this.setOp(this.currentOp);
		this.timer = window.setTimeout(this.gRef + ".animate()", 1000/this.fps);
	}
}

jb_fader.prototype.setOp = function(op) {
//alert(op);
	this.currentOp = op;
	jbsetOpacity(this.obj, this.currentOp);
	if (this.seqRef) {
		eval(this.seqRef + ".nextStep()");
	}
}
