/**
 * Fix the width of 'embed' tags such that they won't overflow
 * out of the selected elements
 * This is doen by modifying the width of too-wide siblings to be equal to
 * the width of each selected element.
 * The height is also modified to maintain the original aspect-ratio
 *
 * @author Gilad
 */
(function($){
    $.fn.fixEmbedWidth = function() {
        this.each(function (i) {
            var parent = $(this);
            var maxWidth = parent[0].offsetWidth - (parseInt(parent.css("paddingLeft")) || 0) - (parseInt(parent.css("paddingRight")) || 0);
            parent.find('embed').each(function (i) {
                if( $('embed').attr('width') > maxWidth ) {
                    var height = this.offsetHeight / this.offsetWidth * maxWidth;
                    $(this).width(maxWidth).height(height);
                }
            });
        });
        // Chain:
        return this;
    };
})(jQuery);
