﻿(function($) {
    $.fn.ellipsis = function() {
        return this.each(function() {
            var el = $(this);
            var title = $(el.find(".title"));
            var author = $(el.find(".author"));

            var originalTitle = title.html();

            var titleClone = $(title.clone()).hide().css({
                'position': 'absolute',
                'width': title.width(),
                'height': 'auto',
                'max-height': 'none',
                'overflow': 'visible'
            });
            title.after(titleClone);

            var text = originalTitle;
            var h = author.height() || 0;
            while (text.length > 0 && titleClone.height() > el.height() - h) {
                text = text.substr(0, text.length - 1);
                titleClone.html(text + "&hellip; ");
            }
            title.html(titleClone.html());

            titleClone.remove();
        });
    };
})(jQuery);