var picture_galleries = new Array();
var interval = null;
var fading = false;

function startSlideshow( gallery_id ) {
	if (interval) {
		window.clearInterval( interval );
		interval = null;
		$('gallery_'+gallery_id+'_btn_start').innerHTML = 'Slideshow starten';
	} else {
		interval = window.setInterval('galleryNext(\''+gallery_id+'\', true)', 5000);
		window.setTimeout("galleryNext( '"+gallery_id+"', true);", 2000);

		$('gallery_'+gallery_id+'_btn_start').innerHTML = 'Slideshow stoppen';
	}
}

function galleryNext( gallery_id, automatic ) {

	if (fading) return;
	
	gallery_id = parseInt(gallery_id);
	
	if (interval && !automatic) {
		window.clearInterval( interval );
		interval = null;
		$('gallery_'+gallery_id+'_btn_start').innerHTML = 'Slideshow starten';
	}
	
	var all_pics = $('gallery_'+gallery_id+'_pic_container').childElements();

	picture_galleries[gallery_id].current_pic++;
	if (automatic) {
		if ( (picture_galleries[gallery_id].current_pic) > picture_galleries[gallery_id].total_pics ) {
			picture_galleries[gallery_id].current_pic = 1;
			$('gallery_'+gallery_id+'_btn_prev').hide();
			$('gallery_'+gallery_id+'_btn_next').show();
			var pic_current_ref = all_pics[picture_galleries[gallery_id].total_pics-1];
			var pic_to_load_ref = all_pics[picture_galleries[gallery_id].current_pic-1];
		} else {
			var pic_current_ref = all_pics[picture_galleries[gallery_id].current_pic-2];
			var pic_to_load_ref = all_pics[picture_galleries[gallery_id].current_pic-1];
		}
	} else {
		if ( (picture_galleries[gallery_id].current_pic) > picture_galleries[gallery_id].total_pics ) {
			picture_galleries[gallery_id].current_pic--;
			return;
		}
		var pic_current_ref = all_pics[picture_galleries[gallery_id].current_pic-2];
		var pic_to_load_ref = all_pics[picture_galleries[gallery_id].current_pic-1];
	}

	// Check if next picture was already loaded, else wait until it is
	if (!pic_to_load_ref.down('img').loaded) {
		$('gallery_'+gallery_id+'_indicator').show();
		window.setTimeout('galleryNext(\''+gallery_id+'\')',100);
		return;
	}

	$('gallery_'+gallery_id+'_indicator').hide();
	
	pic_to_load_ref.setOpacity(0);
	pic_to_load_ref.setStyle({visibility:'visible'});
	
	$('gallery_'+gallery_id+'_pagecounter').innerHTML = picture_galleries[gallery_id].current_pic;
	
	if (picture_galleries[gallery_id].current_pic!=1) {
		$('gallery_'+gallery_id+'_btn_prev').show();
	}
	
	if (picture_galleries[gallery_id].current_pic==picture_galleries[gallery_id].total_pics) {
		$('gallery_'+gallery_id+'_btn_next').hide();
	}
	
	crossFade( pic_current_ref, pic_to_load_ref, .5 );

}

function galleryPrevious( gallery_id ) {
	
	if (fading) return;
	
	if (interval) {
		window.clearInterval( interval );
		interval = null;
	}

	var all_pics = $('gallery_'+gallery_id+'_pic_container').childElements();
	
	if ( (picture_galleries[gallery_id].current_pic-1) < 1 )
		return;
		
	picture_galleries[gallery_id].current_pic--;
	$('gallery_'+gallery_id+'_pagecounter').innerHTML = picture_galleries[gallery_id].current_pic;
	
	$('gallery_'+gallery_id+'_btn_next').show();
	
	if (picture_galleries[gallery_id].current_pic==1) {
		$('gallery_'+gallery_id+'_btn_prev').hide();
	}
	
	var pic_current_ref = all_pics[picture_galleries[gallery_id].current_pic];
	var pic_to_load_ref = all_pics[picture_galleries[gallery_id].current_pic-1];

	crossFade( pic_current_ref, pic_to_load_ref, .5 );
	
}

function crossFade( current_element, next_element, duration ) {
	
	if (!duration) duration = 1;
	
	fading = true;
	
	Effect.Fade( current_element, { duration:duration, from:1.0, to:0.0 } );
	Effect.Appear( next_element, { duration:duration, from:0.0, to:1.0, afterFinish:function(){fading=false;} } );
	
}
