var volgendPlaatje = true;
$(document).ready(function(){
	
	if ($(".nivo-controlNav a:first").length > 0) {
		var firstImage = $(".nivo-controlNav a:first").addClass('active').attr("href");
		
		//$('.nivo-controlNav a:first').addClass('active');
		var captionid = $('.nivo-controlNav a:first').text();
		loadNewImage(firstImage, captionid, $(".nivo-controlNav a:first"));
		
		//setInterval( "slideSwitch()",5000 );
	}
	else {
		$('#slider').removeClass('loading').append("Er zijn geen afbeeldingen gevonden.");
		$('.nivo-controlNav').css('display','none');
	}
	
	$(".nivo-controlNav a").click(function(event) {
		event.preventDefault();
		clearTimeout(timer);
		var captionid = $(this).text();
		
		if (volgendPlaatje == true) {
			volgendPlaatje = false;
			$(".nivo-controlNav a").removeClass('active');
			$(this).addClass('active');
			var image = $(this).attr("href");
			if ($('#slider img').length > 0) {
					loadNewImage(image, captionid, this); 
			}
			else {	 loadNewImage(image, captionid, this);	}
		}
	});
	
	
	$(".nextslide").click(function(event) {
		event.preventDefault();
		clearTimeout(timer);	
		var captionid = $(".nivo-controlNav .active").next();
		if (captionid.length == 0) {captionid = $(".nivo-controlNav a").first(); }
		
		if (volgendPlaatje == true) {
			volgendPlaatje = false;
			$(".nivo-controlNav a").removeClass('active');
			$(captionid).addClass('active');
			var image = $(captionid).attr("href");
			if ($('#slider img').length > 0) {
					loadNewImage(image, captionid.text(), captionid); 
			}
			else {	 loadNewImage(image, captionid.text(), captionid);	}
		}
	});
	
	$(".prevslide").click(function(event) {
		event.preventDefault();
		clearTimeout(timer);	
		var captionid = $(".nivo-controlNav .active").prev();
		if (captionid.length == 0) {captionid = $(".nivo-controlNav a").last(); }
		
		if (volgendPlaatje == true) {
			volgendPlaatje = false;
			$(".nivo-controlNav a").removeClass('active');
			$(captionid).addClass('active');
			var image = $(captionid).attr("href");
			if ($('#slider img').length > 0) {
					loadNewImage(image, captionid.text(), captionid); 
			}
			else {	 loadNewImage(image, captionid.text(), captionid);	}
		}
	});
	
	
	
});

var timer;
function slideSwitch() {
    //console.log('SLIDESHOWSWITCH!');
	if (volgendPlaatje == true) {
		volgendPlaatje = false;
		var nextActive = $(".nivo-controlNav .active").next();
		
		if (nextActive.length == 0) nextActive = $(".nivo-controlNav a:first"); 
		
		var captionid = $(nextActive).text();
		var image = $(nextActive).attr("href");
		
		if ($('#slider img').length > 0) {
			loadNewImage(image, captionid, nextActive); 
		}
		else {	 loadNewImage(image, captionid, nextActive);	}
	
	}
}

function loadNewImage(image, captionid, nextActive) {

	var img = new Image();
	$('#slider').addClass('loading');

	$(img).load(function () {	 
		$('#slider').removeClass('loading').append(this);
		$(this).hide();
		
		if ($('#slider img').length > 1) {
			$('#slider img:first').fadeOut(1000, function(){
				$(this).remove();	
			});
		}
		$(".nivo-controlNav a").removeClass('active');
		$(nextActive).addClass('active');
		
		$('.nivo-html-caption').fadeOut(1000);
		$('.nivo-caption').fadeOut(1000);
		if (captionid > 0) {
			$('#caption'+captionid).fadeIn(1000);	
		}
		$(this).fadeIn(1000, function(){
			//if (firstTime == true) {
				//console.log('setTimeOut');
				timer = setTimeout( "slideSwitch()",5000 );	
			//}
			return volgendPlaatje = true;
		});
		
	})
	.error(function () {
		$('#slider').removeClass('loading').append("de foto kon niet geladen worden.");
		return volgendPlaatje = true;
	})
	.attr('src', image);
	
	
	
	
	
}
