jQuery.fn.photogallery = function() {
	pg = this;
	photos = $(this).find("div.photos");
	
	pg.wrap("<div class='active'></div>")
	
	photos.find("img:eq(0)").addClass("selected").end().find("img:gt(0)").hide();

	$("<p class='nav'>Images&nbsp;&nbsp;<a href='#' class='previous'><</a> <span class='current'>1</span>/<span class='total'></span> <a href='#' class='next'>></a></p>").appendTo(this);
	
	pg.find("span.total").html(photos.find("img").length);
	
	pg.find("a.previous").bind("click", function() {
		currentImage = photos.find("img").index($("img.selected")[0]);
		
		photos.find("img").eq(currentImage).removeClass("selected");
		
		if (currentImage==0) {
			photos.find("img:first").fadeOut("fast");
			photos.find("img:last").addClass("selected").fadeIn("fast");
			pg.find("span.current").html(photos.find("img").length);
		}
		else {
			photos.find("img").eq(currentImage).fadeOut("fast");
			photos.find("img").eq(currentImage-1).addClass("selected").fadeIn("fast");
			pg.find("span.current").html(currentImage);
		}

		return false;
	});
	pg.find("a.next").bind("click", function() {
		currentImage = photos.find("img").index($("img.selected")[0]);
		
		photos.find("img").eq(currentImage).removeClass("selected");
		
		
		if (currentImage==photos.find("img").length-1) {
			photos.find("img:last").fadeOut("fast");
			photos.find("img:first").addClass("selected").fadeIn("fast");
			pg.find("span.current").html("1");
		}
		else {
			photos.find("img").eq(currentImage).fadeOut("fast");
			photos.find("img").eq(currentImage+1).addClass("selected").fadeIn("fast");
			pg.find("span.current").html(currentImage+2);
		}

		return false;
	});

	
}