// ---------------------------------------------------------------------------------------------
//	Javascript functions used to support a scrolling news ticker
//	Version	Date			Reason
//	1.0		12-Oct-2010		First production release
// ---------------------------------------------------------------------------------------------
var tickerTape = {
	_ContainerElement: null,
	_tickerTapeElement: null,
	_StartStopLinkElement: null,
	_StartStopButtonElement: null,
	_tickerTapeText: null,
	_intervalProcess: null,
	_currentScrollState: null,

	init: function () {
		var filename = '/data/ticker_tape_text.txt';

		function makeXMLHttpRequest(filename) {
			var timer;
			try {
				request = new XMLHttpRequest();
			} catch (exception) {
				try {
					request = new ActiveXObject('Microsoft.XMLHTTP');
				} catch (exception) {
					request = null;
				}
			}
			if (request !== null) {
				timer = setTimeout(function() {
					request.abort();
				}, 10000); // ten second timeout
				request.open('GET', filename, true);
				request.onreadystatechange = function() {
					if (request.readyState == 4) {
						clearTimeout(timer);
						if (request.status == 200 || request.status == 304) {
							/* retrieve the text for the ticker tape and remove leading/trailing spaces */
							tickerTape._tickerTapeText = request.responseText;
							tickerTape._tickerTapeText.trim;
							/* set up the ticker tape */
							createTickerTape();
						}
					}
				}
				request.send(null);
			}
		};
		function createTickerTape() {
			if(!tickerTape._tickerTapeText) {
				return; // don't create the ticker tape if the ticker tape text is empty
			}

			/* create the container element and insert it before the content */
			var element;
			var afterElement = Base.byId('content');
			var parentElement = afterElement.parentNode;
			element = document.createElement('div');
			tickerTape._ContainerElement = parentElement.insertBefore(element, afterElement);
			tickerTape._ContainerElement.setAttribute('id', 'ticker-tape-container');

			/* create the ticker tape element */
			element = document.createElement('div');
			tickerTape._tickerTapeElement = tickerTape._ContainerElement.appendChild(element); 
			tickerTape._tickerTapeElement.setAttribute('id', 'ticker-tape');

			/* create the pause/restart button element */
			tickerTape._StartStopLinkElement = document.createElement('a');
			tickerTape._StartStopLinkElement.setAttribute('id', '');
			tickerTape._StartStopLinkElement.id = 'ticker-tape-start-stop';
			tickerTape._StartStopLinkElement.setAttribute('href', '');
			tickerTape._StartStopLinkElement.href = '#';
			tickerTape._StartStopLinkElement.appendChild(document.createTextNode('Stop/start ticker tape'));
			tickerTape._StartStopLinkElement.setAttribute('title', '');
			tickerTape._StartStopLinkElement.title = 'Stop ticker tape';

			tickerTape._StartStopButtonElement = document.createElement('div');
			tickerTape._StartStopButtonElement.appendChild(tickerTape._StartStopLinkElement);

			if (tickerTape._ContainerElement.nextSibling != null) {
				tickerTape._ContainerElement.parentNode.insertBefore(tickerTape._StartStopButtonElement, tickerTape._ContainerElement.nextSibling);
			} else {
				tickerTape._ContainerElement.parentNode.appendChild(tickerTape._StartStopButtonElement);
			}
			Base.addEventListener(tickerTape._StartStopLinkElement, 'click', tickerTape.toggleScrollingOnOff);

			/* get width of the ticker tape */
			if (Base.getComputedStyle(tickerTape._tickerTapeElement, 'position') === 'relative') {
				var relativeWidth = tickerTape._tickerTapeElement.offsetWidth;
				tickerTape._tickerTapeElement.style.position = 'absolute';
				tickerTape._tickerTapeElement.calculatedWidth = tickerTape._tickerTapeElement.offsetWidth;
				if (relativeWidth > tickerTape._tickerTapeElement.calculatedWidth) {
					tickerTape._tickerTapeElement.calculatedWidth = relativeWidth;
				}
				tickerTape._tickerTapeElement.style.position = 'relative';
			} else {
				tickerTape._tickerTapeElement.calculatedWidth = tickerTape._tickerTapeElement.clientWidth;
			}
			
			/* write the retrieved text to the ticker tape */
			element = document.createTextNode(tickerTape._tickerTapeText);
			tickerTape._tickerTapeElement.appendChild(element);

			/* start ticker tape to the right, to allow the reader time to absorb the information */
			tickerTape._tickerTapeElement.style.left = '550px';

			/* start scrolling the ticker tape */
			tickerTape.startScrolling();
			return true;
		};
		makeXMLHttpRequest(filename);
		return true;
	},
	scrollTickerTape: function() {
		var increment = 4;
		var currLeft = parseInt(tickerTape._tickerTapeElement.style.left);

		if (currLeft < tickerTape._tickerTapeElement.calculatedWidth * -2.8) { // lengthen for longer ticker texts, shorten for shorter texts
			tickerTape._tickerTapeElement.style.left = tickerTape._ContainerElement.offsetWidth + 'px';
		} else {
			tickerTape._tickerTapeElement.style.left = (parseInt(tickerTape._tickerTapeElement.style.left) - increment) + 'px';
		}
	},
	startScrolling: function() {
		tickerTape._currentRotationState = 1;
		if (window.setInterval) {
			clearInterval(tickerTape._intervalProcess);
			tickerTape._StartStopLinkElement.title = 'Stop ticker tape';
			if (typeof document.getElementById !== 'undefined') { // modern browsers
				tickerTape._intervalProcess = setInterval(function () { tickerTape.scrollTickerTape(); }, 65);
			} else if (typeof document.all !== 'undefined') {
				tickerTape._intervalProcess = setInterval('tickerTape.scrollTickerTape', 65);
			}
		}
		return true;
	},
	stopScrolling: function () {
		tickerTape._currentRotationState = 0;
		if (window.setInterval) { 
			clearInterval(tickerTape._intervalProcess);
		}
		tickerTape._StartStopLinkElement.title = 'Restart ticker tape';
	},
	toggleScrollingOnOff: function () {
		if (tickerTape._currentRotationState === 1) { // on - switch off
			tickerTape.stopScrolling();
		} else { // off - switch on
			tickerTape.startScrolling();
		}
	}
};

Base.start(tickerTape);
