(function($){

    $.fn.LoadImage = function(settings){
                
                settings = $.extend({
                    height:0,
                    width:0,
                    loading:"images/ajax-loader.gif"
                    },settings);
                    
                var images = this;
                $(images).hide();
               var img = new Image();
                var preLoad = function($this){
                   
                    img.src = $this.src;
                    if (img.complete) { 
                        processImg.call($this);
                        return;
                    }
                    img.onload = function(){
                        processImg.call($this);
                        img.onload=function(){};
                    } 
                }
                function processImg(){
                        
                        if (img.width > 0 && img.height > 0) {
					            if (img.width / img.height >= settings.width / settings.height) {
					            $(this).attr("width",settings.width);
					            $(this).attr("height",(img.height * settings.width) / img.width);
					            
	                           var margin = (settings.height - (img.height * settings.width) / img.width) / 2;
	                           $(this).css("margin-top", margin)
	                           
                            } else {
                                $(this).attr("height",settings.height);
					            $(this).attr("width",((img.width * settings.height) / img.height));
					            
					            var margin = (settings.width - ((img.width * settings.height) / img.height)) / 2;
					             $(this).css("margin-left", margin)
                            }
				        }

                        
                        $(this).show();
                }
                return $(images).each(function(){
                    preLoad(this);
                });                
        }
})(jQuery);

