/*
* Resize, A resize plugin for jQuery
* Intructions: $(seletor).resize(max_size)
* By: Carlos Alessandro Sena de Freitas - veneinzuela@gmail.com
* Version: 1.0
* Updated: September 1st, 2011
*
* Licensed under the GNU - General Public License 
* You may obtain a copy of the License at
*
* http://www.gnu.org/copyleft/gpl.html
*
*/
(function( $ ){
	$.fn.resize = function(max_width, max_height) 
	{
   	return this.each(function() 
		{  
			//Wait until the image is loaded
			$(this).load(function()
			{
				//Create a container div
				var div_container = $("<div></div>");
				div_container.css({ 'height': max_height, 'width': max_width, 'text-align': 'center', 'vertical-align': 'middle'});
				
				var scale_w = max_width / $(this).width();
				var scale_h = max_height / $(this).height();
				
				$(this).removeAttr("width");
				$(this).removeAttr("height");
				//Get the new size of the image
				if (scale_w > scale_h) 
				{
					 var h = max_height;
					 var w = Math.floor($(this).width()  * scale_h);
				} 
			  	else 
				{
					var w = max_width;
				 	var h = Math.floor($(this).height() * scale_w);
			  	}

				//Centralize the image
				var top  = Math.floor((max_height-h)/2);
				var left = Math.floor((max_width-w)/2);

				//Resize the image and put it in the container div
			  	$(this).css({ 'height': h, 'width': w, 'top': top, 'left': left});
				$(this).parent().append(
					div_container.append($(this).detach())
				);

			});
		});  
  	};
})( jQuery );
