/* (pre)load images */
xhr('GET','./include/randomGallerie.php', 'id=1');
xhr('GET','./include/randomGallerie.php', 'id=2');
xhr('GET','./include/randomGallerie.php', 'id=3');
xhr('GET','./include/randomGallerie.php', 'id=4');
xhr('GET','./include/randomGallerie.php', 'id=5');

var slideshow_active = 1;

function slideshow_pause(status){
	var buttons = document.getElementById('slideshow-container').getElements('input');
	
	if(status) slideshow_active = status;
	else if(slideshow_active != 1) slideshow_active = 1;
	else slideshow_active = 0;
	
	buttons.each(function(input,i){ 
    if(i > 0) {
      if(input.value == 'stop slideshow' || input.value == 'start slideshow'){
		if(slideshow_active == 1) input.value = 'stop slideshow';
		else input.value = 'start slideshow';
	  }
    }});
}

/* slideshow settings */
var fade_show;
var fade_container = $('slideshow-container');
var fade_images = fade_container.getElements('div');
var fade_currentIndex = 0;
var fade_interval;
var loop = fade_images.length;
  
function slideshow_init(){
	/* opacity and fade */
  fade_images.each(function(img,i){ 
    if(i > 0) {
     img.set('opacity',0);
    }
  });
  
  /* start once the page is finished loading */
  window.addEvent('load',function(){
    fade_interval = slideshow_work.periodical(5300);
  });
}

function slideshow_work(force){
    // show next image if slideshow is active, or change is forced
    if(slideshow_active == 1 || force == 'true'){
		fade_images[fade_currentIndex].fade('out');
		fade_images[fade_currentIndex = fade_currentIndex < fade_images.length - 1 ? fade_currentIndex+1 : 0].fade('in');
		
		// preload another image
		xhr('GET', './include/randomGallerie.php', 'id='+loop);
		
		if(++loop > fade_images.length) loop=1;
	}
}

function slideshow_next(){
	// stop slideshow
	slideshow_pause('0');
	// show next image
	slideshow_work('true');
}

window.addEvent('domready', slideshow_init());

