$(document).ready(function() {
	initGallery();
});

function initGallery(){
	for(i=1;i<=20;i++){
		if($('.gallery_0'+i).length != 0){
			$('.gallery_0'+i).each(function(){
				$(this).replaceWith('<a href="#" class="gallery_link_0' + i + '">' + $(this).html() + "</a>");
			});
			$('body').append($('<div>').attr({'id':'galleryloader_0'+i}));
			loadGallery(i);
		}
	}
}

function loadGallery(number){
	$.ajax({
		url: '/index.php/gallerij?id='+number,
		success: function(data) {
			var content = data.split('##');
			var id = content[0];
			var html = content[1];
			
			if($('#galleryloader_0'+id).length != 0){
				$('#galleryloader_0'+id).html(html);
				var firstlink = $('#galleryloader_0'+id+' .first').attr('href');
				$('#galleryloader_0'+id+' .first').remove();
				$('.gallery_link_0'+id).each(function(){
					$(this).attr({'href':firstlink, 'rel':'gallery_0'+id});
				});
				$('#galleryloader_0'+id+' a').each(function(){
					$(this).attr({'rel':'gallery_0'+id});
				});
				$('a[rel=gallery_0'+id+']').lightBox();
			}
		}
	});
}
