var ImageTransparentEffect = Class.create();

ImageTransparentEffect.prototype = {
	
	imageArray: '',
	anzImages: 0,
	aktImage: 0,
	visibleImage: 0,
	timeOut: 5000,
	objTimer: '',
	opacityMin: 0.2,
	opacityMax: 1.0,
	fade: false,
	
	initialize: function(aImageArray) {
		
		if(aImageArray.length > 0){
			this.imageArray = aImageArray;
			this.anzImages = aImageArray.length;
			this.fade = true;
			
			var thisObj = this;
			for(var i=0; i<this.anzImages; i++){
				Element.setOpacity($(this.imageArray[i]), this.opacityMin);
				
				$(this.imageArray[i]).onmouseover = function(){
					for(var j=0; j<thisObj.anzImages; j++){
						if($(this).id == thisObj.imageArray[j]){
							thisObj.hoverImage(j);
						}
					}
				};
				
				$(this.imageArray[i]).onmouseout = function(){ 
					thisObj.resetImages();
					thisObj.changeImage();  
				};
			}
			Element.setOpacity($(this.imageArray[0]), this.opacityMax);
		}
	},
	
	startTimer : function(){
		var thisObj = this;
		if(this.objTimer)
			window.clearTimeout(this.objTimer);
		if(this.fade == true)
			this.objTimer = window.setTimeout(this.changeImage.bind(this), this.timeOut);
	},
	
	stopTimer : function(){
		this.fade = false;
		if(this.objTimer)
			window.clearTimeout(this.objTimer);
	},
	
	hoverImage : function(imageNr){
		this.stopTimer();
		this.aktImage = imageNr;
		this.displayImages2();
		this.resetImages();
	},
	
	changeImage : function(){		
			if(this.aktImage+1 == this.anzImages)
				this.aktImage = 0;
			else
				this.aktImage = this.aktImage+1;
			
			this.fade = true;
			this.displayImages();
		
	},
	
	resetImages : function(){
		var thisObj = this;
		for(var i=0; i<this.anzImages; i++){
			if(this.imageArray[this.aktImage] == this.imageArray[i]){
				new Effect.Opacity($(this.imageArray[i]), {duration:0.0, from:thisObj.opacityMin, to:thisObj.opacityMax});
			}
			else{
				new Effect.Opacity($(this.imageArray[i]), {duration:0.0, from:thisObj.opacityMax, to:thisObj.opacityMin});				
			}
			
		}
	},
	
	displayImages : function(){
		if(this.aktImage != this.visibleImage){
				var thisObj = this;
				new Effect.Opacity($(this.imageArray[this.visibleImage]), {duration:0.6, from:thisObj.opacityMax, to:thisObj.opacityMin});
				new Effect.Opacity($(this.imageArray[this.aktImage]), {duration:0.6, from:thisObj.opacityMin, to:thisObj.opacityMax});
				this.visibleImage = this.aktImage;
				if(this.fade = true)
					this.startTimer();
		}		
	},
	
	displayImages2 : function(){
		if(this.aktImage != this.visibleImage){
				var thisObj = this;
				new Effect.Opacity($(this.imageArray[this.aktImage]), {duration:0.0, from:thisObj.opacityMin, to:thisObj.opacityMax});
				new Effect.Opacity($(this.imageArray[this.visibleImage]), {duration:0.0, from:thisObj.opacityMax, to:thisObj.opacityMin});
				this.visibleImage = this.aktImage;
				this.stopTimer();
		}
	}
}


function initImageTransparentEffect(){
	var myImageTransparentEffect = new ImageTransparentEffect(imageTransparentArray);
	myImageTransparentEffect.startTimer();
}