/*
 * 
 */
(function($) {
    $.fn.inboximage = function(params) { 

        params = $.extend( {parent: false}, params);
        
        var _parentHeight, 
			_parentWidth, 
			_parentRatio,
			_imgHeight, 
			_imgWidth, 
			_imgRatio,
			_imgNewWidth, 
			_imgNewHeight;
        
        this.each( function() {
            var $img = $(this);
            var $parent = ( params.parent ? $img.parents( params.parent ) : $img.parent() );
			_scaleImage( $img, $parent );				
        });
		
        function _scaleImage( $img, $parent ) {
            $img.removeAttr('height').removeAttr('width');
			
			_parentWidth = $parent.width();
			_parentHeight = $parent.height();
            _parentRatio = _parentWidth / _parentHeight;
           
			_imgWidth = $img.width();
			_imgHeight = $img.height();
            _imgRatio = _imgWidth / _imgHeight;

			if ( _imgRatio < _parentRatio ) {
				_imgNewHeight = _parentHeight;
				_imgNewWidth = _imgRatio * _imgNewHeight;
			} else {
				_imgNewWidth = _parentWidth;
				_imgNewHeight = _imgNewWidth / _imgRatio;
			}
			
			$img.width(_imgNewWidth).height(_imgNewHeight);            
        }
    };
})(jQuery);
