// JavaScript Document
function sShow(ph,slides,time,path){
	if(slides.length>1){
		this.path=path;
		this.ph=ph;
		this.slides=slides;
		this.time=time;
		this.wait;
		this.sh;
		this.a=0;
		this.run=true;
		this.ph.each(function(item){
			item.fd=new Fx.Tween(item,{duration: 2000, wait: false});
			item.fd.addEvent('onComplete',function(item){
				this.swap()						   
			}.bind(this));
			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');
		this.sh=this.nxt.delay(this.time,this);
	}else{
	    return null;
	}
}
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.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.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');
}

sShow.prototype.getAct=function(){
    var src=String(this.ph[this.a].getElement('img').src).split(this.path)[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=this.getDim(this.slides[this.n].w, this.slides[this.n].h);
	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].ld=true;
				switch(state){
					case 'play':
						if(this.wait){
							this.wait=false;
							this.nxt();
						}
					break;
					case 'flip':
						im.replaces(this.ph[m].getElement('img'));
						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;
				}
				this.n=((this.n<(this.slides.length-1))? this.n+1 : 0);
			}
		}.bind(this)
	});
}

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

sShow.prototype.swap=function(){
	this.ph[this.a].getElements('img').dispose();
	this.ph[this.a].fd.set('opacity',1);
	this.ph[this.a].setStyle('z-index',900);
	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',1000);
	//this.shTxt(this.slides[this.getAct()].txt);
	if(this.run)this.sh=this.nxt.delay(this.time,this);
}
sShow.prototype.getDim=function(w,h){
    if(w>h && (h*(512/w))<384){
	h=Math.round(h*(512/w));
	w=512;
    }else{
	w=Math.round(w*(384/h));
	h=384;
    }
    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);
}