(function(jQuery) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  jQuery.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

jQuery.preLoadImages(window.location.protocol + "//" + window.location.host+'/modules/image-mosaic/images/wait.gif');
imageLoaderDefaultConfig={
	LoadingImage:'img/image-loader.gif',
	FailImage:'img/image-loader-error.jpg'
};

jQuery.fn.loadImage=function(pConfig,pCallback){
	if(pConfig){
		var config=jQuery.extend(imageLoaderDefaultConfig,pConfig)
	}else{
		var config=imageLoaderDefaultConfig
	}
		var loader=jQuery(this);
		loader.fadeIn().html('<img class="product_image" src="'+config.LoadingImage+'"/>');
		
		var img_src=loader.attr('src');
		loader.removeAttr('src');
		img=jQuery(new Image());
		img.fadeOut();
		
		img.load(function(){
			
			loader.html(this);
			jQuery(this).fadeIn();
			
			if(pCallback){
				pCallback(jQuery(this));
			}
				
			}).attr('src',img_src).error(function(){
						jQuery(this).attr('src',config.FailImage)}).fadeIn()
			};
			
			jQuery.fn.loadImages=function(pConfig,pCallback){
				this.each(function(){
					jQuery(this).loadImage(pConfig,pCallback);
				}
		)};
			
			
jQuery(document).ready(function(){
								
	jQuery('.imgloader').loadImages({
		LoadingImage:window.location.protocol + "//" + window.location.host+'/modules/image-mosaic/images/wait.gif',
		FailImage:window.location.protocol + "//" + window.location.host+'/modules/shoppingcart/images/no-image-uploaded.gif'},function(){
		
		var img=jQuery(arguments[0]);
		var imgWidth = img.clientWidth;
		var imgHeight = img.clientHeight;
		
		//img.attr('width',thumbWidth);
		//img.attr('height',thumbHeight);
	
	});
});
