/** 
 * Frent Gallery
 * Create by Frederik Van Everbroeck
 */
 
(function($){
	$.fn.frentGallery = function(){
		
		return this.each(function(){
			
			// Hide all images
			$(this).find("li").each(function(){
				$(this).parent().hide();	
			});
			
			// Enable first image
			$first = $(this).find("li").first();
			$("#galleryFooter").before("<img id='current'>");
			loadImage($first.html());
			
			// Create Navigation
			var navigation =  $("<ul id='galleryNav'>").appendTo($(this));
			var length = $(this).find("li").length;
			for(var i=length;i>0;i--){
				$a = $("<a href='#" + (i-1) + "'>" + (i) + "</a>");
				// Create click event
				$a.click(function(){
					navigation.find('.selected').removeClass('selected');
					$(this).addClass('selected');
					
					var url = $("#gallery").find("li").eq($(this).html()-1).html();	
					if(url != $('#current').attr("src"))
						loadImage(url);
				});
				if(i==1) $a.addClass('selected');
				$("<li>").appendTo(navigation).append($a);		
			}
			
		});
		
		function loadImage(url){
			$("#current").parent().addClass("loading");
					
			$("#current").fadeOut("normal", function(){
							$(this)
								.load(function(){
									//$(this).parent().removeClass("loading");
									$(this).fadeIn();
								})
								.attr("src", url);
			});		
		}
	};
})(jQuery);
