﻿//ellipsis plugin http://devongovett.wordpress.com/2009/04/06/text-overflow-ellipsis-for-firefox-via-jquery/ + comments + custom mods
(function($) {
	$.fn.ellipsis = function(lines, enableUpdating, moreLink, lessLink) {
		return $(this).each(function() {
			var el = $(this);
			if (el.css("overflow") == "hidden") {
				var originalText = el.html();
				var availWidth = el.width();
				var availHeight = el.height();
				var MoreLessTag;
				if (!moreLink) {
					MoreLessTag = "";
				}
				else if (typeof moreLink === "string") {
					enableUpdating = true;
					MoreLessTag = " <a class='MoreLessTag' href='#' >" + moreLink + "</a>";
				};
				var t = $(this.cloneNode(true))
					.hide()
					.css({
						'position': 'absolute',
						'overflow': 'visible',
						'max-width': 'none',
						'max-height': 'none'
					});
				if (lines) t.css("height", "auto").width(availWidth);
				else t.css("width", "auto");
				el.after(t);
				var fullHeight = t.height();
				var avail = (lines) ? availHeight : availWidth;
				var test = (lines) ? t.height() : t.width();
				var foundMin = false, foundMax = false;
				if (test > avail) {	//Binary search style trimming of the temp element to find its optimal size
					var min = 0;
					var max = originalText.length;
					while (min <= max) {
						var trimLocation = (min + max) / 2;
						var text = originalText.substr(0, trimLocation);
						t.html(text + "&hellip;" + (typeof MoreLessTag === "string" ? MoreLessTag : ""));
						if (typeof MoreLessTag !== "string") {
							$j(moreLink).appendTo(t);
						};
						test = (lines) ? t.height() : t.width();
						if (test > avail) {
							if (foundMax)
								foundMin = true;
							max = trimLocation - 1;
							if (min > max) { //If we would be ending decrement the min and regenerate the text so we don't end with a slightly larger text than there is space for
								trimLocation = (max + max - 2) / 2;
								text = originalText.substr(0, trimLocation);
								t.html(text + "&hellip;" + (typeof MoreLessTag === "string" ? MoreLessTag : ""));
								if (typeof MoreLessTag !== "string") {
									$j(moreLink).appendTo(t);
								};
								break;
							};
						}
						else if (test < avail) {
							min = trimLocation + 1;
						}
						else {
							if (foundMin && foundMax && ((max - min) / max < .2)) break;
							foundMax = true;
							min = trimLocation + 1;
						};
					}
				};
				el.html(t.html());
				t.remove();
				if (MoreLessTag) {
					jQuery(".MoreLessTag", this).click(function(event) {
						event.preventDefault();
						el.html(originalText);
						if (lessLink) {
							el.append(" <a class='MoreLessTag' href='#' >" + lessLink + "</a>");
						};
						jQuery(".MoreLessTag", el).click(function(event) {
							event.preventDefault();
						});
					});
				}
				else {
					var replaceTags = new RegExp(/<\/?[^>]+>/gi);
					el.attr("alt", originalText.replace(replaceTags, ''));
					el.attr("title", originalText.replace(replaceTags, ''));
				};
				if (enableUpdating == true) {
					var oldW = el.width();
					var oldH = el.height();
					el.one("resize", function() {
						if (el.width() != oldW || (lines && el.height != oldH)) {
							el.html(originalText);
							el.ellipsis(lines, enableUpdating, moreText, lessText);
						};
					});
				};
			};
		});
	};
})(jQuery);

