

function textToHtml(text){
	var newtext="";
	var i;
	text=text.replace(/&/,"&amp;");
	text=text.replace(/>/,"&gt;");
	text=text.replace(/</,"&lt;");
        text=text.replace(/\r/g,"");
	text=text.split("\n");
	return "<p>" + text.join("</p><p>");
      }

      function SlideShow(div){

          this.div=div;
	  this.div.innerHTML="";
	  this.fotoset=this.div.getAttribute("_amfotoset");
	  this.autostart=this.div.getAttribute("_amautostart");

	  var collections=imageDatabase.collections;
	  var i;
	  this.collection=null;
	  var me=this;

	  this.waitingforbg=-1;
	  this.waitingforfg=-1;


	  //hu en br
	  this.imageready=function(foto){
	    if(this.waitingforbg==foto.index){
	      this.imgdiv.style.backgroundImage="url(" + foto.url+")";
	      this.waitingforbg=-1;
	      
	    }

	    if(this.waitingforfg==foto.index){
	      this.waitingforfg=-1;
	      setOpacity(this.img,0);
              this.img.src=foto.url;
	      this.fader.fade(this.img,0,100,900,function(){me.imgdiv.style.backgroundImage="none"})
	      this.flick();
	    }
	  }
	  this.load=function(index){
	    if(this.fotos[index].loader){
	      if(this.fotos[index].loader.loaded){
                 this.imageready(this.fotos[index]);
	      }
	    }else{
	      var foto=this.fotos[index];
	      foto.loader=new loader(this.fotos[index],this);
	    }
	  }
	  this.setfg=function(index){
	    this.waitingforfg=index;
	    this.load(index);
	  }
	  this.setbg=function(index){
	    this.imgdiv.style.backgroundImage="url(" + this.fotos[index].url +")";
//	    this.waitingforbg=index;
//	    this.load(index);
	  }

	  for(i=0;i<collections.length;i++){
	    if(this.fotoset== collections[i].id){
	      this.collection=collections[i];
	      this.fotos=this.collection.fotos;
	    }
	  }

          this.headdiv=document.createElement("div");
	  this.div.appendChild(this.headdiv);
	  this.headdiv.className="slideshowhead";
	  if(! this.collection){
	    this.headdiv.innerHTML="Fout: fotoset niet gevonden";
	    return;
	  }
	  if(this.collection.fotos.length==0){
            this.headdiv.innerHTML="<small>De fotoset: " + this.collection.name + " is leeg</small>";
	    return;
	  }else{
            this.headdiv.innerHTML="<small>" + this.collection.name + "</small>";
	    this.currentindex=0;
	  }

	  for(i=0;i<this.fotos.length;i++){
	    this.fotos[i].url=imageDatabase.file.root +"/img-" + this.collection.fotos[i].id+".jpg"
	    this.fotos[i].index=i;
	  }
	  this.fader=new fader();

	  this.progress=document.createElement("span");
	  this.progress.innerHTML="1/"+this.collection.fotos.length;
	  this.progress.className="slideshowprogress";

	  this.headdiv.appendChild(this.progress);



	  this.knoppendiv=document.createElement("div");
	  this.knoppendiv.className="slideshowbuttonpanel";

	  this.startbutton=document.createElement("button");
	  this.startbutton.innerHTML="start";
	  this.startbutton.show=this;
	  this.startbutton.onclick=function(){
               this.show.startstop();
	  }
	  this.knoppendiv.appendChild(this.startbutton);

/*	  this.stopbutton=document.createElement("button");
	  this.stopbutton.innerHTML="stop";
	  this.stopbutton.show=this;
	  this.stopbutton.onclick=function(){
               alert(this.show.fotoset);
	  }
	  this.knoppendiv.appendChild(this.stopbutton);
*/
	  this.previousbutton=document.createElement("button");
	  this.previousbutton.innerHTML="vorige";
	  this.previousbutton.show=this;
	  this.previousbutton.onclick=function(){
               this.show.previous();
	  }
	  this.knoppendiv.appendChild(this.previousbutton);


	  this.nextbutton=document.createElement("button");
	  this.nextbutton.innerHTML="volgende";
	  this.nextbutton.show=this;
	  this.nextbutton.onclick=function(){
               this.show.nextbutton();
	  }
	  this.knoppendiv.appendChild(this.nextbutton);


     // Voeg de afbeelding in
          this.imgdiv=document.createElement("div");
	  this.imgdiv.className="slideshowimgdiv";
	  this.div.appendChild(this.imgdiv);


	  this.img=document.createElement("img");
//	  this.fotos[this.currentindex].loader=new loader(this.fotos[this.currentindex],this,function(){me.imageready(me.currentindex)});
//	  this.waitingforfg=this.currentindex;
          this.setfg(0);
	  this.setbg(0);
	  //this.img.src=imageDatabase.file.root +"/img-" + this.collection.fotos[this.currentindex].id+".jpg";
	  this.imgdiv.appendChild(this.img);
     // Voeg de knoppendiv in
	  var el=document.createElement("br");
	  el.setAttribute("clear","all");
	  this.div.appendChild(el);
	  this.div.appendChild(this.knoppendiv);

     // Voeg de tekst div in
     // var el=document.createElement("br");
     //  el.setAttribute("clear","all");
     //  this.div.appendChild(el);

	  this.textdiv=document.createElement("div");
	  this.textdiv.className="slideshowtextbox";
	  this.div.appendChild(this.textdiv);

	  this.progdiv=document.createElement("div");
	  this.progdiv.className="slideshowprogdiv";
	  this.textdiv.appendChild(this.progdiv);


     // De kop
	  this.header=document.createElement("h1");
	  this.header.innerHTML=this.collection.fotos[0].kop;
	  this.textdiv.appendChild(this.header);
     // De body
	  this.text=document.createElement("div");
	  this.text.innerHTML=textToHtml(this.collection.fotos[0].bijschrift);
	  this.textdiv.appendChild(this.text);
	  


          this.nextbutton=function(){
	    this.stop();
	    this.next();
	  }

	  this.next=function(){
	    this.setbg(this.currentindex);
	    if(this.currentindex < this.collection.fotos.length-1){
	      this.currentindex++;
	     
	    }else{
	      this.currentindex=0;
	    }
	    setOpacity(this.img,0);
	    this.setfg(this.currentindex);
	  }

	  this.previous=function(){
	    this.stop();
	    this.setbg(this.currentindex);
	    if(this.currentindex > 0){
              this.currentindex--;
	    }else{
	      this.currentindex=this.collection.fotos.length-1;
	    }
	    setOpacity(this.img,0);
	    this.setfg(this.currentindex);
	  }

	  this.startstop=function(){
             if(this.startbutton.innerHTML != "stop"){
	       this.start();
	     }else{
	       this.stop();
	     }
	  }

	  this.step=function(){
	    this.countdown++;
	    if(this.countdown>10){
	      this.countdown=0;
	      this.next();
	    }
	    this.progdiv.innerHTML=10-this.countdown;

	  }

	  this.start=function(){
	      this.startbutton.innerHTML="stop";
	      var mi=this;
	      this.countdown=0;
	      this.interval=setInterval(function(){
				mi.step();
			      },600);	

	  }
	  this.stop=function(){
	      this.startbutton.innerHTML="start";
	      clearInterval(this.interval);

	  }

	  this.flick=function(){
	      this.progress.innerHTML=this.currentindex+1 + "/" +this.collection.fotos.length;
	      this.header.innerHTML=this.collection.fotos[this.currentindex].kop;
	      this.text.innerHTML=textToHtml(this.collection.fotos[this.currentindex].bijschrift);
	  }
      
        if(this.autostart == "yes"){
	  this.start();
	}

      }

      var slideshows=[];
      function slideShowsInitialize(){
	var divs=document.getElementsByTagName("DIV");
	var div;
        for(div=0;div<divs.length;div++){
	  if(divs[div].className=="slideshow"){
	    show=new SlideShow(divs[div]);;
	    slideshows.push(show);	  
	  }
	}

      }
      var imageDatabase=null

      if(window.location.href.match(/https:\/\/(dev\.)*antimatter/)){
       imageDatabase = new JsonProc({"url":"/am/brilserv.cgi",
                                "action":"get-database",
                                "filename":"database"
                                },
	  function(retval){
	    if(!retval){
	       alert("Duh!");
	    }else{
	     imageDatabase=retval.database;
	     slideShowsInitialize();
//     	     setInterval("fade(slideshows[0].img,00,100,1500)",500);
	    }
	  }
	);

      }else{

       imageDatabase = new JsonGet(
	    "/database.json?iecache="+(new Date()).getTime(),
	  function(retval){
	    if(!retval){
	       alert("Duh!");
	    }else{
	     imageDatabase=retval;
	     slideShowsInitialize();
//     	     setInterval("fade(slideshows[0].img,00,100,1500)",500);
	    }
	  }
	);
      }
      //change the opacity for different browsers
      function setOpacity(el,opacity) {
	if(el){
	  el.style.opacity = (opacity / 100);
	  el.style.MozOpacity = (opacity / 100);
	  el.style.KhtmlOpacity = (opacity / 100);
	  el.style.filter = "alpha(opacity=" + opacity + ")";
	}
      }


      function fader(){

       this.done=true;
       this.fade=function(el,from,to,time,afterfade){
         if(afterfade){
	   this.afterfade=afterfade;
	 }else{
	   afterfade=null;
	 }
	 this.start=(new Date().getTime());
	 this.rate=(to-from)/time;
	 this.done=false;
	 this.el=el;
	 this.from=from;
	 this.to=to;
	 this.time=time;
	 setOpacity(el,from); //was uitgecommentarieerd
	 var me=this;
	 setTimeout(function(){me.fadestep(me)},10);
       }

       this.fadestep=function(me){
	  var now=(new Date()).getTime();
          if(me.done){

	  }else{
	    if(me.start+me.time<now){
	      setOpacity(me.el,me.to);
	      if(me.afterfade){
		me.afterfade();
	      }
	    }else{
	      setOpacity(me.el,(now-me.start)*me.rate);
	      setTimeout(function(){me.fadestep(me)},10);
	    }
	  }
       }

      

       return this;
      }



   function loader(foto,slideshow,oncomplete){
 
      this.foto=foto;
      this.slideshow=slideshow;
      this.image=new Image();
      this.tries=0;
      this.loaded=false;
      this.oncomplete=oncomplete;
      var me=this;
      this.image.src=this.foto.url ;
      this.image.onload=function(){
	me.loaded=true;
	me.slideshow.imageready(me.foto);
      }
      this.onerror=function(){
	if(me.tries<3){
	  me.tries++;
	  me.image.src= me.foto.url + "?retry="+me.tries;
	  me.image.onerror=me.onerror;
	}else{
	  alert("Fout bij het laden van de afbeelding, mogelijk is de verbinding verbroken. Herlaad de pagina (F5) om het eventueel opnieuw te proberen.\n" + me.foto.url);
	}
      }
      this.image.onerror=this.onerror;
   }




  //<!--[F9 perl uploader]-->

