dw_Rotator.restartDelay = 500; // delay before call to rotate
dw_Rotator.col=[];

/*--------------------------------------------------------/
/* Rotator Class Constructor							 */
/*--------------------------------------------------------/
| arguments: 
|	image name
|	rotation speed (milliseconds)
|	path to images (optional)
|	name of target window (optional, string)
|	object name that displays the current photo number (optional, i.e. Photo *1* of 3)
|	object name that displays the current photo's caption (optional)
/-----------------------------------------------------------*/
function dw_Rotator(name,speed,path,tgt,photonum,caption){
	this.name		= name;
	this.speed		= speed||4500;
	this.path		= path||"";
	this.tgt		= tgt;
	this.photonum	= photonum||"";
	this.caption	= caption||"";
	this.ctr		= 0;
	this.timer		= 0;
	this.imgs		= [];
	this.captions	= [];
	this.actions	= [];
	this.started	= false;
	this.index		= dw_Rotator.col.length;
	dw_Rotator.col[this.index]=this;
	this.animString	= "dw_Rotator.col["+this.index+"]";
};

//-------- Private Functions ----------//
dw_Rotator.prototype.addImages=function(){
	var img;
	for(var i=0;arguments[i];i++){
		img = new Image();
		img.src = this.path+arguments[i];
		this.imgs[this.imgs.length] = img;
	}
};

dw_Rotator.prototype.addCaptions=function(){
	for(var i=0;arguments[i];i++){
		this.captions[this.captions.length] = arguments[i];
	}
};

dw_Rotator.prototype.addActions=function(){
	var len = arguments.length;
	for(var i=0;i<len;i++)
		this.actions[this.actions.length] = arguments[i];
};
	
dw_Rotator.prototype.rotate=function(){
	clearTimeout(this.timer);
	this.timer = null;
	
	if(this.ctr < this.imgs.length-1)
		this.ctr++;
	else 
		this.ctr=0;
	
	var imgObj = document.images[this.name];
	
	if(imgObj){
		imgObj.src = this.imgs[this.ctr].src;
		
		if (this.started)
			this.timer = setTimeout(this.animString+".rotate()",this.speed);
		
		if (!this.photonum == "")
			document.getElementById(this.photonum).innerHTML = this.ctr+1;
		
		if (!this.caption == ""){
			if (this.captions[this.ctr] == undefined){
				document.getElementById(this.caption).innerHTML = '';
				imgObj.alt = '';
			}else{
				document.getElementById(this.caption).innerHTML = this.captions[this.ctr];
				imgObj.alt = this.captions[this.ctr];
			}
		}
	}
};

//-------- Public Methods ----------//
dw_Rotator.start=function(){
	var len = dw_Rotator.col.length,obj;
	for(var i=0;i<len;i++){
		obj = dw_Rotator.col[i];
		if(obj&&obj.name) obj.timer = setTimeout(obj.animString+".rotate()",obj.speed);
		obj.started = true;
	}
};

dw_Rotator.doClick=function(n){
	var obj = dw_Rotator.col[n];
	if(!document.images || !obj) return true;
	if(obj.actions && obj.actions[obj.ctr]){
		if(typeof obj.actions[obj.ctr] == "string"){
			if(obj.tgt){
				var win = window.open(obj.actions[obj.ctr],obj.tgt);
				if(win && !win.closed) win.focus();
			}else{
				window.location = obj.actions[obj.ctr];
			}
		}else{
			obj.actions[obj.ctr]();
		}
	}
	
	return false;
};

dw_Rotator.pause=function(n){
	dw_Rotator.col[n].started = false;
	dw_Rotator.clearTimers(n);
};

dw_Rotator.clearTimers=function(n){
	var obj = dw_Rotator.col[n];
	if(obj){
		clearTimeout(obj.timer);
		obj.timer = null;
	}
};

dw_Rotator.resume=function(n){
	dw_Rotator.clearTimers(n);
	var obj = dw_Rotator.col[n];
	if(obj){
		obj.timer = setTimeout(obj.animString+".rotate()",dw_Rotator.restartDelay);
	}
};

dw_Rotator.togglePauseResume=function(resume,n){
	if (resume){
		if (dw_Rotator.col[n].started)
			dw_Rotator.resume(n);
		else
			dw_Rotator.start();
	}else{
		dw_Rotator.pause(n);		
	}
};

dw_Rotator.movenext=function(n){
	dw_Rotator.col[n].rotate();
};

dw_Rotator.moveprevious=function(n){
	var obj = dw_Rotator.col[n];
	
	if(obj){
		if (obj.ctr == 0) {
			obj.ctr = obj.imgs.length-2
		}else{
			if (obj.ctr > 1){
				obj.ctr = obj.ctr - 2;
			}else{
				obj.ctr = obj.imgs.length-1;
			}
		}
	}

	dw_Rotator.col[n].rotate();
};
