// JavaScript Document
function mod(modThis, byThis) {
	multiple=Math.floor(modThis / byThis);
	return modThis-byThis*multiple;
}

// Note: aryOfHTMLElements is optional - if you don't include it or if htmlTargetId is null, the rotator will simply not try to cycle HTML, it will only cycle images.
function Rotator(imgTargetId, htmlTargetId, cycleDuration, aryOfImages, aryOfLinks, aryOfHTMLElements, aryOfIndicators) {
	
	this.imgTargetId=imgTargetId;				// Store the image id
	
	// Store the reference to the node. If a node ref was passed instead of a string, use that too.
	if (typeof this.imgTargetId == "string") {
		this.imgTargetNode=document.getElementById(this.imgTargetId);
	} else {
		this.imgTargetNode=this.imgTargetId;
	}
	
	// If the node has no id (which might happen if a ref rather than id is passed) give it one.
	if ((typeof this.imgTargetNode.id == "undefined") || (this.imgTargetNode.id==null)) {
		this.imgTargetNode.id="rotator_img_"+(Math.random()*1000000);
	}
	
	this.imgTargetId=this.imgTargetNode.id;	// Now, incase a ref was originally passed, make sure it really is an id that's stored.
	
	this.imgTargetNode.rotator=this;		// Ensure the imgTarget node has a reference back to this object
											// This is essential for actions like setTimeout that'd lose object properties.
											
	this.aryOfImages=aryOfImages;			// String representation of all image urls
				
	// Keep track of the array of links
	if ((typeof aryOfLinks == "undefined") || (aryOfLinks==null)) { // Blank it out if no HTML elements are passed
		this.aryOfLinks=null;
	} else {
		this.aryOfLinks=aryOfLinks;
	}										
											
	this.htmlTargetId=htmlTargetId;			// Store the html id
	if ((typeof aryOfHTMLElements == "undefined") || (aryOfHTMLElements==null)) { // Blank it out if no HTML elements are passed
		this.htmlTargetId=null;
	}
	
	// Store the reference to the node. If a node ref was passed instead of a string, use that too.
	if (this.htmlTargetId!=null) {
		if (typeof this.htmlTargetId == "string") {
			this.htmlTargetNode=document.getElementById(this.htmlTargetId);
		} else {
			this.htmlTargetNode=this.htmlTargetId;
		}
	
		// If the node has no id (which might happen if a ref rather than id is passed) give it one.
		if ((typeof this.htmlTargetNode.id == "undefined") || (this.imgTargetNode.id==null)) {
			this.htmlTargetNode.id="rotator_html_"+(Math.random()*1000000);
		}
	
		this.htmlTargetId=this.htmlTargetNode.id;	// Now, incase a ref was originally passed, make sure it really is an id that's stored.
		
		this.aryOfHTMLElements=aryOfHTMLElements;		// An array of all HTML elements (ideally in string id form)
		for (i=0; i<this.aryOfHTMLElements.lenght; i++) {
			if (typeof this.aryOfHTMLElements == "string") {
				this.aryOfHTMLElements=document.getElementById(this.aryOfHTMLElements);
			}
		}
	}
	
	// Keep track of the array of indicators
	if ((typeof aryOfIndicators == "undefined") || (aryOfIndicators==null)) { // Blank it out if no HTML elements are passed
		this.aryOfIndicators=null;
	} else {
		this.aryOfIndicators=aryOfIndicators;
	}
	
	this.cycleDuration=cycleDuration;	// Duration of a single animation cycle.
	this.animating=false;				// Keep track of whether it's currently animating.
	this.continuable=true;				// Keeps track of whether the rotator has been outright hardstopped (rather than paused) and thus needs a start (rather than resume)
		
	this.currentImage=0;				// Keep track of which image is currently displayed.
	this.timer=null;
	
	this.preCache(); 					// Comment out if precaching isn't desired.
	
	this.setImage(0);					// Initialize;
}

function InitSimpleRotator(item1, item2, item3, item4) {
	result = new Rotator('rotating_image', 'rotator_content_div', 2000, 
							[item1[0], item2[0], item3[0], item4[0]], 
							[item1[1], item2[1], item3[1], item4[1]], 
							['rotator_content_0', 'rotator_content_1', 'rotator_content_2', 'rotator_content_3'],
							['rotator_state_0', 'rotator_state_1', 'rotator_state_2', 'rotator_state_3']);
	result.start();
	return result;
}

Rotator.prototype.preCache=function() {
	this.imgArray=new Array();
	for (i=0; i<this.aryOfImages.length; i++) {
		this.imgArray[i]=new Image();
		this.imgArray[i].src=this.aryOfImages[i];
	}
}

Rotator.prototype.updateIndicators=function() {
	if (this.aryOfIndicators!=null) {
		for (i=0; i<this.aryOfIndicators.length; i++) {
			thisIndicator=document.getElementById(this.aryOfIndicators[i]);
			if (i==this.currentImage) {
				thisIndicator.className="current";
			} else {
				thisIndicator.className="";
			}
		}
	}
}

Rotator.prototype.updateLink=function() {
	if (this.aryOfLinks!=null) {
		if (this.imgTargetNode.parentNode.nodeName.toString().toLowerCase()=="a") {
			this.imgTargetNode.parentNode.href=this.aryOfLinks[this.currentImage];
		}
	}
}

Rotator.prototype.setImage=function(imageNumber) {
	imageNumber=mod(imageNumber, this.aryOfImages.length);
	this.currentImage=imageNumber;
	this.imgTargetNode.src=this.aryOfImages[imageNumber];
	
	if (this.htmlTargetId!=null) {
		this.htmlTargetNode.innerHTML=this.aryOfHTMLElements[imageNumber].innerHTML;
	}
	
	this.updateIndicators();
	this.updateLink();
}

Rotator.prototype.next = function() {
	newImg=this.currentImage+1;
	if (this.newImg >= this.aryOfImages.length) {
		newImg=0;
	}
	this.setImage(newImg);
	
	if (this.animating) {
		this.pause();	// Clear any existing animation cycles.
		this.resume();	// Start a new one.
	}
}

Rotator.prototype.previous = function() {
	newImg=this.currentImage-1;
	if (this.newImg < 0 ) {
		newImg=this.aryOfImages.length-1;
	}
	this.setImage(newImg);
}

// Resumes the animation only if it hasn't been stopped with a hardstop.
Rotator.prototype.resume = function() {
	if (this.timer==null) {
		if (this.resumable) {
			this.animating=true;
			this.timer=setTimeout("document.getElementById('"+this.imgTargetId+"').rotator.next()", this.cycleDuration);
		}
	}
}

// Starts the animation, even if it has been stopped with a hardstop.
Rotator.prototype.start = function() {
	this.resumable=true;
	this.resume();
}

// Stops the animation temporarily but can be resumed with resume or start.
Rotator.prototype.pause = function() {
	if (this.timer!=null) {
		clearTimeout(this.timer);
		this.timer=null;
		this.animating=false;
	}
}

// Stops the animation and blocks resumes from resuming it (requires a full start)
Rotator.prototype.hardstop = function() {
	this.resumable=false;
	this.pause();
}