// JavaScript Document
function sShow(ph,slides,time,typ,nav){
	if(slides.length>1){
		this.ph=ph;
		this.slides=slides;
		this.nav=nav;
		this.time=time;
		this.wait=false;
		this.sh;
		this.typ=typ;
		this.a=0;
		this.run=false;
		this.sh_ld=false;
		this.ph.each(function(item,n){
			item.fd=new Fx.Tween(item,{duration: 2000, wait: false});
			item.fd.addEvent('onComplete',function(item){
				this.swap()						   
			}.bind(this));
			item.fd_in=new Fx.Tween(item,{duration: 2000, wait: false});
			//if(n)item.fd.set('opacity',0);
			//item.addEvent('click',function(){alert('auww')});
		}.bind(this));
		var n=this.getAct();
		this.n=((n< this.slides.length-1)? n+1 : 0);
		this.ld(1,'play');
	}
}

sShow.prototype.start=function(tm){
	this.run=true;
	this.sh=this.nxt.delay(tm,this);
}

sShow.prototype.getAct=function(){
    var src=String(this.ph[this.a].getElement('img').src).split(root)[1];
    for(var i=0; i<this.slides.length; i++){
	
	if(this.slides[i].img==src)return i;
    }
}

sShow.prototype.ld=function(m,state){
	var once=false;
	var dim=show_getDim($('ss'),this.slides[this.n].w, this.slides[this.n].h,this.typ);
	var im= new Asset.image(/*this.path+*/this.slides[this.n].img,{'width':dim.w,'height':dim.h,
		onload: function(){
			if(once){
				return;
			}else{
			    
				once = true;
				this.ph[m].grab(im);
				this.ph[m].dim={'w':dim.w,'h':dim.h};
				this.ph[m].ld=true;
				this.ph[m].n=this.n;
				this.n=((this.n<(this.slides.length-1))? this.n+1 : 0);
				switch(state){
					case 'play':
						if(this.wait){
							this.wait=false;
							this.sh=this.nxt.delay(this.time,this);
							this.nxt();
						}
					break;
					case 'flip':
						
						this.shTxt(this.slides[this.n].txt);
						$('load').setStyle('display','none');
						if(this.run){
							var n=((this.a)? 0 : 1);
							if(this.ph[n].ld){
								this.ph[n].ld=false;
								this.ph[n].getElement('img').dispose();
							}
							this.ld(n,'play');	
							this.sh=this.nxt.delay(this.time,this);
						}
					break;
					case 'jump':
						if(this.sh_ld)$clear(this.sh_ld);
						im.replaces(this.ph[m].getElement('img'));
						this.nxt();
						
					break;	
				}
				
			}
		}.bind(this)
	});
}

sShow.prototype.jump=function(p){
	var n=((this.a)? 0 : 1);
	if(this.run){
		$clear(this.sh);
		this.run=false;
		if(this.ph[n].getStyle('opacity')<1)this.ph[n].fd.cancel();
	}
	var pp=((p)? p-1 : this.slides.length-1);
	if(this.ph[n].n==p){
		//alert(p+' :: '+this.getAct());
		this.nxt();
	}else{
		this.n=p;
		this.ld(n,'jump');
		this.sh_ld=this.show_load.delay(500,this,1);
	}
}

sShow.prototype.show_load=function(n){
	$('s2').setStyles({'background':((n)? '#111' : 'none'),'opacity':'0.7'});
	$('load').setStyles({'display':((n)? 'block' : 'none')});
}




sShow.prototype.nxt=function(){
	var n=((this.a)? 0 : 1);
	if(this.ph[n].ld){
		this.ph[this.a].fd.options.duration=((this.run)? 2500 : 500);
		this.ph[this.a].fd.set('opacity',1);
		this.ph[this.a].fd.start('opacity',1,0);
	}else{
		//alert('else');
		this.wait=true;
	}
}

sShow.prototype.swap=function(){
	this.show_load(0);
	this.ph[this.a].getElements('img').dispose();
	this.ph[this.a].fd.set('opacity',1);
	this.ph[this.a].setStyle('z-index',9);
	this.ph[this.a].ld=false;
	//if(this.run)
	this.ld(this.a,'play');
	this.a=((this.a)? 0 : 1);
	this.ph[this.a].setStyle('z-index',10);
	this.nav.update(this.getAct());
	//this.shTxt(this.slides[this.getAct()].txt);
	if(this.run)this.sh=this.nxt.delay(this.time,this);
}


show_getDim=function(pr,w,h,typ){
    var dim=pr.getSize();
    var f=(dim.y/dim.x);
		if(f<(h/w)){
			ff=((typ=='scale')? dim.y/h :dim.x/w) ;
		}else{
			ff=((typ=='scale')?dim.x/w : dim.y/h );
		}
		h=h*ff;
		w=w*ff;
    return {'w':w,'h':h};
}

sShow.prototype.shTxt=function(txt){
	$('f_g').getElement('.bijschrift').setStyle('display',((txt)? 'inline' : 'none'));
	$('f_g').getElement('.bijschrift').set('text',txt);
}
sShow.prototype.pause=function(){
	$clear(this.sh);
	this.run=false;
	if(this.ph[this.a].getStyle('opacity')<1){
		this.ph[this.a].fd.cancel();
		this.swap();
	}/*else{
		var n=((this.a)? 0 : 1);
		if(this.ph[n].ld){
			this.ph[n].ld=false;
			this.ph[n].getElement('img').dispose();
		}
	}*/
}
/*
sShow.prototype.play=function(){
	var n=this.getAct();
	this.n=((n< this.slides.length-1)? n+1 : 0);
	var n=((this.a)? 0 : 1);
	if(this.ph[n].ld){
		this.ph[n].ld=false;
		this.ph[n].getElement('img').dispose();
	}
	this.ld(n,'play');
	this.run=true;
	this.sh=this.nxt.delay(500,this);
}

sShow.prototype.flip=function(typ){
	if(this.run){
		$clear(this.sh);
		if(this.ph[this.a].getStyle('opacity')<1){
			this.run=false;
			this.ph[this.a].fd.cancel();
			this.swap();
			this.run=true;
		}
	}
	var n=this.getAct();
	if(typ=='fwd'){
		this.n=((n< this.slides.length-1)? n+1 : 0);
	}else{
		this.n=((n)? n-1 : this.slides.length-1);
	}
	$('load').setStyle('display','inline');
	this.ld(this.a,'flip');
}
*/