/*****************************************************************************************************
* CLASS ImageRotator		- Handles js for image rotating apps such as product images previews
*
* AUTHOR
*	brian@deep.co.uk bbodelcampo@yahoo.co.uk
* COPYRIGHT
*	Deep Creative 2005
*
* INSTRUCTIONS
*	1 Instantiate with id of the image tag to rotate
*	2 Adds img sources with .addUrl(url) to rotate list
*	3 .preload() if desired
*	4 Run .showImg(0) if you wanna be meticulous
*	5 Rotate with .next(), .previous(), or set image explicitly with .showImg(index)
*
* PROPERTIES
*	imgId		- id of image tag
*	imgUrls[]	- array of img urls
*	currIndex	- the index in imgUrls[] of the currently displaying file
*	dumBuf		- used to preload images
*
* METHODS
*	getImgCount()		- returns number of images
*	getCurrIndex()		- returns index of current img
*	addUrl(url)			- adds image sr cto the list
*	preload()			- guess
*	showImg(index)		- shows img corr to .imgUrls[index]
*	next()				- shows next image
*	prevoius()			- shows prev image
****/
function ImageRotator(imgId) {
	this.imgId = imgId;
	this.imgUrls = new Array();
	this.imgAlts = new Array();
	this.currIndex = null;
	
	this.dumBuf = new Array();		// buffer for preloading images
	
	/*************************************************
	* o: Returns the number of images in the rotator
	*************************************************/
	this.getImgCount = function () {	
		return this.imgUrls.length;	
	}
	/*************************************************
	* o: Returns index of current image
	*************************************************/
	this.getCurrIndex = function () {	return this.currIndex;		}
	
	/********************************************
	* Adds an image to the roation list
	*********************************************/
	this.addImg = function (url, alt) {
		var cnt = this.getImgCount();
		this.imgUrls[cnt] = url;
		this.imgAlts[cnt] = alt;		
	}
	/********************************************
	* Preloads the images into a dummy buffer
	*********************************************/
	this.preload = function () {
		for (n=0; n<this.getImgCount(); n++) {
			this.dumBuf[n] = new Image();
			this.dumBuf[n].src = this.imgUrls[n];
		}
	}
	/*******************************************************
	* Show a particular image
	* i: index - index of .imgUrls[] of img to display
	* o: Returns false if index is OOB or imgId doesnt exist
	*	 Otherwise, sets source of the img with id=imgId 
	********************************************************/	
	this.showImg = function (index) {
		var img = document.getElementById(this.imgId);
		if (!img)
			return false;
		if (index >= this.getImgCount())
			return false;
		
		img.src = this.imgUrls[index];
		img.alt = this.imgAlts[index];
		if (typeof(img.title) != 'undefined')
			img.title = this.imgAlts[index];
		
		this.currIndex = index;
		return true;
	}
	/*******************************************************
	* Rotates the img to the next one in the list
	* o: Returns the output of the call to .showImg()
	********************************************************/	
	this.next = function () {
		// if nothing has been set then assume the second image
		if (this.currIndex == null)
			return this.showImg(1);

		// Otherwise show next image, or first if we're at the end
		else if ((this.currIndex+1) >= this.getImgCount())
			return this.showImg(0);
		else
			return this.showImg(this.currIndex+1);
	}
	/*******************************************************
	* Rotates the img to the prevoious one in the list
	* o: Returns the output of the call to .showImg()
	********************************************************/	
	this.previous = function () {
		// if nothing has been set then assume the last image
		if (this.currIndex == null)
			return this.showImg(this.getImgCount()-1);

		// Otherwise show next image, or first if we're at the end
		else if (this.currIndex==0)
			return this.showImg(this.getImgCount()-1);
		else
			return this.showImg(this.currIndex-1);
	}
	
}
