jQuery.fn.gallery = function()
{
	jQuery(this).each(function(e)
	{
		var photolistWindow = jQuery(this).find(".photolist");
		var previewWindow = jQuery(this).find(".preview");
		jQuery(this).find(".photolist a.photo").each(function()
		{
			jQuery(this).click(function()
			{
				var eq = jQuery(this).parent().find("a.photo").index(jQuery(this));
				previewWindow.attr("title", eq);
								
				var img = "<img src='" + jQuery(this).attr("href") + "' alt='' />";
				previewWindow.html(img);
				previewWindow.find("img").css("display", "none").fadeIn();
				return false;
			});
		});
		
		previewWindow.click(function()
		{
			var eq = parseInt(jQuery(this).attr("title"));
			if (eq === NaN) console.log("zzz");
			if (eq >= 0) eq = eq + 1; else eq = 0;
			if (photolistWindow.find("a.photo").length <= eq) { eq = 0; }
			
			
			previewWindow.attr("title", eq);
			
			var href = photolistWindow.find("a.photo:eq(" + eq + ")").attr("href");
			var img = "<img src='" + href + "' alt='' />";
			jQuery(this).html(img);
			previewWindow.find("img").css("display", "none").fadeIn();
		});
	});
}
