/*******************************************************************************************
 * ticker
 * Written by Craig Francis
 * Create a ticker by taking several <p>'s, lining them up then scrolling them.
 *******************************************************************************************/

	var ticker = new function () {

		//--------------------------------------------------
		// Script config

			this.speedRefresh = 75;
			this.speedDistance = 4;
			this.speedSeparation = 100;

			this.deadOffset = -9999;

		//--------------------------------------------------
		// Initialisation function used to define the global
		// variables used in this script

			this.init = function () {

				//--------------------------------------------------
				// Get a reference to the holder

					ticker.holder = document.getElementById('ticker');
					if (!ticker.holder) {
						return;
					}

				//--------------------------------------------------
				// Get a reference to the paragraph tags

					ticker.paras = ticker.holder.getElementsByTagName('p');
					if (!ticker.paras || ticker.paras.length < 1) {
						return;
					}

				//--------------------------------------------------
				// Style up the ticker holder.

					cssjs('add', ticker.holder, 'jsTickerEnabled');
					
					ticker.holder.style.overflow = 'hidden';

				//--------------------------------------------------
				// Attach the mouse event handlers used to stop the
				// ticker if the mouse rolls over it

					ticker.moveTickerInt = null;

					ticker.holder.onmouseover = function () {
						if (ticker.moveTickerInt !== null) {
							clearInterval(ticker.moveTickerInt);
							ticker.moveTickerInt = null;
						}
					}

					ticker.holder.onmouseout = function () {
						if (ticker.moveTickerInt === null) {
							ticker.moveTickerInt = setInterval(ticker.moveTicker, ticker.speedRefresh);
						}
					}

				//--------------------------------------------------
				// Style up each of the paras and set their left
				// offset to the "dead" value.

					ticker.paraOffsets = new Array();

					for (var k=(ticker.paras.length - 1); k>=0; k--) {

						ticker.paraOffsets[k] = ticker.deadOffset;

						ticker.paras[k].style.display = 'block';
						ticker.paras[k].style.position = 'absolute';
						ticker.paras[k].style.whiteSpace = 'nowrap';
						ticker.paras[k].style.left = ticker.paraOffsets[k] + 'px';

					}

				//--------------------------------------------------
				// Start the ticker from working

					setTimeout(ticker.startTicker, 100);

			}

		//--------------------------------------------------
		// As some browsers need the init function to
		// finish before they apply the display: block,
		// then this script is used to get the widths.

			this.startTicker = function () {

				//--------------------------------------------------
				// Get the holder width

					ticker.holderWidth = ticker.holder.offsetWidth;

				//--------------------------------------------------
				// Get the paragraphs widths

					ticker.paraWidths = new Array();

					for (var k=(ticker.paras.length - 1); k>=0; k--) {
						ticker.paraWidths[k] = ticker.paras[k].offsetWidth;
					}

				//--------------------------------------------------
				// Get things moving with the first (0) paragraph.

					ticker.paraOffsets[0] = ticker.holderWidth;
					ticker.paras[0].style.left = ticker.paraOffsets[0] + 'px';

				//--------------------------------------------------
				// Move the ticker in an animated fasion

					ticker.moveTickerInt = setInterval(ticker.moveTicker, ticker.speedRefresh);

			}

		//--------------------------------------------------
		// Function called at regular intervals to move
		// the paras

			this.moveTicker = function () {

				//--------------------------------------------------
				// Ensure we still have access to the ticker object

					if (!ticker || !ticker.holder) {
						return;
					}

				//--------------------------------------------------
				// Go though each para and process those which
				// are not dead.

					for (var k=(ticker.paras.length - 1); k>=0; k--) {
						if (ticker.paraOffsets[k] > ticker.deadOffset) {

							//--------------------------------------------------
							// Either kill (gone off screen) or move this
							// paragraph a bit more to the left

								if (ticker.paraOffsets[k] < (0 - ticker.paraWidths[k])) {
									ticker.paraOffsets[k] = ticker.deadOffset;

								} else {
									ticker.paraOffsets[k] -= ticker.speedDistance;
									ticker.paras[k].style.left = ticker.paraOffsets[k] + 'px';

								}

							//--------------------------------------------------
							// If this para has cleared the right side of the
							// holder, then try to load the next para.

								if ((ticker.paraOffsets[k] + ticker.paraWidths[k] + ticker.speedSeparation) < ticker.holderWidth) {

									var nextPara = k + 1;
									if (nextPara >= ticker.paras.length) {
										nextPara = 0;
									}

									if (ticker.paraOffsets[nextPara] == ticker.deadOffset) {
										ticker.paraOffsets[nextPara] = ticker.holderWidth;
										ticker.paras[nextPara].style.left = ticker.paraOffsets[nextPara] + 'px';
									}

								}

						}
					}

			}

		//--------------------------------------------------
		// Set JS specific styles ready for page load.

			addCssRule('#ticker p { position: absolute; left: -5000px; width: auto; }');

		//--------------------------------------------------
		// When the page has loaded, run the init function

			addLoadEvent (function() {
				ticker.init();
			});

	}
