/**
 *	@author: Daniel Jakobsson <fake_healer@hotmail.com>
 *	@created: 2005-03-31
 *	
 *	
 *	Description:
 *	Object to handle animated image swapping
 *	Works with filters for IE and simple swapping otherwise
 *	As many images as need can be added. addImage(src[, id])
 *	An inter image can also be added: setInterImage(src)
 *	
 *	When a swapping occurs, an event is triggered: onSwap(index, imgsrc, imgid)
 *	
 *	To use a filter:
 *	Call setUseFilter(true) to enable it.
 *	Set the htmlElId, in the constructor or using the setHtmlElId(id) method.
 *	Filters only works for >=IE5
 *	
 *	See example of usage in the end of this document.
 *	
 *	@param instance	String with the global name of the instance
 *	@param imgId	String with the id for the html <img> element
 *	@param elId		String with the id for the html element that contains the <img>
 *					Only needed for usa of filter
 *	
 */
function ImageSwapper(instance, imgId, elId) {
	var images=[];
	var imageIds=[];
	var interImage = null;
	var imgElId=imgId;
	var htmlElId=(typeof elId=="undefined"?"":elId);
	var currImage=-1;
	var imageDelay=5000;
	var interDelay=1000;
	var useFilter=false;
	var timer=0;

	/**
	 *	Add images to be swapped.
	 *	@param src	String with the src of the image
	 *	@param id	String with an id of this image. The purpose is to be able
	 *				determine which the current image shown is. To do this,
	 *				use the getCurrentId() method.
	 *	
	 */
	this.addImage=function (src,id) {
		var idx=images.length;
		images[idx]=new Image();
		images[idx].src=src;
		imageIds[idx]=id;
	}
	/**
	 *	The inter image is used to separate the images of the swapping serie.
	 */
	this.setInterImage=function (src) {
		interImage=new Image();
		interImage.src=src;
	}
	this.setImgElId=function (id) {
		imgElId=id;
	}
	this.setHtmlElId=function (id) {
		htmlElId=id;
	}
	this.setImageDelay=function (milliseconds) {
		imageDelay=milliseconds;
	}
	this.setInterDelay=function (milliseconds) {
		interDelay=milliseconds;
	}
	/**
	 *	@param use	boolean, use filter or not
	 */
	this.setUseFilter=function (use) {
		useFilter=use;
	}
	/**
	 *	If id is not used, or doesn`t exist -1 will be returned.
	 */
	this.getCurrentId=function () {
		if (imageIds.length>0 && currImage!=-1 && typeof imageIds[currImage]!="undefined") {
			return imageIds[currImage];
		}
		return -1;
	}
	this.animate=function () {
		if (images.length == 0) {
			alert('No images added!');
			return;
		}
		currImage++;
		if (currImage >= images.length) {
			currImage=0;
		}

		this.doFilter(images[currImage].src);
		if (this.onSwap) {
			this.onSwap(currImage, images[currImage].src, this.getCurrentId());
		}
		if (interImage!=null) {
			timer=setTimeout(instance+".aniInterImage();", imageDelay);
		} else {
			timer=setTimeout(instance+".animate();", imageDelay);
		}
	}
	this.aniInterImage=function () {
		this.doFilter(interImage.src);
		timer=setTimeout(instance+".animate();", interDelay);
	}
	this.doFilter=function (src) {
		var imgEl=document.getElementById(imgElId);
		var htmlEl=document.getElementById(htmlElId);
		if (useFilter && document.all && document.getElementById && htmlEl) {
			htmlEl.filters[0].apply();
			imgEl.src=src;
			htmlEl.filters[0].play();
		} else {
			imgEl.src=src;
		}
	}

	this.stop=function () {
		killTimer();
		if (currImage!=-1) {
			currImage=0;
		}
	}

	function killTimer() {
		if (timer) {
			clearTimeout(timer);
			timer=0;
		}
	}

	this.onSwap = null;



}

/**
 *	Examples:
 *	
 *	
 *	Simple example
 *	
 *	Code:
 *	var imgSwapper = new ImageSwapper("imgSwapper", "home_ImgReceitaMes");
 *	imgSwapper.addImage('imgRec/rec60.jpg');
 *	imgSwapper.addImage('imgRec/rec69.jpg');
 *	imgSwapper.setImageDelay(5000);
 *	
 *	HTML:
 *	<img id="home_ImgReceitaMes" border=0 src="sp.gif" width=247 height=126>
 *	
 *	
 *	
 *	Example with inter image and filter
 *	
 *	Code:
 *	var imgSwapper = new ImageSwapper("imgSwapper", "home_ImgReceitaMes", "home_TdReceitaMes");
 *	imgSwapper.setInterImage('imagem/noise.gif');
 *	imgSwapper.addImage('imgRec/rec60.jpg');
 *	imgSwapper.addImage('imgRec/rec69.jpg');
 *	imgSwapper.setImageDelay(5000);
 *	imgSwapper.setInterDelay(200);
 *	
 *	HTML:
 *	<table border=0 cellspacing=0 cellpadding=0>
 *	<tr>
 *		<td id="home_TdReceitaMes" style="filter:progid:DXImageTransform.Microsoft.Fade(Overlap=1.00);">
 *		<a href="#" onclick="home_abreReceitaDoMes();">
 *		<img id="home_ImgReceitaMes" onError="this.src='imagem/priReceitaImagem.jpg'" border=0 src="sp.gif" width=247 height=126></a></td>
 *	</tr>
 *	</table>
 *	
 */



