var ImgDimensions = {
    thumb: {width: 124, height: 86},
    album: {width: 474, height: 356}
};

/**
 * Se a imagem for menor que os limites para os lados e para cima, a imagem
 * que deve ser exibida é a original.
 * 
 * @param img
 * @param dim
 */
function resizeImg(img, dim) {
    img = $(img);
    var imgWidth = img.width();
    var imgHeight = img.height();    
    
    if (!(imgWidth <= dim.width
    		&& imgHeight <= dim.height)) {
    	var newWidth = imgWidth + (dim.width - imgWidth);
	    var widthDiff = newWidth * 100 / imgWidth;    
	    var newHeight = imgHeight * widthDiff / 100;
	    
	    if (newHeight > dim.height) {
	        newHeight = dim.height;
	        var heightDiff = newHeight * 100 / imgHeight;
	        newWidth = imgWidth * heightDiff / 100;
	    }
	    
	    var horizontalMargin = Math.floor((dim.width - newWidth) / 2);
	    var marginBottom = dim.height - newHeight;
	    
	    if (imgWidth != newWidth) {
	        img.width(Math.floor(newWidth));
	    }
	    
	    if (imgHeight != newHeight) {
	        img.height(Math.floor(newHeight));
	    }
	    
	    if (horizontalMargin > 0) {
	    	img.css("margin-left", horizontalMargin + "px");
	    	img.css("margin-right", horizontalMargin + "px"); 
	    }
	    
	    if (marginBottom > 0) {
	    	img.css("margin-bottom", Math.round(marginBottom) + "px");
	    }
    }
}
