// JavaScript Document
function startTicker() {
	if (document.getElementById) {
		    theTickerDiv = document.getElementById(theTickerDivId);
		    prePopulateOutputTable();
			runTheTicker();
		 }
	else {
            document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
            return true;
	}
}

function runTheTicker() {
	if(theCurrentLength == 0&&theArticleFadeCompleted==false&&theArticleFadeInit==true) {
		//we have reached the end of an article, fade text in
		myTimeout = theFadeTimeout;
		stepFadeArticle(theTickerRowIndex);
	}
	else if(theCurrentLength == 0&&(theCurrentStory==theItemCount-1||theTickerRowIndex==theMaxRows-1)&&theCurrentTagType == unknownTagType) {
		//we have reached the end of the articles, fade out
		myTimeout = theFadeTimeout;
		stepFade();
	}
	else {

		if(theCurrentLength == 0) {

				//starting a new tag
				if(theCurrentTagType == unknownTagType) {
					//starting a new row and date tag
					theCurrentStory++;
					theCurrentTagType = dateTagType;
					theArticleFadeCompleted = false;
				}

				theTickerRowIndex = theCurrentStory % theMaxRows;
				theCurrentStory      = theCurrentStory % theItemCount;
				theStorySummary      = theSummaries[theCurrentStory].replace(/&quot;/g,'"');
				theStoryDate      	 = theDates[theCurrentStory];
				theTargetLink        = theSiteLinks[theCurrentStory];
				if(theCurrentTagType == dateTagType) {
					theTDObject = document.getElementById("tickerDateTD"+theTickerRowIndex);
					theTDObject.innerHTML = beginDateTag(theTickerRowIndex)+closeDateTag();
					theAnchorObject = document.getElementById("tickerDate"+theTickerRowIndex);
				}
				else if(theCurrentTagType == storyTagType) {
					theTDObject = document.getElementById("tickerAnchorTD"+theTickerRowIndex);
					theTDObject.innerHTML = beginAnchorTag(theTickerRowIndex)+closeAnchorTag();
					theAnchorObject = document.getElementById("tickerAnchor"+theTickerRowIndex);
				}
				theAnchorObject.href = theTargetLink;
				theFadingTagIds[theFadingTagIds.length] = theAnchorObject.id;
				theTDTagIds[theTDTagIds.length] = theTDObject.id;

				//you may need to fade the TD objects as well as the anchor objects
				//theFadingTagIds[theFadingTagIds.length] = theTDObject.id;


		}

		var currenttext = "";
		if(theCurrentTagType == dateTagType) {
			currenttext = theStoryDate ;
		}
		else if(theCurrentTagType == storyTagType) {
			currenttext = theStorySummary;
		}

		if((theCurrentLength != theStorySummary.length&&theCurrentTagType == storyTagType)) {
			//add the new char to the date tag
			theAnchorObject.innerHTML = currenttext;
			theCurrentLength = theStorySummary.length;
			myTimeout = theCharacterTimeout;
		}
		else if((theCurrentLength != theStoryDate.length&&theCurrentTagType == dateTagType)) {
			//add the new char to the story  tag
			theAnchorObject.innerHTML = currenttext;
			theCurrentLength = theStoryDate.length;
			myTimeout = theCharacterTimeout;
		}
		else {
			//written the last char of this tag
			theCurrentLength = 0;
			myTimeout = theStoryTimeout;
			theAnchorObject.innerHTML = currenttext;
			if(theCurrentTagType == dateTagType) {
				myTimeout = theCharacterTimeout;
				theCurrentTagType = storyTagType;
				theCurrentLength = 0;
			}
			else if(theCurrentTagType == storyTagType) {
				//we have just written the last char of a story tag
				theCurrentTagType = unknownTagType;
				theArticleFadeInit=true;
				theArticleFadeCompleted=false;
			}
		}

	}

	//rerun the thread after suitable delay
	setTimeout("runTheTicker()", myTimeout);
}


function prePopulateOutputTable() {
	//you may want to format this table, or the dynamic aspect this this app could expand the table height
	ret = '<table border="0" cellspacing="0" cellpadding="2">';
	for(var i=0;i<theSummaries.length;i++) {
		if(i >= theMaxRows)
		{
			break;
		}
		ret += '<tr>';
			ret += '<td style="display: none;" id="tickerDateTD'+i+'">';
			ret += '&nbsp;';
			ret += '</td>';
			ret += '<td valign="top" name="tickerAnchorTD" id="tickerAnchorTD'+i+'">';
			ret += '&nbsp;';
			ret += '</td>';
		ret += '</tr>';
	}
	theTickerDiv.innerHTML = ret + '</table>';
}

function resetOutputTable() {
	for(var i=0;i<theTDTagIds.length;i++) {
		myelement = document.getElementById(theTDTagIds[i]);
		myelement.innerHTML = '&nbsp;';
	}
}



