
function autoscale(selectors)  {

	
		
		$(selectors).each(function(){
			
		
			$(this).fadeTo(1,0,function(){
				
				$(this).width('auto');
				$(this).height('auto');
				
				var width = $(this).width();
				var height = $(this).height();
				
				if(width == 0) width = $(this).attr('width');
				if(height == 0) height = $(this).attr('height');
				

				var target_width = $(this).parent().width();
				var target_height = $(this).parent().height();

				if (width < target_width || width > target_width) {
					var ratio = (height / width);
					var new_width = target_width;
					var new_height = (new_width * ratio);
					target_width = target_width-1;
					
					if(new_height > target_height)
					{
						do 
						{ 
							new_width = target_width;
							new_height = (new_width * ratio);
							target_width = target_width-1;
							if(new_height<=target_height){ $(this).height(new_height).width(new_width); $(this).fadeTo(1000,1); }
						} 
						while(new_height>target_height);
					}else
					{	
						$(this).height(new_height).width(new_width); 
						$(this).fadeTo(1000,1);
					}
				}else
				{
					$(this).fadeTo(1000,1);
				}
				
				
			});
			
			
		}
		);
	
	

}