function ini_SlideShow(ss){
	var slides=[];
	var link=ss.getElement('a').href;
	ss.getElements('input').each(function(item,n){
		// eerste inpput overslaan bevat het pad
		if(n){
			var data=String(item.value).split('#');										
			slides.push({'img':data[0],'w':data[1],'h':data[2],'art':data[3],'work':data[4]});
		}
	});
	for(var i=0; i<3; i++){
		var div= new Element('div', {
    		'id': 's'+i,
   			 'class': 'slide'
		});
		if(!i){
			div.grab(ss.getElement('img'));
		}else if(i==2){
			var ld=new Element('div', {
    			'id': 'load'
			});
			div.grab(ld);
			div.link=String(link).split('_artwork_').join(slides[0].work);
			div.addEvent('click',function(){
				window.location=this.link;
			});
		}
		ss.grab(div);
	}
	var ph=[$('s0'),$('s1')];
	var buttons=[];
	ss.getParent().getParent().getElements('.nr').each(function(nr){
		if(!nr.hasClass('bg_fff'))buttons.push(nr);
	})
	var txt=ss.getParent().getElement('.cap');
	var show_nav=new sNav(buttons);
	var img_path=ss.getElement('input').value;
	show=new sShow(ph,slides,4000, img_path,'',show_nav,txt,link,$('s2'));

}

function ini_ss_nav(ss){
	var buts=[];
	ss.getParent().getAllPrevious('.kol').each(function(k,n){
		k.getElements('.nr').each(function(nr,p){
			buttons.push(nr);
			if(!nr.hasClass('bg_fff')){
				nr.act=false;
				nr.addEvent('mouseover',function(){
					if(!this.act){
						this.setStyle('background','#ef4136');
					}
				});
				nr.addEvent('mouseout',function(){
					if(!this.act){
						this.setStyle('background','none');
					}
				})
			}
		})
	});
}



// JavaScript Document
function sShow(ph,slides,time,path,typ,nav,txt,link,loader){
	if(slides.length>1){
		this.path=path;
		this.ph=ph;
		this.slides=slides;
		this.nav=nav;
		this.txt=txt;
		this.time=time;
		this.wait=false;
		this.link=link;
		this.loader=loader;
		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});
		}.bind(this));
		var n=0;

		this.n=((n< this.slides.length-1)? n+1 : 0);
		this.ld(1,'play');
		this.start(4000)
	}else{
	    return null;
	}
}

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(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);
				im.setProperties({'width':dim.w,'height':dim.h});
				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){
		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{
		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;
	this.ld(this.a,'play');
	this.a=((this.a)? 0 : 1);
	this.ph[this.a].setStyle('z-index',10);
	var n=this.getAct();
	this.nav.update(n);
	var link=String(this.link).split('_artwork_').join(this.slides[n].img);
	if(type=='project'){
		this.txt.set('text',this.slides[n].work);
	}else{
		this.txt.set('html','<a href="'+link+'"><h1>'+this.slides[n].art+'</h1></a><a href="'+link+'"><h3>'+this.slides[n].work+'</h3></a>');
	}
	this.loader.link=link;
	if(this.run)this.sh=this.nxt.delay(this.time,this);
}
sShow.prototype.getDim=function(w,h){
	var tw=((type=='project')? 580 : 420);
	var th=((type=='project')? 400 : 360);
	if(w>h && (h*(tw/w))<th){
	    h=Math.round(h*(tw/w));
	    w=tw;
	}else{
	    w=Math.round(w*(th/h));
	    h=th;
	}
	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();
	}
}

function sNav(buttons){
	if(buttons.length>1){
		var tmp=new Array(buttons.length);
		this.act=0;
		var half=Math.ceil(buttons.length/2);
		buttons.each(function(b,n){
			if(!n)b.setStyle('background','#ef4136');
			var p=((type=='project')? n : ((n<half)? n*2 : 1+((n-half)*2)));
			tmp[p]=b;
			b.act=false;
			b.n=p;
			b.addEvent('mouseover',function(){
				if(!this.act){
					this.setStyle('background','#ef4136');
				}
			});
			b.addEvent('mouseout',function(){
				if(!this.act){
					this.setStyle('background','none');
				}
			});
			b.addEvent('click',function(){
				if(!this.act){
					show.jump(this.n);
				}
			});
		})
		this.buttons=tmp;
	}else{
	    return null;
	}
}

sNav.prototype.update=function(n){
	this.buttons[this.act].act=false;
	this.buttons[this.act].setStyle('background','none');
	this.act=n;
	this.buttons[this.act].act=true;
	this.buttons[this.act].setStyle('background','#ef4136');
}




