/*****************************************************************************
 *                                                                           *
 * Program lastnews.js                                                       *
 *                                                                           *
 * Allow changing text lins and anchors in a <IFRAME> tag.                   *
 * Based on the functions at the BBC webpage.                                *
 *                                                                           *
 * Internet Explorer: WORKS.                                                 *
 * Fireforx:          WORKS.                                                 *
 * Mozilla:           WORKS.                                                 *
 * Konqueror:         WORKS.                                                 *
 * Opera:             WORKS.                                                 *
 *                                                                           *
 * Author: MAd        Email: mad@freechess.org                               *
 * Created:           29 december 2005                                       *
 * Last Modified:     16 january  2007                                       *
 * Copyright:         Free Internet Chess Server 2005                        *
 *                                                                           *
 *****************************************************************************/
var theCharacterTimeout = 50;
var theStoryTimeout     = 5000;
var theWidgetOne        = "_";
var theWidgetTwo        = "-";
var theWidgetNone       = "";
var theLeadString       = "Latest news:&nbsp;";

var theSummaries = new Array();
var theSiteLinks = new Array();

theSummaries[0] = "Carlsen and Ivanchuk win Amber.";
theSiteLinks[0] = "/Events/Relay/2010/Amber2010.html";

theSummaries[1] = "Topalov wins Linares 2010.";
theSiteLinks[1] = "/Events/Relay/2010/Linares2010.html";

theSummaries[2] = "Le Quam wins Aeroflot and qualifies for Dortmund.";
theSiteLinks[2] = "/Events/Relay/2010/AeroflotOpen2010A1.html";


var theItemCount = theSummaries.length;

StartTicker();

/*****************************************************************************
 *                                                                           *
 * Function StartTicker:                                                     *
 *                                                                           *
 * Start the ticker or write nothing on the web.                             *
 *                                                                           *
 *****************************************************************************/
function StartTicker(){
  theCurrentStory     = -1;
  theCurrentLength    = 0;

  if (document.getElementById){
     theAnchorObject = document.getElementById("newsAnchor");
     RunTheTicker();   	
  }
  else {
    document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
    return true;
  }
}
/*****************************************************************************
 *                                                                           *
 * Function RunTheTicker:                                                    *
 *                                                                           *
 * Update the anchor and the text after a suitable time. Write the text one  *
 * character at the time. While the text is being written add a trailing     *
 * "-" or "_" as a coll effect.                                              *
 *                                                                           *
 *****************************************************************************/
function RunTheTicker(){
  var myTimeout;  

  if(theCurrentLength == 0){
    theCurrentStory++;
    theCurrentStory      = theCurrentStory % theItemCount;
    theStorySummary      = theSummaries[theCurrentStory].replace(/&quot;/g,'"');
    theTargetLink        = theSiteLinks[theCurrentStory];
    theAnchorObject.href = theTargetLink;
    thePrefix            = "<span class=\"lead\">" + theLeadString + "</span>";
  }

  theAnchorObject.innerHTML = thePrefix + 
           theStorySummary.substring(0, theCurrentLength) + WhatWidget();

  if(theCurrentLength != theStorySummary.length){
    theCurrentLength++;
    myTimeout = theCharacterTimeout;
  }
  else{
    theCurrentLength = 0;
    myTimeout = theStoryTimeout;
  }

  setTimeout("RunTheTicker()", myTimeout);
}
/*****************************************************************************
 *                                                                           *
 * Function WhatWidget:                                                      *
 *                                                                           *
 * Select whether to trail the scrolling text with a "-" or a "_".           *
 *                                                                           *
 *****************************************************************************/
function WhatWidget(){
  if(theCurrentLength == theStorySummary.length){
    return theWidgetNone;
  }

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


