function lightbox() {
	var lbCounter = new Array();
	var links = $('a[target^=popup]');
	var overlay = $(jQuery('<div id="overlay" style="display: none"></div>'));
	var container = $(jQuery('<div id="lightbox" style="display: none"></div>'));
	var target = $(jQuery('<div class="target"></div>'));
	var head = $(jQuery("<h2></h2>"));
	var close = $(jQuery('<div class="close"><a href="#close"><img src="/gfx/close.gif" alt=""></a></div>'));
/* */
/* plaatjes aanpassen in de gfx map. En desnoods nog even de left/right aanpassen in de css */

	var prev = $(jQuery('<a href="#prev" class="prev"><img src="/gfx/left.gif" alt="vorige"></a>'));
	var next = $(jQuery('<a href="#next" class="next"><img src="/gfx/right.gif" alt="volgende"></a>'));
	var activeRel = "";
	var lbImages = new Array();
			
	$('body').append(overlay).append(container);
	
	overlay.css('width', $(window).width());
	overlay.css('height', $(window).height());
	
	container.append(head);
	container.append(close);
	container.append(target);
	
	container.append(prev);
	container.append(next);
	
	container.show().css({
				'top': Math.round(($(window).height() - container.outerHeight()) / 2) + 'px',
				'left': Math.round(($(window).width() - container.outerWidth()) / 2) + 'px',
				'margin-top': 0,
				'margin-left': 0
				}).hide();
	
	close.click(function(c) {
		c.preventDefault();
		overlay.add(container).fadeOut('normal');
	});

	prev.add(next).click(function(c) {
		c.preventDefault();
		isPrev = $(this).is('.prev') ? true : false;
		
		var current = parseInt(links.filter('.selected').attr('lb-' + activeRel),10);
		if(isPrev) {
			current = current - 1;
			if(current < 0) {
				current = lbCounter[activeRel];
			}
		} else {
			current = current + 1;
			if(current > lbCounter[activeRel]) {
				current = 0;
			}
		}
		var to = lbImages[activeRel][current];
		if(to.size()) {
			to.click();
		}
	});
	
	links.each(function(index) {
		var link = $(this);
		link.click(function(c) {
			c.preventDefault();
			activeRel = link.attr('rel');
			open(link.attr('href'), link.attr('title'));
			links.filter('.selected').removeClass('selected');
			link.addClass('selected');
		});
		
		r = $(this).attr("rel");
		if(typeof(lbCounter[r]) == "undefined") {
			lbCounter[r] = 0;
			lbImages[r] = new Array();
		} else {
			lbCounter[r] = lbCounter[r] + 1;
		}
		rname = "lb-" + r;
		lbImages[r][lbCounter[r]] = link;
		link.attr(rname, lbCounter[r]);
	});

	var open = function(url, t) {
		if(container.is(':visible')) {
			prev.hide();
			next.hide();
			target.children().fadeOut('normal', function() {
				target.children().remove();
				loadimage(url, t);
			});
		} else {
			target.children().remove();
			prev.hide();
			next.hide();
			overlay.css("opacity", "0.8");
			overlay.add(container).fadeIn('normal', function(){
				loadimage(url, t);
			});
		}
	}
	
	var loadimage = function(url, t) {
		if(container.is('.loading')) { return; }
		container.addClass('loading');
		var img = new Image();
		img.onload = function() {
			img.style.display = 'none';
			
			var maxWidth = ($(window).width() - parseInt(container.css('padding-left'),10) - parseInt(container.css('padding-right'), 10)) - 100;
			var maxHeight = ($(window).height() - parseInt(container.css('padding-top'),10) - parseInt(container.css('padding-bottom'), 10)) - 100;
			
			if(img.width > maxWidth || img.height > maxHeight) { // One of these is larger than the window
				var ratio = img.width / img.height;
				if(img.height >= maxHeight) {
					img.height = maxHeight;
					img.width = maxHeight * ratio;
				} else {
					img.width = maxWidth;
					img.height = maxWidth * ratio;
				}
			}
			head.text(t);
			_top = Math.round(($(window).height() - img.height - parseInt(container.css('padding-top'),10) - parseInt(container.css('padding-bottom'),10)) / 2) + $(document).scrollTop();
			container.animate({'width': img.width,'height': img.height+21, 'top': _top + 'px', 'left': Math.round(($(window).width() - img.width - parseInt(container.css('padding-left'),10) - parseInt(container.css('padding-right'),10)) / 2) + 'px'},'normal', function(){
				target.append(img);
				$(img).fadeIn('normal', function() {
					prev.show();
					next.show();
					container.removeClass('loading');
				});
			})
		}
		img.src = url;
	}
	
	$(document).keyup( function( e ) { 
		if( e.which == 27) {  // escape, close box 
			close.click();
		} 
	});

}