
(function($){$.fn.backscale=function(options,callback){var settings={hideUntilReady:true,speed:500,center:false,border:0,debug:true},imgRatio=0,imgHeight=0,imgWidth=0;if(options&&typeof options=='object')$.extend(settings,options);return this.each(function(){var self=$(this);var image=new Image();var commonCSS={left:0,top:0};var uid;if(settings.hideUntilReady)self.hide();var wrap=$('<div />').attr('id','backscale-wrap').css($.extend(commonCSS,{position:'absolute',zIndex:-1}));var container=$('<div />').attr('id','backscale').css($.extend(commonCSS,{position:'relative',overflow:'hidden',zIndex:-1})).appendTo(wrap);$(image).bind('load',function(){imgWidth=self.css('width').slice(0,-2);imgHeight=self.css('height').slice(0,-2);imgRatio=imgWidth/imgHeight;_resizeBG(function(){if(settings.hideUntilReady){self.show();if(typeof callback=='function')callback();}});}).bind('error',function(){if(window.console)console.log('[backscale] Image could not be found: '+self.attr('src'));}).attr('src',self.attr('src'));self.wrap(wrap);$(window).resize(_resizeBG);function _resizeBG(callback){var parent;var newWidth=$(window).width();var newHeight=newWidth/imgRatio;if(settings.center){newWidth=imgWidth;newHeight=imgHeight;if(newHeight>$(window).height()){newHeight=$(window).height();newWidth=newHeight*imgRatio;newHeight-=(settings.border*2);newWidth-=(settings.border*2);}parent=$.browser.msie?parent=wrap:parent=self.parent();parent.css({marginTop:($(window).height()/2)-(newHeight/2),marginLeft:($(window).width()/2)-(newWidth/2)});}else{if(newHeight<$(window).height()){newHeight=$(window).height();newWidth=newHeight*imgRatio;}}self.width(newWidth).height(newHeight);if(typeof callback=='function')callback();};});return this;};})(jQuery);
