// BEGIN general config parameters   
   var webfeedHeading = "Occasio Consulting GmbH";
   var shouldDisplayHeading = false;
   var shouldDisplayTime    = false;
   var erfDisplaySource     = true;
   var openNewWindow        = false;
   
   // Delay after a single character has been typed (milliseconds)
   //
   var strokeDelay = 50;

   // Delay after the whole content of an article was displayed (milliseconds)
   //
   var linefeedDelay = 3000;

// END   general config parameters
//
//
// BEGIN news items separarators
//  ________ Article ________
// /                          \  
// Headline Source Access Time
//^        ^      ^      ^
//|        |      |      |
//|        |      |      timeSeparator
//|        |      accessSeparator
//|        sourceSeparator
//headlineSeparator
//
var headlineSeparator="&nbsp;";
var sourceSeparator="&nbsp;&nbsp;";
var accessSeparator="&nbsp;";
var timeSeparator = "&nbsp;";
//END news items separarators

var article = new Array();

//Defines caracters indicating the cursor position.
var cursors = new Array("-","_","_");

//Returns the cursor for current position.
function updateCursor(nCharsTyped, charsInThisArticle) 
{
    if (nCharsTyped == charsInThisArticle)
        return ""; // no cursor at the end
        
    if (cursors != null && cursors.length > 0)
        return cursors [ nCharsTyped % cursors.length ];
        
    return ""; //no cursor defined    
}

// Stores article items as pairs: (content, URL).
// If the URL is not null, a link is actualized and displyed (for headline, source or access).
// Otherwise that entry is displyed as text (the harvest time).
//
var articleItems = new Array(4);

// Updates the articleItems array to contain information regarding the current article.
//
function updateArticleItems(theArticle)
{
 articleItems[0] = new Array(theArticle.headline_text + "", theArticle.url);
 articleItems[1] = new Array(theArticle.source, theArticle.document_url);
 articleItems[2] = new Array(theArticle.access_status, theArticle.access_registration);

 time = new Date(theArticle.harvest_time);
 time.setHours(time.getHours() - (time.getTimezoneOffset() / 60 ));
 articleItems[3] = new Array(time.toString(), null);
}

// Endings added to the ticker id to identify the HTML tags used.
//
var itemEndings = new Array("_headline", "_source", "_access", "_time", "_access_spacing");

// Creates a TickerTypewriter object and initializes its attributes.
//
function TickerTypewriter(elementId, 
    headlineSeparator, sourceSeparator, accessSeparator, timeSeparator,
    strokeDelay, linefeedDelay)
{
    // Index of the article being typed
    this.iCurrentArticle = -1;

    // Index of the item in the article that is being typed
    this.iCurrentItem = -1;

    // Ticker id    
    this.elementId     = elementId;

    // Delay after a single character has been typed (milliseconds)
    this.strokeDelay   = strokeDelay;

    // Delay after the whole content of the article was displayed (milliseconds)
    this.linefeedDelay = linefeedDelay;

    // Number of characters from the current item that had been typed
    this.nCharsTyped = 0;

    // Set the separators to use
    this.headlineSeparator = headlineSeparator;
    this.sourceSeparator = sourceSeparator;
    this.accessSeparator =  accessSeparator;
    this.timeSeparator = timeSeparator;

    // Creates the HTML tags that will be used by this ticker
    this.createTicker();

    // Indicates the active tag
    this.activeTag = document.getElementById(this.elementId+itemEndings[0]);//the link

    // The content that will be displayed by this ticker
    this.theContent = "";

    // The URL for the content
    this.theLink="#";
}

// Creates the HTML tags that will be used by this ticker.
//
TickerTypewriter.prototype.createTicker = function()
{
    // Set the target for each link
    //
    var theTarget="_self";

    if (openNewWindow == true)
    {
     theTarget="_blank";
    }

    document.write(this.headlineSeparator);
    document.write("<a class='headlinetype' id='" + this.elementId + itemEndings[0] + "' href='#' target="+theTarget+"></a>");
    if (erfDisplaySource)
        document.write(this.sourceSeparator);
    document.write("<a class='sourcetype' id='" + this.elementId + itemEndings[1] + "' href='#' target="+theTarget+"></a>");
    document.write("<span id='"+ this.elementId + itemEndings[4]+"'></span>");
    document.write("<a class='accesstype' id='" + this.elementId + itemEndings[2] + "' href='#' target="+theTarget+"></a>");
    if (shouldDisplayTime)
        document.write(this.timeSeparator);
    document.write("<span class='timetype' id='" + this.elementId + itemEndings[3] + "' target="+theTarget+"> </span>");
}

