//-----------------------//
//--------SlideSow------//
/* by marox, marc bissegger /*
/* based on moontools */

var SlideShow = new Class({
	initialize: function(container, images, duration){
		this.container =$(container);
		this.container.setStyles({
			position: 'relative',
			overflow: 'hidden'
		});
		if(duration){
			this.duration=duration;
		}
		else{
			this.duration = 1000;
		}
		this.images=images;
		this.loading = new Element('div').addClass("loading").setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: 3,
			display: 'none',
			width: this.container.getStyle('width'),
			height: this.container.getStyle('height')
		}).injectInside(this.container);
		this.oldImage = new Element('div').setStyles({
			position: 'absolute',
			overflow: 'hidden',
			top: 0,
			left: 0,
			opacity: 0,
			width: this.container.getStyle('width'),
			height: this.container.getStyle('height')
		}).injectInside(this.container);
		this.newImage = this.oldImage.clone();
		this.newImage.injectInside(this.container);
		this.imageObjs=new Array();
		this.imageCnt = 0;
		this.stopped=false;
		
	},
	loadImage:function(){
			this.stopped=true;
			//this.loading.setStyle('display','block');
			delete this.imageObj;
			this.newIma=false;
			this.loaded = true;
			if(this.imageObjs[this.imageCnt]){
				this.imageObj=this.imageObjs[this.imageCnt];
				this.showImage();
			}
			else{
				this.newIma=true;
				this.imageObj = new Asset.image(this.images[this.imageCnt], {onload: this.showImage.bind(this)});
			}
	},
	showImage:function(){
		if(this.loaded == true){
			this.loaded=false;
			
			if(this.newIma){
				this.imageObjs[this.imageCnt]=this.imageObj;
			}
			this.newImage.setStyles({
				zIndex: 1,
				opacity: 0
			});
			var img = this.newImage.getElement('img');
			if(img){
				img.replaceWith(this.imageObj.clone());
			}else{
				var obj = this.imageObj.clone();
				obj.injectInside(this.newImage);
			}
			this.loading.setStyle('display','none');
			this.fade();
		}
	},
	fade:function(){
		this.setNumbers();
		this.effectObj = this.newImage.effects({
			duration: this.duration,
			transition: Fx.Transitions.linear
		});
		this.effectObj.start({
			opacity: [0,1]
		});
		this.effectObj2 = this.oldImage.effects({
			duration: this.duration,
			transition: Fx.Transitions.linear
		});
		this.effectObj2.start({
			opacity: [1,0]
		});
		this.resetStopped.delay(this.duration,this);
	},
	next:function(){
		if(!this.stopped){
			this.imageCnt++;
			if(this.imageCnt >= this.images.length){
				history.back();
				//this.imageCnt=0;
			}
			this.prepareImage();
			this.loadImage();
		}
	},
	previous:function(){
		if(!this.stopped){
			this.imageCnt--;
			if(this.imageCnt < 0){
				history.back();
				//this.imageCnt=this.images.length-1;
			}
			this.prepareImage();
			this.loadImage();
		}
	},
	prepareImage:function(){
		var img = this.oldImage.getElement('img');
		if(img){
			img.replaceWith(this.imageObj.clone());
		}else{
			var obj = this.imageObj.clone();
			obj.injectInside(this.oldImage);
		}
		
		this.oldImage.setStyles({
			zIndex: 0,
			top: 0,
			left: 0,
			opacity: 1
		});
		
		this.newImage.setStyles({opacity:0});
	},
	setNumbers:function(){
		if(this.images.length>1){
			var myCnt=this.imageCnt+1;
			if(myCnt<10){
				$('digit1').src="/pics/numbers/n0.gif";
				$('digit2').src="/pics/numbers/n"+myCnt+".gif";
			}
			else{
				var zahl=myCnt.toString();
				$('digit1').src="/pics/numbers/n"+zahl.substr(0,1)+".gif";
				$('digit2').src="/pics/numbers/n"+zahl.substr(1,1)+".gif";
				
			}
			if(this.oldLegend){
				$(this.oldLegend).setStyle('display','none');
			}
			$("l"+this.imageCnt).setStyle('display','block');
			this.oldLegend="l"+this.imageCnt;
		}
	},
	resetStopped:function(){
		this.stopped=false;
	}

});
