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', "mlh-"+imgid);
		jQuery(this).children().css('cursor', 'pointer');
	});
	
	jQuery(function($){
		var first 			= jQuery(".ngg-galleryoverview:first .ngg-gallery-thumbnail-box:last img");
		var first_img		= jQuery(first).attr('rel');
		var first_imgid		= jQuery(first).attr('id').replace("mlh-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('<p><img id="' + first_imgidtag + '" src="' + first_img + '" /></p><p><strong>' + data.gallerytitle + '</strong><br />' + data.description + '</p>');
				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("mlh-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('<p><img id="' + imgidtag + '" src="'+fimg+'" /></p><p><strong>' + data.gallerytitle + '</strong><br />' + data.description + '</p>');
				jQuery('#' + imgidtag).load(function(){
					jQuery(this).hide();
					jQuery(this).show();
					jQuery("div.entry .gallery-content").fadeIn();
//					jQuery("div.entry .gallery-loading").hide();
				}).attr('src', fimg);
			});
	});
});

jQuery.preloadImages = function(){
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i > 0; i--) {
		jQuery("<img id='a'>").attr("src", a[i]);
		alert(jQuery("img#a").complete);
	}
}