﻿/*
 * Resize images to their context.
 */
$(document).ready(function() {
    /*
     * Set maxwidth attribute on the image src
     */
    function fixMaxWidth(attr, newWidth) {
        var index = attr.indexOf("maxwidth");

        if (index > -1) {
            var oldWidth = attr.substr(index + 9, 3);
            if (parseInt(oldWidth) > newWidth)
                return attr.replace("maxwidth=" + oldWidth, "maxwidth=" + newWidth);
        }
        return attr + (attr.indexOf("?") == -1 ? "?" : "&") + "maxwidth=" + newWidth;
    }

    /*
     * Container size rules
     */
    $("body.default .message img").attr("src", function () {
        return fixMaxWidth($(this).attr("src"), 300);
    });
});

