﻿gBaseUrl = "/images";
gLoadSpinnerUrl = gBaseUrl + '/loading5.gif';
gFailImage = gBaseUrl + '/image_load_error.gif'

//gImageLoaderVersion = '0.3';

function LoadThisVideoImage(loader, pCallback) {
	image_src = loader.attr('src');
	//alert(loader.attr('id'));
	img = $j(new Image());
	img.addClass("pic");	
	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');
		$j(this).show();
		if (onload_cb) {
			onload_cb($j(this));
		}
		if (pCallback) {
			cb = pCallback;
			cb($j(this));
		}
	})
   .error(function() { $j(this).attr('src', gFailImage).show(); })
   .attr('src', image_src)
   .show();
	//alert(img.attr('src') + ' loading/loaded');
}

function LoadThisImage(loader, pCallback) {
	image_src = loader.attr('src');
	
	img = $j(new Image());
	img.addClass("pic");	
	
	img.attr('id', "img_" +loader.attr('id'));
	
	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');
		$j(this).show();
		if (onload_cb) {
			onload_cb($j(this));
		}
		if (pCallback) {
			cb = pCallback;
			cb($j(this));
		}
	})
   .error(function() { $j(this).attr('src', gFailImage).show(); })
   .attr('src', image_src)
   .show();
	//alert(img.attr('src') + ' loading/loaded');
}

function LoadThisNewsImage(loader, pCallback) {
	image_src = loader.attr('src');
	//alert(loader.attr('id'));
	img = $j(new Image());
	img.addClass("pic");
	img.attr('id', 'newsHeadlineImage');
	img.hide();

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

		loader.html(this);
		loader.removeClass('loadable-news-image');
		loader.removeAttr('src');
		loader.removeAttr('onload');
		$j(this).show();
		if (onload_cb) {
			onload_cb($j(this));
		}
		if (pCallback) {
			cb = pCallback;
			cb($j(this));
		}
	})
   .error(function() { $j(this).attr('src', gFailImage).show(); })
   .attr('src', image_src)
   .show();
	//alert(img.attr('src') + ' loading/loaded');
}

function LoadImage(pSelector, pCallback) {
	var loader = $j(pSelector);
	loader.html('<img src="' + gLoadSpinnerUrl + '"/>');

	LoadThisImage(loader, pCallback);
}

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

		//Load images asynchronously
		setTimeout(function() { LoadThisImage(loader); }, 10);
		//LoadThisImage(loader);
	});

	$j('.loadable-video-image').each(function() {
		var loader = $j(this);
		loader.html('<img style="padding-left:130px;" src="' + gLoadSpinnerUrl + '"/>');

		//Load images asynchronously
		setTimeout(function() { LoadThisVideoImage(loader); }, 10);
		//LoadThisImage(loader);
	});

	$j('.loadable-news-image').each(function() {
		var loader = $j(this);
		loader.html('<img style="padding-left:130px;" src="' + gLoadSpinnerUrl + '"/>');

		//Load images asynchronously
		setTimeout(function() { LoadThisNewsImage(loader); }, 10);
		//LoadThisImage(loader);
	});		
}
