var resizeImage = function(me, blockWidth) {
    /**
     * Определяем ширину изображения
     */
    var imgWidth = me.outerWidth();
    /**
     * Если изображение шире блока, то удаляем его атрибуты размеров и
     *  отступов, и назначаем ему новую ширину
     */
    if (imgWidth > blockWidth) {
        me.removeAttr('height')
          .removeAttr('width')
          .removeAttr('hspace')
          .removeAttr('vspace')
          .attr('width', blockWidth);
    }
}
var resizeVideo = function(me, blockWidth) {
    var seprx, srcrx, width, height, proportion;
    seprx = new RegExp(/\?/);
    srcrx = new RegExp(/youtube\.com|youtu\.be/);
    var src = me.attr('src');
    if (srcrx.test(src)) {
        var sep = (seprx.test(src))
                ? '&'
                : '?';
        me.attr('src', src + sep + 'wmode=transparent');
        width   = me.attr('width');
        height  = me.attr('height');
        proportion = width / height;
        if (width > blockWidth) {
            me.attr('width', blockWidth);
            height = blockWidth / proportion
            me.attr('height', height);
        }
    }
}
/**
 * if DOM ready
 */
jQuery(document).ready(function(){
    /**
     * Посты
     */
    var postBlockId = '.post-inner';
    var postWidth = jQuery(postBlockId).innerWidth();
    jQuery(postBlockId + ' img').each(function(index) {
        var imgWidth = jQuery(this).outerWidth();
        console.log();
        if (imgWidth >= '400') {
            jQuery(this).addClass('bottom-margin lightbox');
            jQuery(this).parent('a')
                        .replaceWith(jQuery(this));
        }
        resizeImage(jQuery(this), postWidth);
    });
    jQuery(postBlockId + ' iframe').each(function(index) {
        resizeVideo(jQuery(this), postWidth);
    });
    /**
     * Комментарии
     */
    var commentBlockId = '.comment-txt';
    var commentWidth = jQuery(commentBlockId).innerWidth();
    jQuery(commentBlockId + ' img').each(function(index) {
        resizeImage(jQuery(this), commentWidth);
    });
    jQuery(commentBlockId + ' iframe').each(function(index) {
        resizeVideo(jQuery(this), commentWidth);
    });
});
