window.addEvent('domready', function() {
	if($('gallery')!=null) {
		make_fading_slideshow($('gallery'), 'div');
	}
	if($('gallery_2')!=null) {
		make_fading_slideshow($('gallery_2'), 'div');
	}
	if($('gallery_3')!=null) {
		make_fading_slideshow($('gallery_3'), 'div');
	}
	if($('large_map')!=null) { init_map(); }
	
});

///////////////////////////////////////////////////////////////////////
///////////// FADING SLIDESHOW /////////////////////////////////
/////////////////////////////////////////////////////////////////////
var news_change_interval = 0;
var fade_speed = 10000;
var news_ticker_fade_speed = 500;
var fade_delay;
var auto_play;
function make_fading_slideshow(slideshow_holder, element_tag) {
	var slideshow_holder_id = slideshow_holder.id;
	var slides = $$('#'+slideshow_holder_id+' '+element_tag);
	var num_slides = slides.length;

	slideshow_holder.his_slides = slides; 
	slideshow_holder.num_slides = num_slides; 
	slideshow_holder.cur_index = 0;
	
	slides.each(function(slide, index) {
		slide.store('his_index', index);
		slide.store('his_z_index', num_slides);
		slide.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			'z-index': num_slides
		});
		num_slides--;
	});

	// CONTROL BUSINESS  / FADE
	slideshow_holder.store('fading', false);
	
	var slideshow_controls = $$('#'+slideshow_holder_id+' a.slider_control');
	if(slideshow_controls.length>0) {
		slideshow_holder.store('auto_play', false);
		slideshow_controls.each(function(slide_control, index) {
			if(slide_control.hasClass('prev_slide')) {
				slide_control.addEvent('click', function() {
					var show_is_fading = slideshow_holder.retrieve('fading');
					if(show_is_fading==false) {
						fade_slide_V2(slideshow_holder, 'prev');
					}
					return false;
				});			
			} else if(slide_control.hasClass('next_slide')) {
				slide_control.addEvent('click', function() {
					var show_is_fading = slideshow_holder.retrieve('fading');
					if(show_is_fading==false) {
						fade_slide_V2(slideshow_holder, 'next');
					}
					return false;
				});
			}
		});
	} else {
		slideshow_holder.store('auto_play', true);
		// alert('slideshow_holder = '+slideshow_holder.id);
		fade_delay = fade_slide_V2.delay(news_change_interval, slideshow_holder);
	}	
}

function fade_slide_V2(active_slideshow, slideshow_direction) {
	if(active_slideshow==undefined) {
		active_slideshow = this;
	}
	active_slideshow.store('fading', true);
	if(slideshow_direction==undefined) {
		slideshow_direction = 'next';
	}
	if(active_slideshow.id=='news_ticker') {
		active_slideshow.store('this_fade_speed', news_ticker_fade_speed);
	} else {
		active_slideshow.store('this_fade_speed', fade_speed);
	}
	var NEXT_reset_top = false;
	var PREV_reset_top = false;
	var cur_index = active_slideshow.cur_index;
	var slides = active_slideshow.his_slides;
	var num_slides = active_slideshow.num_slides;
	var next_index;
	var index_to_fade;

	if(slideshow_direction=='next') {
		var top_opacity_start = 1;
		var top_opacity_end = 0;
		if((cur_index+1)<num_slides) {
			NEXT_reset_top = false;
			next_index = cur_index+1;
		} else {
			NEXT_reset_top = true;
			next_index = 0;
			top_opacity_end = 1;
		}	
		// RESET INBETWEENERS ALS INDEX: 0 IS
		if(next_index==1) { NEXT_reset_inbetweeners(slides); }
		index_to_fade = cur_index;
	} else if(slideshow_direction=='prev') {
		if(cur_index==0) {
			PREV_reset_inbetweeners(slides);
			var top_opacity_start = 1;
			var top_opacity_end = 0;
			next_index = num_slides - 1;
			index_to_fade = cur_index;
		} else {
			var top_opacity_start = 0;
			var top_opacity_end = 1;
			next_index = cur_index - 1;					
			index_to_fade = next_index;
		}
	
	}
	// if(active_slideshow.id=='page_slideshow') {	output('cur_index = '+cur_index+' | next_index = '+next_index); }

	var fade_top = new Fx.Tween(slides[index_to_fade], {property: 'opacity', duration: active_slideshow.retrieve('this_fade_speed')});
	fade_top.start(top_opacity_start, top_opacity_end).chain(
			function(){ 
				if(NEXT_reset_top==false) {
					if(slideshow_direction=='next') {
						active_slideshow.cur_index++;
					} else {
						if(cur_index==0) {
							active_slideshow.cur_index=num_slides - 1;
						} else {
							active_slideshow.cur_index--;
						}
					}
				} else {
					active_slideshow.cur_index = 0;
				}
				active_slideshow.store('fading', false);
				var this_auto_play = active_slideshow.retrieve('auto_play');
				if(this_auto_play==true) {
					clearTimeout(fade_delay);
					fade_delay = fade_slide_V2.delay(news_change_interval, active_slideshow);
				}
			}
	);
	if((NEXT_reset_top==true) && (slides[next_index].getStyle('opacity')==0)) {
		//output('NEXT_reset_top | next_index = '+next_index);
		var fade_bottom = new Fx.Tween(slides[next_index], {property: 'opacity', duration: active_slideshow.retrieve('this_fade_speed')});
		fade_bottom.start(0, 1);
	} 
}
function NEXT_reset_inbetweeners(slides) {
	// ZET TUSSENLIGGENDE SLIDES WEER OP 1
	slides.each(function(slide, slide_index) {
		if(slide_index>0) {
			slide.setStyle('opacity', 1);
		}
	});
}
function PREV_reset_inbetweeners(slides) {
	// ZET TUSSENLIGGENDE SLIDES WEER OP 1
	slides.each(function(slide, slide_index) {
		if((slide_index>0) && (slide_index<(slides.length-1))) {
			slide.setStyle('opacity', 0);
		} else if(slide_index==(slides.length-1)) {
			slide.setStyle('opacity', 1);
		}
	});
}
