(function($){

    $.fn.LoadImage = function(settings){
                
                settings = $.extend({
                    height:0,
                    width:0,
                    loading:"images/ajax-loader.gif"
                    },settings);
                    
                var images = this;
                $(images).hide();
//                var loadding = new Image();
//                loadding.className="loadding"
//                loadding.src = settings.loading;
//                $(".loadding").attr("style","margin-top:"+(settings.height - (loadding.height * settings.width) / loadding.width) / 2+";");
//                $(images).after(loadding);    
               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(settings.height===0||settings.width ===0) return;
//                        var m = this.height-settings.height;
//                        var n = this.width - settings.width;
//                        if(m>n)
//                        {                     
//                            this.height = this.height>settings.height ? settings.height : this.height;
//                        }
//                        else
//                        {
//                            this.width = this.width >settings.width ? settings.width : this.width;
//                        }
                        
                        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.width) / img.width)) / 2;
					            $(this).css("margin-left", margin)
                            }
				        }

                        //$(this).next(".loadding").remove()
                        $(this).show();
                }
                return $(images).each(function(){
                    preLoad(this);
                });                
        }
})(jQuery);