function beginAnchorTag(index) {
	return '<a name="tickerAnchor" id="tickerAnchor'+index+'" class="tickl" target="_top">';
}

function closeAnchorTag() {
	return '</a>';
}

function beginDateTag(index,theCurrentTagStorySnippet) {
	return '<a name="tickerDate" id="tickerDate'+index+'" class="ticklb" target="_top">';
}

function closeDateTag() {
	return '</a>';
}

function stepFade() {

	for(var i=0;i<theFadingTagIds.length;i++) {
		myelement = document.getElementById(theFadingTagIds[i]);

		if(theCurrentFadeStep<=theFadeMaxSteps) {
			myelement.style.color= getStepColorFadeOut(theCurrentFadeStep);
		}
		else if(theCurrentFadeStep==0) {
			//myelement.style.color="rgb("+theStartColor[0]+", "+theStartColor[1]+", "+theStartColor[2]+")"
		}
		else {
			//myelement.style.color="rgb("+theEndColor[0]+", "+theEndColor[1]+", "+theEndColor[2]+")";
			//we have reached the end of the fade, restart the ticker

			//uncomment this alert to see the completed output
			//alert(theTickerDiv.innerHTML);

			resetOutputTable();
			theCurrentFadeStep = 0;
			if(theCurrentStory==theItemCount-1)
			{
				theCurrentStory     = -1;
			}
			if(theTickerRowIndex==theMaxRows-1)
			{
				theTickerRowIndex = 0;
			}
			theCurrentLength    = 0;
			theFadingTagIds = new Array();
			theTDTagIds = new Array();
			theCurrentTagType = unknownTagType;
			return;
		}
		theCurrentFadeStep++;
	}
}

function stepFadeArticle(articleIndex) {

	var myFadingTagIds = new Array('tickerDate'+articleIndex,'tickerAnchor'+articleIndex);
	for(var i=0;i<myFadingTagIds.length;i++) {
		myelement = document.getElementById(myFadingTagIds[i]);
	//	alert(myFadingTagIds[i]);

		if(theCurrentFadeStep<=theFadeMaxSteps) {
			myelement.style.color= getStepColorFadeIn(theCurrentFadeStep);
		}
		else if(theCurrentFadeStep==0) {
			//myelement.style.color="rgb("+theStartColor[0]+", "+theStartColor[1]+", "+theStartColor[2]+")"
		}
		else {
			//myelement.style.color="rgb("+theEndColor[0]+", "+theEndColor[1]+", "+theEndColor[2]+")";
			//we have reached the end of the fade, restart the ticker

			//uncomment this alert to see the completed output
			//alert(theTickerDiv.innerHTML);

			if(theCurrentStory==theItemCount-1||theTickerRowIndex==theMaxRows-1)
			{
				//we have just faded in last story
				myTimeout = theLastStoryToFadeTimeout;

			}
			else
			{
				myTimeout = theStoryPause;
			}
			theCurrentFadeStep = 0;
			theCurrentTagType = unknownTagType;
			theArticleFadeCompleted = true;
			theArticleFadeInit = false;
			return;
		}
		theCurrentFadeStep++;
	}
}


function getStepColorFadeOut(step) {
	myarray = getStepArrayFadeOut(step);
	ret = ("rgb(" + myarray[0] + ", " + myarray[1] + ", " + myarray[2] + ")");
	return ret;
}
function getStepColorFadeIn(step) {
	myarray = getStepArrayFadeIn(step);
	ret = ("rgb(" + myarray[0] + ", " + myarray[1] + ", " + myarray[2] + ")");
	return ret;
}

function getStepArrayFadeOut(step) {
  var diff
  var newcolor=new Array(3);
  for(var i=0;i<3;i++) {
    diff = (theStartColor[i]-theEndColor[i]);
    if(diff > 0) {
      newcolor[i] = theStartColor[i]-(Math.round((diff/theFadeMaxSteps))*step);
    }
    else {
      newcolor[i] = theStartColor[i]+(Math.round((Math.abs(diff)/theFadeMaxSteps))*step);
    }
  }
  ret = new Array( newcolor[0], newcolor[1], newcolor[2]);
  return ret;
}

function getStepArrayFadeIn(step) {
  var diff
  var newcolor=new Array(3);
  for(var i=0;i<3;i++) {
    diff = (theEndColor[i]-theStartColor[i]);
    if(diff > 0) {
      newcolor[i] = theEndColor[i]-(Math.round((diff/theFadeMaxSteps))*step);
    }
    else {
      newcolor[i] = theEndColor[i]+(Math.round((Math.abs(diff)/theFadeMaxSteps))*step);
    }
  }
  ret = new Array( newcolor[0], newcolor[1], newcolor[2]);
  return ret;
}