/*-------------------------------------------------------------------------
JsAni 1.0
Dirk Ginader für Bartenbach & Co. Werbeagentur GmbH & Co. KG
-------------------------------------------------------------------------*/
JsAni = function(objID,thisID,picFolder,picBaseName,picFileExt,picAmount){
	if(document.getElementById && document.createElement){		
		this.objID = objID;
		this.thisID = thisID;
		this.picFolder = picFolder;
		this.links = [];
		this.picBaseName = picBaseName;
		this.picFileExt = picFileExt;
		this.picAmount = picAmount;
		this.useTransition = false;
		this.transitionLenght = 1;
		this.frameDelay = 20;
		this.endDelay = this.frameDelay;
		this.autoReverse = false;
		this.playingReverse = false;
		this.activePic = 0;
		this.obj = document.getElementById(objID);
		this.loaded = false;
		this.isIE = document.all && navigator.userAgent.toLowerCase().indexOf('opera') == -1;
		this.preloadImages();
	}
}
JsAni.prototype.preloadImages = function(){
	var preloadHTML = "";
	this.lastImage = new Image();
	for(var i=0;i<this.picAmount;i++){
		this.lastImage.src = this.picFolder+"/"+this.picBaseName+(i+1)+this.picFileExt;
		preloadHTML += "<img src=\""+this.picFolder+"/"+this.picBaseName+(i+1)+this.picFileExt+"\" />";
	}
	this.container = document.createElement("div");
	if(this.container){
		this.container.id = this.thisID+"_container";
		this.container.style.display = "none";
		this.container.innerHTML = preloadHTML;
		document.getElementsByTagName("body")[0].appendChild(this.container);
	}
}
JsAni.prototype.checkLoaded = function(){
	this.images = this.container.getElementsByTagName("img");
	for(var i=0;i<document.images.length;i++){
		if(this.images.length >= this.picAmount-1){
			this.preloadImagesFinished();
		}
	}
}
JsAni.prototype.preloadImagesFinished = function(){
	this.loaded = true;
}
JsAni.prototype.play = function(){
	this.playing = true;	
	this.trigger();
}
JsAni.prototype.pause = function(){
	this.playing = false;	
}
JsAni.prototype.trigger = function(){
	if(this.loaded){
		if(this.playing){
			if(this.useTransition && this.isIE){
				this.obj.style.filter = this.getTransition();
				this.obj.filters.revealTrans.Apply();
			}
			this.obj.src = this.images[this.activePic].src;
			if (this.links.length >0){
				this.obj.parentNode.href = this.links[this.activePic];
			}
			if(this.useTransition && this.isIE){
				this.obj.filters.revealTrans.play();
			}
			if(this.playingReverse){
				this.activePic--;
			}else{
				this.activePic++;
			}
			if(this.activePic > this.images.length-1){
				if(this.autoReverse){
					this.playingReverse = true;
				}else{					
					this.activePic = 0;
				}
				window.setTimeout(this.thisID+".trigger()",this.endDelay);
			}else if(this.activePic <= 0){
				this.playingReverse = false;
				window.setTimeout(this.thisID+".trigger()",this.endDelay);
			}else{
				window.setTimeout(this.thisID+".trigger()",this.frameDelay);
			}
		}
	}else{
		this.checkLoaded();
		window.setTimeout(this.thisID+".trigger()",1000);
	}
}
JsAni.prototype.getTransition = function(){
	return "revealTrans(Duration="+(this.transitionLenght/1000)+",Transition="+this.useTransition+");";
}