jQuery(document).ready(function(){
	// jQuery("div.entry .gallery-loading").hide();
	
	jQuery(".ngg-gallery-thumbnail").each(function(i){
		var fimg = jQuery(this).children().attr("href");
		var img = jQuery(this).children().html();
		var imgid = jQuery(this).children().attr("id");
		jQuery(this).html(img);
		jQuery(this).children().attr('rel', fimg);
		jQuery(this).children().attr('id', "afrimod-"+imgid);
		jQuery(this).children().css('cursor', 'pointer');
	});
	
	jQuery(function($){
		if(jQuery(".ngg-galleryoverview").length > 0){
			jQuery(".ngg-galleryoverview").wrap('<div id="gallery-container"></div>');
			jQuery(".ngg-clear").remove();
			var gl=jQuery(".gallery-loading").html();
			jQuery(".gallery-loading").remove();
			jQuery("#gallery-container").prepend('<div class="gallery-loading" align="center">' + gl + '</div>');
			jQuery(".ngg-galleryoverview").before('<div class="gallery-content"></div>');
			
			var first 			= jQuery(".ngg-galleryoverview:first .ngg-gallery-thumbnail-box:first img");
			var first_img		= jQuery(first).attr('rel');
			var first_imgid		= jQuery(first).attr('id').replace("afrimod-thumb", "");
			var first_imgidtag	= 'main-gallery-image' + first_imgid;

			// jQuery("div.entry .gallery-content").hide();
			// jQuery("div.entry .gallery-loading").show();
			jQuery.getJSON(document.location + "?ajax=true&id=" + first_imgid,
				function(data){
					jQuery("div.entry .gallery-content").html('<img id="' + first_imgidtag + '" src="' + first_img + '" />');
					jQuery('#' + first_imgidtag).load(function(){
						// jQuery(this).hide();
						jQuery(this).show();
						// jQuery("div.entry .gallery-content").fadeIn();
						// jQuery("div.entry .gallery-loading").hide()
					}).attr('src', first_img);
				});
		}
	});
	
	jQuery(".ngg-gallery-thumbnail img").click(function(i){
			var fimg	= jQuery(this).attr('rel');
			var imgid	= jQuery(this).attr('id').replace("afrimod-thumb", "");
			var imgidtag= 'main-gallery-image' + imgid;
			
			// jQuery("div.entry .gallery-content").hide();
			// jQuery("div.entry .gallery-loading").show();
			jQuery.getJSON(document.location + "?ajax=true&id=" + imgid,
				function(data){
					jQuery("div.entry .gallery-content").html('<img id="' + imgidtag + '" src="'+fimg+'" />');
					jQuery('#' + imgidtag).load(function(){
						// jQuery(this).hide();
						jQuery(this).fadeIn();
						// jQuery("div.entry .gallery-content").fadeIn();
						// jQuery("div.entry .gallery-loading").hide()
					}).attr('src', fimg);
				});
		});
});