﻿
/*** Trim text length for display in RssReader ***/
function autoEllipseText(textElementId, text, maxTitleLength) {
	var padding = 15;

	textElement = document.getElementById(textElementId);

	// Need to compute the amount of space used up by existing elements in the parent container
	// so that we know how much space is leftover for the text string passed in.

	// Calculate the available text string space we have to work with
	var availableSpace;
	if (maxTitleLength == -1) {
		// If no length specified, use the max length available in the HTML container.
		availableSpace = textElement.parentNode.offsetWidth - usedSpace - padding;
	} else {
		availableSpace = maxTitleLength - padding;
	}

	// Insert the full text, and the test to see if we're over the max length
	textElement.innerHTML = '<span id="' + textElementId + 'ellipsisSpan" style="white-space:nowrap;">' + text + '</span>';
	inSpan = document.getElementById(textElementId + 'ellipsisSpan');

	if (inSpan.offsetWidth > availableSpace) {
		inSpan.innerHTML = '';
		var j = 1;
		// Text is too long, so we'll add one character at a time.
		while ((inSpan.offsetWidth < availableSpace) && (j < text.length)) {
			inSpan.innerHTML = text.substr(0, j) + '...';
			j++;
		}

		textElement.innerHTML = '<span id="' + textElement.id + 'ellipsisSpan" >' + inSpan.innerHTML + '</span>';
	}

	return;
}
