// JavaScript Document
function sShow(ph,slides,time){
	if(slides.length>1){
		this.ph=ph;
		this.slides=slides;
		this.time=time;
		this.wait;
		this.sh;
		this.a=0;
		this.run=false;
		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,'pause');
	}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('http://www.roelofheida-architecten.nl')[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 im= new Asset.image(this.slides[this.n].img,{'height':'100%',
		onload: function(){
			if(once){
				return;
			}else{
				once = true;
				im.erase('height');
				im.erase('width');
				
				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.shTxt=function(txt){
	$('f_g').getElement('.bijschrift').setStyle('display',((txt)? 'inline' : 'none'));
	$('f_g').getElement('.bijschrift').set('text',txt);
}
