$(window).load(function() {
	$('.box1.ads .content img').each(function(){
        doResize($(this));
	});
	
	$('.pic.fl img').each(function(){
		doResize($(this));
	});
	
	$('.pic.flr img').each(function(){
		doResize($(this));
	});
	
	$('.review-box.fl img').each(function(){
		doResize($(this));
	});
	
	$('.review-box.flr img').each(function(){
		doResize($(this));
	});
	
	$('#postImage').each(function(){
		doResize($(this));
	});
});

function doResize(image) {
	var maxWidth = image.width(); // Max width for the image
    var maxHeight = image.height();       // Max height for the image
    
    image.css("width", "auto").css("height", "auto"); // Remove existing CSS
    image.removeAttr("width").removeAttr("height"); // Remove HTML attributes
    var width = image.width();    // Current image width
    var height = image.height();  // Current image height
    
    if(width > height) {
        // Check if the current width is larger than the max
        if(width > maxWidth){
            var ratio = maxWidth / width;   // get ratio for scaling image
            image.css("width", maxWidth); // Set new width
            image.css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;        // Reset height to match scaled image
        }
    } else if (width < height){
        // Check if current height is larger than max
        if(height > maxHeight){
            var ratio = maxHeight / height; // get ratio for scaling image
            image.css("height", maxHeight);   // Set new height
            image.css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;  // Reset width to match scaled image
        }
    } else {
    	image.css("height", maxHeight);   // Set new height
        image.css("width", maxWidth);    // Scale width based on ratio
    }
}
