﻿function getColor(color)
{
  var r = (color - (color % 65536)) / 65536;
  var g = ((color % 65536) - (color % 256)) / 256;
  var b = (color % 256);
  return "#" + hex(r) + hex(g) + hex(b);
}
function hex(value)
{
  return hexone((value - (value % 16)) / 16) + hexone(value % 16);
}
function hexone(value)
{
  if (value == 15)
    return "F";
  if (value == 14)
    return "E";
  if (value == 13)
    return "D";
  if (value == 12)
    return "C";
  if (value == 11)
    return "B";
  if (value == 10)
    return "A";
  return "" + value;
}
// Ticker startup
function startTicker()
{
	// Define run time values
	theCurrentStory     = -1;
	theCurrentLength    = -1;
	// Locate base objects
	if (document.getElementById)
  {	
    theAnchorObject     = document.getElementById("tickerAnchor");
    theTitleObject      = document.getElementById("tickertitle");
    runTheTicker();   	
  }
}
// Ticker main run loop
var titleTick = false;
var theOldTitle = "";
var theCurrentTitle = "";

function runTheTicker()
{
	var myTimeout;
	// Go for the next story data block
	if(theFadeOut == theFadeTo)
	{
    theAnchorObject.style.color = getColor(theFadeFrom);
	  if(theCurrentLength == -1)
	  {
      theCurrentStory++;
      theCurrentStory      = theCurrentStory % theItemCount;
      if (theCurrentTitle != theTitle[theCurrentStory])
        titleTick = true;
      theOldTitle          = theCurrentTitle;
      theCurrentTitle      = theTitle[theCurrentStory];
      theStorySummary      = theSummaries[theCurrentStory].replace(/&quot;/g,'"');		
      theTargetLink        = theSiteLinks[theCurrentStory];
      theAnchorObject.href = theTargetLink;
      theAnchorObject.innerHTML = "";
      theCurrentLength = 0;
    }
    // Stuff the current ticker text into the anchor
    if (titleTick)
      {
      // Changing the title
      if (theCurrentLength < theOldTitle.length)
        // Taking out title
        theTitleObject.innerHTML = theOldTitle.substring(0,theOldTitle.length-theCurrentLength);
      else
        // Putting in the next title
        theTitleObject.innerHTML = theCurrentTitle.substring(0,theCurrentLength-theOldTitle.length); // + whatWidget(theOldTitle.length+theCurrentTitle.length);
      
      if(theCurrentLength == theOldTitle.length)
        theTitleObject.className = "title-"+theType[theCurrentStory];
      if(theCurrentLength != theOldTitle.length+theCurrentTitle.length)
	      {
		      theCurrentLength++;
		      if (theCurrentLength < theOldTitle.length)
		        myTimeout = theWipeOutTimeout;
		      else
		        myTimeout = theCharacterTimeout;
	      }
	      else
	      {
		      theCurrentLength = 0;
		      titleTick = false;
	      }
      }
    else
      {
        // Ticking the story text
        theAnchorObject.innerHTML = theStorySummary.substring(0,theCurrentLength) + whatWidget(theStorySummary.length);
        // Modify the length for the substring and define the timer
        if(theCurrentLength != theStorySummary.length)
	      {
		      theCurrentLength++;
		      myTimeout = theCharacterTimeout;
	      }
	      else
	      {
		      theCurrentLength = -1;
		      myTimeout = theStoryTimeout;
		      theFadeOut = theFadeFrom;
	      }
	    }
  }
  else
  {
    theFadeOut = theFadeOut + theFadeStep;
    if(theFadeDirection = 1)
    {
      if(theFadeOut > theFadeTo)
      {
        theFadeOut = theFadeTo;
      }
    }
    else
    {
      if(theFadeOut < theFadeTo)
      {
        theFadeOut = theFadeTo;
      }
    }
    theAnchorObject.style.color = getColor(theFadeOut);
    myTimeout = theFadeOutTimeout;
  }
	// Call up the next cycle of the ticker
	setTimeout("runTheTicker()", myTimeout);
}
// Widget generator
function whatWidget(length)
{
	if(theCurrentLength == length)
	{
		return theWidgetNone;
	}

	if((theCurrentLength % 2) == 1)
	{
		return theWidgetOne;
	}
	else
	{
		return theWidgetTwo;
	}
}