// Moves to the next entry in the articleItems array or
// to the first item of the next article.
//
TickerTypewriter.prototype.nextEntry = function()
{
    this.iCurrentItem = (this.iCurrentItem + 1) % 4;
    
    // Move to the next article if no more items are available for the current article.
    //
    if (this.iCurrentItem == 0) 
    {
        this.iCurrentArticle = (this.iCurrentArticle + 1) % article.length;
        updateArticleItems(article[this.iCurrentArticle]);
       
        //clear the content of all the HTML tags used by this ticker.
        for(var i=0;i<itemEndings.length;i++)
            document.getElementById(this.elementId+itemEndings[i]).innerHTML = "";
    }
}

// Selects the next item that will be displyed and updates the ticker attributes accordingly.
// The selection is based on the values of the global config parameters.
//
TickerTypewriter.prototype.nextItem = function()
{
    this.nextEntry();
    
    if (this.iCurrentItem == 1 && !erfDisplaySource)
      this.nextEntry();
    
    theArticle = article[this.iCurrentArticle];  
    if (this.iCurrentItem == 2 && theArticle.access_status != "sub" && theArticle.access_status != "reg")
        this.nextEntry();
     else
        document.getElementById(this.elementId+itemEndings[4]).innerHTML = this.accessSeparator;
    
    if (this.iCurrentItem == 3 && !shouldDisplayTime)
      this.nextEntry();
    
    // Update the content and the link
    //
    this.theContent = articleItems[this.iCurrentItem][0];
    this.theLink    = articleItems[this.iCurrentItem][1];
    
    // Update the active tag  
    //
    this.activeTag = document.getElementById(this.elementId+itemEndings[this.iCurrentItem]);

    // Update the link of the active tag, if necessary
    //
    if (this.theLink != null)
        this.activeTag.href = this.theLink;
}

// Computes the index in the articleItems array
// of the last item that will be displyed for the current article
//
TickerTypewriter.prototype.lastItem = function()
{
   var lastItem = 0;
   
   if ( erfDisplaySource )
    lastItem = 1;
   
   theArticle = article[this.iCurrentArticle];  
   if (theArticle.access_status == "sub" || theArticle.access_status == "reg")
    lastItem = 2;
    
   if (shouldDisplayTime)
     lastItem = 3;
     
   return lastItem;
}

// The main function that controls the ticker.
//
TickerTypewriter.prototype.run = function()
{
    if (this.nCharsTyped == 0)
    {
        // Move to next item
        //
        this.nextItem();
    }
    
    // Change the display
    //
    this.activeTag.innerHTML = this.theContent.substring(0, this.nCharsTyped);
    this.activeTag.innerHTML += updateCursor(this.nCharsTyped, this.theContent.length);
    
    // Compute the delay
    //
    var nextDelay = this.strokeDelay;
    if(this.nCharsTyped != this.theContent.length) {
        this.nCharsTyped++;
    }
    else {
        this.nCharsTyped = 0;
        if (this.iCurrentItem == this.lastItem())
            nextDelay = this.linefeedDelay;
    }
    
    setTimeout(this.elementId+".run()", nextDelay);
}

function createNewsfeedTypewrite() {
    if (article == null)
        article = new Array();
    
    // Check to make sure we have the news feed file ok
    //    
    if (article.length == 0)
    {
     document.writeln("<center>Please reload this page to view the News/Information</center>");
    }
    else
    {
      var articleERF = new sa(" ", "http://www.occasio.de/", "Occasio Consulting GmbH - T: 07175 998 91-0", "", "text", " ", " ", "http://www.occasio.de/", article[0].harvest_time,"","");
      article[article.length] = articleERF;
    
      document.writeln("<div class='containertype' width='100%'>");
     
      // Controls the heading display
      //
      if (shouldDisplayHeading)
      {
       document.writeln("<span class='headingtype'>" + webfeedHeading + "</span>");
      }
    
      // Instantiates a ticker object
      //
      theTickerTypewriter = new TickerTypewriter("theTickerTypewriter", 
        headlineSeparator, sourceSeparator, accessSeparator, timeSeparator, 
        strokeDelay, linefeedDelay);
    
      // Starts the ticker
      //
      theTickerTypewriter.run();
     
      document.writeln("</div>");
    }
}