(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 w = el.width();

					var t = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'width': 'auto',
                        'overflow': 'visible',
                        'max-width': 'inherit'
                    });
					el.after(t);
					

					var originalText = t.html();

					var reTrailingPunctuation = /(\W|\s|_)+$/;
					var text = originalText.replace(reTrailingPunctuation,'');
					while(text.length > 0 && t.width() > w){
						if (text.lastIndexOf(' ') != -1) {
							text = text.substr(0, text.lastIndexOf(' ')).replace(reTrailingPunctuation,'');;
						} else {
							text = text.substr(0, text.length - 1).replace(reTrailingPunctuation,'');;
						}
						t.html(text + "&hellip;");
					}
					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);
