$(document).ready(function () {

    var productMainInfo = $('.product-main-info__description'),
        productMainReadLess = $('.product-main-info__read-less'),
        productMainReadMore = $('.product-main-info__read-more'),
        lineHeight = parseInt(productMainInfo.css('line-height')),
        descriptionHeight = parseInt(productMainInfo.css('height')),
        productMainInfoText = productMainInfo.html(),
        clampSize = 10;

    function clampDescription() {

        var text = productMainInfo.html(),
            height = productMainInfo.height(),
            clone = productMainInfo.clone();

        clone.css({
            position: 'absolute',
            visibility: 'hidden',
            height: 'auto'
        });

        productMainInfo.after(clone);

        var i = text.length - 1;
        for (; i >= 0 && clone.height() > height; --i) {
            clone.html(text.substring(0, i));
        }

        var cloneText = clone.html();

        productMainInfo.html(cloneText.substring(0, i - 15) + '...');
        clone.remove();
    }

    if (descriptionHeight > lineHeight * clampSize) {

        productMainInfo.css('height', lineHeight * clampSize);
        productMainReadMore.css('display', 'inline');
        clampDescription();

        productMainReadLess.on('click', function () {

            productMainInfo.css('height', lineHeight * clampSize);
            productMainReadLess.css('display', 'none');
            productMainReadMore.css('display', 'inline');
            clampDescription();
        });

        productMainReadMore.on('click', function () {
            productMainInfo.html(productMainInfoText);
            productMainInfo.css('height', 'auto');
            productMainReadMore.css('display', 'none');
            productMainReadLess.css('display', 'block');
            productMainInfo.css('margin-bottom', '5px');
        });
    }

    $(window).on('resize', function () {

        if (descriptionHeight > lineHeight * clampSize &&
            productMainReadLess.css('display') === 'none') {

            productMainInfo.html(productMainInfoText);
            clampDescription()
        }
    });
});