(function($) {
	$.fn.slideshow = function(options) {
		var defaults = {
			frontend_image_dir : '/',
			display_overlay : 'slideshow_overlay',
			display_selector : 'slideshow_selector',
			display_image : 'slideshow_wrapper',
			slideshow_mode : 'slideshow',
			interval : 3000,
			speed : 1500,
			fixed_width : false,
			fixed_height : false
		};
		var options = $.extend(defaults, options);
		var fixed_width = (options.fixed_width !== false) ? ' width="'+options.fixed_width+'"' : '';
		var fixed_height = (options.fixed_height !== false) ? ' height="'+options.fixed_height+'"' : '';
		
		var slides = [];
		var current = 0;
		var amount = $(this).children('li').length-1;
		var sources = '';
		$(this).children('li').each(function(i) {
			slides[i] = [];
			slides[i]['image'] = $(this).find('div.slide_image').children('img').attr('src');
			slides[i]['title'] = $(this).find('div.slide_title').html();
			slides[i]['link'] = $(this).find('div.slide_link').html();
			slides[i]['text'] = $(this).find('div.slide_text').html();
			sources = slides[i]['image']+',';
		});
		sources = sources.substr(0,sources.length-1).split(',');
		
		var loaded = 1;
		$.preload(sources, {
			onRequest : function(data) {
				
			},
			onComplete : function(data) {
				
			},
			onFinish : function(data) {
				activateSlideshow();
			}
		});
		
		function activateSlideshow() {
			if (current in slides) {
				if (options.slideshow_mode == 'slideshow') {
					var buttons = '';
					for (i = 0; i <= amount; i++) {
						buttons = buttons+'<div id="slide_selector_'+(amount-i)+'" class="slideshow_selector_button"><p class="slideshow_selector_value">'+(amount-i+1)+'</p></div>';
					}
					$('div#'+options.display_selector).html(buttons);
					$('div#slide_selector_0').animate({ opacity : 0.9 }, options.speed).addClass('selector_active');
					$('div.slideshow_selector_button').click(function() {
						var slide_id = parseInt($(this).attr('id').replace('slide_selector_',''));
						openSlide(slide_id)
					});
					$('div#'+options.display_overlay).html('<h1 class="slide_title">'+slides[current]['title']+'</h1><p class="slide_text">'+slides[current]['text']+'</h1><a href="'+slides[current]['link']+'"><img src="'+options.frontend_image_dir+'bt-meld-je-aan.png" title="Meld je aan" class="slide_link" /></a>').fadeIn(options.speed/2);
					$('img.slide_link').hover(function() {
						$(this).attr('src', $(this).attr('src').replace(/\.(.+)$/i, '-active.$1'));
					}, function() {
						$(this).attr('src', $(this).attr('src').replace(/-active\.(.+)$/i, '.$1'));
					});
				}
				$('div#'+options.display_image).append('<img src="'+slides[current]['image']+'"'+fixed_width+fixed_height+' class="slide_image slide_active" />');
				if (options.slideshow_mode == 'static') {
					$('img.slide_active').css('display','block');
				} else {
					$('img.slide_active').fadeIn(options.speed, function() {
						$.doTimeout('slideshow', options.interval, function() {
							if (amount > 0) return nextSlide(options.speed);
						});
					});
				}
			}
		}
		
		function nextSlide(fade_speed) {
			current = current+1;
			if (current > amount) current = 0;
			if (current in slides) {
				if (options.slideshow_mode == 'slideshow') {
					$('div.selector_active').animate({
						opacity : 0.5
					}, (fade_speed/2), function() {
						$('div#slide_selector_'+current).animate({
							opacity : 0.9
						}, (fade_speed/2)).addClass('selector_active');
					}).removeClass('selector_active');
					$('div#'+options.display_overlay).fadeOut(fade_speed/2, function() {
						$(this).html('<h1 class="slide_title">'+slides[current]['title']+'</h1><p class="slide_text">'+slides[current]['text']+'</h1><a href="'+slides[current]['link']+'"><img src="'+options.frontend_image_dir+'bt-meld-je-aan.png" title="Meld je aan" class="slide_link" /></a>').fadeIn(fade_speed/2);
						$('img.slide_link').hover(function() {
							$(this).attr('src', $(this).attr('src').replace(/\.(.+)$/i, '-active.$1'));
						}, function() {
							$(this).attr('src', $(this).attr('src').replace(/-active\.(.+)$/i, '.$1'));
						});
					});
				}
				$('div#'+options.display_image).append('<img src="'+slides[current]['image']+'"'+fixed_width+fixed_height+' class="slide_image slide_next" />');
				$('img.slide_next').fadeIn(fade_speed);
				$('img.slide_active').fadeOut(fade_speed, function() {
					$('img.slide_active').remove();
					$('img.slide_next').removeClass('slide_next').addClass('slide_active');
					$.doTimeout('slideshow', options.interval, function() {
						return nextSlide(options.speed);
					});
				});
			}
		}
		
		function openSlide(slide_id) {
			$.doTimeout('slideshow');
			current = slide_id-1;
			nextSlide(200);
		}
	};
})(jQuery);
