var copyspeed = 1;
var counter = 0;
var opacity = [100,50,25,10,5];
var nFade = opacity.length;
var ticker = '';
var content = '';
var newContent = '';
var tickerBar = '';
var width = 0;
var intID = 0;
var fadeID = 0;
var n = 0;
var x = nFade;

function initTicker(tickerID){
	getContent(tickerID);
	reformat();
	setVisible();
	showAllImediately();
	//startTicker();
}

function initTickerFade(tickerID){
	getContent(tickerID);
	reformat();
	showAllFadeAtOnce();
	//startTicker();
}

function initTickerFadeIn(tickerID){
	getContent(tickerID);
	reformat();
	startFadeIn();
	//startTicker();
}

function showAllFadeAtOnce(){
	//getNodes();
	setHidden();
	x = 1;
	fadeID=window.setInterval("showAllImediately()",100);
}


function getContent(ID){
	ticker = document.getElementById(ID); 
	tickerWidth = ticker.offsetWidth;
	content = ticker.innerHTML;
}


function reformat(){
	newContent = '<div id="fadedEdgeLeft">&nbsp</div><div id="fadedEdgeRight">&nbsp</div><div id="overflow" onMouseOver="copyspeed=0;" onMouseOut="copyspeed='+copyspeed+'"><span id="tickerBar">' + content + '</span></div>';
	ticker.innerHTML = newContent;

	tickerBar = document.getElementById("tickerBar");
	tickerBar.style.marginLeft = 0 +"px";
	width = parseInt(tickerBar.offsetWidth);
	
}

function startTicker(){
	// Only start scrolling if the ticker is not wide enough to hold its contents
	if(width>tickerWidth){
		div1Interval = window.setInterval("scroll()",20);
	}
}

function startFadeIn(){
	//getNodes();
	setHidden();
	counter=n-1;
	intID = window.setInterval("fadeIn()",70);
}

function scroll(){
	var currentPostion = parseInt(tickerBar.style.marginLeft);
	
	//Resetting ticker at the exact right time gives the appearance of an endless loop
	if (width + currentPostion <= 0){
		tickerBar.style.marginLeft = "0px";
	}
	//Otherwise continue scrolling to the left
	else {
		tickerBar.style.marginLeft = currentPostion-copyspeed+"px";
	}
}

function fadeIn(){
	var i = counter;
	counter-=1;
	// Loop through array of opacity 100%, 50% etc
	for (x=0; x<nFade; x++){
		if (i-x>=0 && i<n){
			tagname = aNodes[i-x].tagName;
			// If <img> or <a> tag 
			if (tagname=='A'){
				setOpacity(aNodes[i-x].firstChild,opacity[x]);
			}
			if (tagname=='IMG'){
				setOpacity(aNodes[i-x],opacity[x]);
			}
		}	
	}
	// Quit if we're at the end of the list of elements
	if (i<=0) {
		window.clearInterval(intID);
		reformat();
		//getNodes();
		setVisible();
		startTicker();
	}
}

function showAllImediately(){
	aNodes = tickerBar.childNodes;
	n = parseInt(aNodes.length);
	for(i=0; i<n; i++){
		tagname = aNodes[i].tagName;
		// If <img> or <a> tag 
		if (tagname=='A'){
			setOpacity(aNodes[i].firstChild,opacity[nFade-x]);
		}
		if (tagname=='IMG'){
			setOpacity(aNodes[i],opacity[nFade-x]);
		}
	}
	if(x>=nFade){
		window.clearInterval(fadeID);
		reformat();
		//getNodes();
		setVisible();
		startTicker();
	}
	x++;
}


function setOpacity(obj,alpha){
	obj.style.opacity=alpha/100;
    obj.style.filter='alpha(opacity='+alpha+')';
}



function setHidden(){
	getNodes();
	for(i=0; i<n; i++){
		tagname = aNodes[i].tagName;
		// If <img> or <a> tag 
		if (tagname=='A'){
			setOpacity(aNodes[i].firstChild,0);
			aNodes[i].firstChild.style.visibility = 'visible';
		}
		if (tagname=='IMG'){
			setOpacity(aNodes[i],0);
			aNodes[i].style.visibility = 'visible';
		}
	}
}

function setVisible(){
	if(width>tickerWidth){
		tickerBar.innerHTML += content;
	}
	getNodes();
	for(i=0; i<n; i++){
		tagname = aNodes[i].tagName;
		// If <img> or <a> tag 
		if (tagname=='A'){
			aNodes[i].firstChild.style.visibility = 'visible';
		}
		if (tagname=='IMG'){
			aNodes[i].style.visibility = 'visible';
		}
	}
}

function getNodes(){
	aNodes = tickerBar.childNodes;
	n = parseInt(aNodes.length);
}
