/* JS to trim news ticker article titles with char limit depending on browser width
Expects: a previously declared array called news_titles to exist */

/*  trims a string to a specified charLimit, trimming shorter to the end of a word if possible and adding an ellipsis */
function trimmedText(originalText, charLimit)
{
	trimText = originalText.substr(0,charLimit);
	if (originalText.length > charLimit) {
		if (trimText.lastIndexOf(" ") != -1)
			trimText = trimText.substr(  0 , trimText.lastIndexOf(" ")  );
		trimText = trimText + "...";
	}
	return trimText;
}

/* our method of deciding the charLimit from the browser width obviously depends on font etc, here's a rough n ready attempt */
function tickerLimitFromWidth(winWidth)
{
	tickerWidth = 0.5482 * winWidth;
	if (tickerWidth < 458) tickerWidth = 458;
	return Math.round(tickerWidth / 6) - 12;
}

/* trims the news titles according to browser size */
function resizeTicker()
{
	charLimit = tickerLimitFromWidth(document.body.clientWidth);
	for (i=0;i<news_titles.length;i++)
		document.getElementById("news_title_"+i).innerHTML = trimmedText(news_titles[i],charLimit);
}

/* called on loading the page */
function initTickerResizer()
{
	window.onresize = resizeTicker;
	resizeTicker();
}
