//src: http://blog.realmofzod.com/2009/04/09/asynchronous-image-loading-with-jquery/

SpinnerUrl = BASE_URL + '/images/loader.gif';
FailImage = BASE_URL + '/images/thumb-not-found.png';


function LoadThisImage(loader, pCallback){
	image_src = loader.attr('src');
	
	img = new Image();
	$(img).hide();

	$(img).load(function() {
		cb_js = loader.get(0).getAttribute('onload');
		onload_cb = function(){
			eval(cb_js);
		}

		loader.html(this);
		loader.removeClass('loadable-image');
		loader.removeAttr('src');
		loader.removeAttr('onload');

		$(this).show();
		if (onload_cb){
			onload_cb($(this));
		}
		if (pCallback){
			cb = pCallback;
			cb($(this));
		}
	})

	.error(function() {
		/*
		 * if .gif not found check .jpg
		 */
		var url = $(this).attr("src");
		if (url.indexOf('.gif') >= 0)
		{
			$(this).attr("src", url.replace(/\.gif$/i, ".jpg"));
			return;
		}
		$(this).attr('src', FailImage).show();
	})
	.attr('src', image_src)
	.show();
}

function LoadAllImages(){
	$('.loadable-image').each(function(){
		var loader = $(this);
		loader.html('<img src="' + SpinnerUrl + '"/>');
		LoadThisImage(loader);
	});
}

$(document).ready(function(){
	LoadAllImages();
});
