(function($) {
	$.fn.ellipsis = function(enableUpdating){
		var s = document.documentElement.style;
		if (!('textOverflow' in s || 'OTextOverflow' in s)) {
			return this.each(function(){
				var el = $(this);
				if(el.css("overflow") == "hidden"){
					var originalText = el.html();
					var w = el.width();
					
					var t = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'width': 'auto',
                        'overflow': 'visible',
                        'max-width': 'inherit',
						'height': 'auto',
                    });
					el.after(t);
					
					var text = originalText;
					while (text.length > 0 && this.scrollHeight > $(this).innerHeight()) {
						var index = text.lastIndexOf(' ');
						if (index < 0)
							index = 0;
						else if (index > 0 && (text[index - 1] == '.' ||
						  text[index - 1] == ','))
							index--;
						text = text.substr(0, index);
						$(this).html(text + "...");
						t.html(text + "...");
					}
					el.html(t.html());
					
					t.remove();
					
					if(enableUpdating == true){
						var oldW = el.width();
						setInterval(function(){
							if(el.width() != oldW){
								oldW = el.width();
								el.html(originalText);
								el.ellipsis();
							}
						}, 200);
					}
				}
			});
		} else return this;
	};
})(jQuery);
