jQuery.fn.gallery = function(callback){	
	var gallery = this;
	var href = [];
	var img = [];
	var speed = 1000;
	var slideShowTimeOut = 5000;
	var galleryStructure = '<div id="img-gallery2"><img style="display:none" /><ul></ul></div>';
	var started = false;
	
	// loops over the gallery object ("img-gallery img") and stores the details in an array, while deleting the originals
	$(gallery).each(function(i){
		$(this).hide();
		href[i] = $(this).attr('href');
		img[i] = [$("img",this).attr('src'),$("img",this).attr('alt'),$("img",this).attr('longdesc')];
		$(this).remove();
	});
	
	// initially sets up the gallery
	start();
	
	// correctly highlight the first image
	$("#img-gallery2 ul a:eq(0)").addClass("active");
	
	// called once, to delete the original gallery list and create the new list, with navigation etc.
	function start(){
		// attaches the gallery skeleton to the page
		$("#content").prepend(galleryStructure);
		
		// loops over each image in our premade array, and creates an li navigation button for it
		$(img).each(function(i){
			$("#img-gallery2 ul").append("<li><a href=\"#img"+(i+1)+"\">"+(i+1)+"</a></li>");
		});
		
		$("#img-gallery2 ul").append("<li id=\"img-gallery-enlargebutton\"><a id=\"largerlink\" title=\"\" href=\"\"></a></li>");
		
		// changes the image to the last in the list, so when the slideshow starts it jumps to the first image - kinda hacky, it works just fine
		firstImage = img.length;
		firstImage = firstImage - 1;
		changeImage(firstImage);
		
		startSlideShow();
		
		// handles the click for each navigation button
		$("#img-gallery2 ul a").click(function(){
			// gets the image id from the link location ("img0","img1"...)
			var imgToLoad = $(this).attr("href");
			
			// extracts the number from the link name
			imgToLoad = imgToLoad.split("#");
			imgToLoad = parseInt(imgToLoad[1].substr(3)) - 1;
			
			// takes that number and loads the correct image
			changeImage(imgToLoad);
			
			// stop the slideshow from changing the image again
			stopSlideShow();
			
			// don't follow the link anywhere
			return false;
		});
	}
	
	// called whenever a button is clicked
	function changeImage(n){
		// fades out the currently showing image
		$("#img-gallery2 img").fadeOut(speed / 2, function(){
			// load the correct link location (need to specify that <a> otherwise the navigation links would be changed too!)
			$("#img-gallery2 a#largerlink").attr("href",href[n]).attr("title",img[n][1]);
			
			// replace the details of the image with those of the newly requested image
			$("#img-gallery2 img").attr("src",img[n][0]).attr("alt",img[n][1]);
			
			// sets all the navigation buttons to the inactive class
			$("#img-gallery2 ul a").each(function(){ $(this).removeClass("active"); });
			$("#img-gallery2 ul a:eq("+n+")").addClass("active");
			
			// fades the image back in
			fadeInAll();
			
			// sets up the lightbox
			callback();
		});
	}
	
	// fades everything back in
	function fadeInAll(fromAnimate){
		var localSpeed = speed;
		if (!fromAnimate){ localSpeed = speed / 2; }
		$("#img-gallery2 a").fadeIn(localSpeed / 2);
		$("#img-gallery2 img").fadeIn(localSpeed / 2);
		$("#img-gallery2 ul").fadeIn(localSpeed / 2);
	}
	
	function startSlideShow(){
		// gets the next image to load
		var imgToLoad = $('#img-gallery2 ul a.active:eq(0)').attr('href');
		imgToLoad = imgToLoad.split('#');
		imgToLoad = parseInt(imgToLoad[1].substr(3));
		
		// cancels the slideshow if there is only one image
		if(gallery.length == 1){
			stopSlideShow();
		}
		
		// pauses the slideshow if the lightbox is in use
		if($("#img-gallery-enlargebutton").attr("class") == "activated"){
			window['galleryTimeout'] = setTimeout(function(){startSlideShow()},slideShowTimeOut);
		}else{
			
			// start again when we reach the end
			if(imgToLoad == gallery.length){
				imgToLoad = 0;
			}
			
			// set the timer running that will control the transition
			window['galleryTimeout'] = setTimeout(function(){startSlideShow()},slideShowTimeOut);
			
			// change the image
			changeImage(imgToLoad, function(){
				eval(galleryTimeout);
			});
		}
	}
	
	function stopSlideShow(){
		clearTimeout(eval(galleryTimeout));
	}
}