//links - Add your link and copy in the array

var arrNewsItems = new Array();
arrNewsItems.push(new Array('Rotary Magic for The Memory Cafe', 'nws_club.cfm'));

arrNewsItems.push(new Array('Wadebridge Rotary Club Helps Youth Project', 'nws_club.cfm'));

arrNewsItems.push(new Array('Click Here to view our latest News', 'nws_club.cfm'));

arrNewsItems.push(new Array('Thanks for Life - End Polio Now Programme - India', 'w_ss_india.cfm'));

arrNewsItems.push(new Array('Football 2010', 'nws_club.cfm'));

arrNewsItems.push(new Array('Thanks for Life - End Polio Now Programme tops 1 million pounds', 'nws_club.cfm'));

arrNewsItems.push(new Array('Wadebridge Rotary Club Wins an Award', 'http://www.rotaryclubwadebridge.org/nws_club.cfm?StartRow=6&OrderBy=ordr1&Direction=ASC'));

arrNewsItems.push(new Array('News of our Memory Cafe', 'sje3.cfm?sje=CE71933E-AF41-E741-EA77C55A6F188232'));

arrNewsItems.push(new Array("The latest Wheels News - one of the great attractions in Cornwall, held every year", 'sje2.cfm?sje=7'));

arrNewsItems.push(new Array('Schools Make Music', 'http://www.rotaryclubwadebridge.org/nws_club.cfm?StartRow=6&OrderBy=ordr1&Direction=ASC'));

var intTickSpeed = 9000;
var intTickPos = 0;
var tickLocked = false;
var fadeTimerID;
var autoTimerID = 0;
var intTypeSpeed = 20;
var intCurrentPos = 0;
var currentText = '';
var currentLink = '';
var strText = '';
var isFirstPass = true;


// Buttons - Please don't edit below 

function initButtons() {
  var kids = document.getElementsByTagName('img');
  for (var i=0; i < kids.length; i++) {
    kids[i].onclick = buttonClick;
    kids[i].onmousedown = buttonDown;
    kids[i].onmouseup = buttonUp;
    kids[i].oncontextmenu = buttonMenu;
  }
  //document.getElementById("tickContentLink").onmouseover = stopTicker;
  //document.getElementById("tickContentLink").onmouseout = resumeTicker;
	getLinkElems();
	playFirstTicker();
}
function buttonMenu(e) {
	return false;
}
function buttonDown(e) {
	if (!e) var e = window.event;
	if ((tickLocked == false) && (e.button != 2)) {
		document.getElementById(this.id).style.cssText = "margin: 2px 0px 0px 2px;";
	}
}
function buttonUp(e) {
	if (!e) var e = window.event;
	if ((tickLocked == false) && (e.button != 2)) {
		document.getElementById(this.id).style.cssText = "";
	}
}
function buttonClick(e) {
  delayTicker();
  if (this.id == "back") {
    prevArticle();
  } else if (this.id == "next") {
    nextArticle();
  }
}
function prevArticle() {
  if (tickLocked == false) {
	  if (intTickPos == 0) {
	    intTickPos = arrNewsItems.length-1;
	  } else {
	    intTickPos--;
		}
		setArticle(intTickPos);
	}
}
function nextArticle() {
	if (tickLocked == false) {
	  if (intTickPos == arrNewsItems.length-1) {
	    intTickPos = 0;
	  } else {
	    intTickPos++;
		}
		setArticle(intTickPos);
	}
}
function typeText() {
	if(intCurrentPos < currentText.length) {
		strText += currentText.charAt(intCurrentPos);
		setSpan(strText,currentLink);
		intCurrentPos++;		
	} else if (intCurrentPos == currentText.length) {
		strText += currentText.charAt(intCurrentPos);
		setSpan(strText,currentLink);
		clearInterval(typeInterval);
		hideCursor();
	} else if (intCurrentPos > currentText.length){
		setSpan(strText,currentLink);
		clearInterval(typeInterval);
		hideCursor();
	}
}
function setSpan(strText, strLink) {
	var tickElem = document.getElementById("tick");
		var tickFirstChild =  tickElem.firstChild;
		var tickLinkElem = document.createElement("a");
		tickLinkElem.setAttribute('href', strLink);
    tickLinkElem.setAttribute('target', '_top');
		//tickLinkElem.setAttribute('id', 'tickContentLink');
		tickText = document.createTextNode(strText);
		tickLinkElem.appendChild(tickText);
		tickElem.replaceChild(tickLinkElem,tickFirstChild);
		getLinkElems();
}
function getLinkElems() {
	var tickerElem = document.getElementById("tick"); 
	var tickerAElem = tickerElem.getElementsByTagName("a"); 
	for (var i=0; i < tickerAElem.length; i++) {
    tickerAElem[i].onmouseover = stopTicker;
   	tickerAElem[i].onmouseout = resumeTicker;
  }
}

function setArticle(intPos) {
	if(arrNewsItems[intPos]!=null) {
		tickLocked = true;
		intCurrentPos = 0;
		strText = '';
		setSpan('', '#');
		showCursor();
		currentText = arrNewsItems[intPos][0];
		currentLink = arrNewsItems[intPos][1];
		typeInterval = setInterval( "typeText()", intTypeSpeed);
		tickLocked = false;
  }
}
function playTicker() {
  isInFirstTimeout = false;
	if (autoTimerID != 0) {
		clearInterval(typeInterval);
		nextArticle();
  }
  autoTimerID = self.setTimeout("playTicker()", intTickSpeed);
}
function playFirstTicker() {
	if(isFirstPass == true) {
		hideCursor();
		setSpan(arrNewsItems[0][0],arrNewsItems[0][1]);
		isFirstPass = false;
		typeInterval = setInterval('',0);
		isInFirstTimeout = true;
		autoTimerID = self.setTimeout("playFirstTicker()", intTickSpeed);
	}
	else if(isFirstPass == false) {
		clearTimeout(autoTimerID);
		isInFirstTimeout = false;
		setArticle(intTickPos);
		playTicker();
	}
}
function stopTicker() {
	clearTimeout(autoTimerID);
}
function resumeTicker() {
	clearTimeout(autoTimerID);
	autoTimerID = self.setTimeout("playTicker()", intTickSpeed);
}
function delayTicker() {
  clearTimeout(autoTimerID);
	clearInterval(typeInterval);
  autoTimerID = self.setTimeout("playTicker()", intTickSpeed * 2);
}
function hideCursor() {
	document.getElementById("cursor").className = "hidden";
}

function showCursor() {
	document.getElementById("cursor").className = "";
}
initButtons();