﻿
// (C) Copyright 2007, Stichting Tree of Life Centra
// Owner : Stichting Tree of Life Centra - http://www.treeoflifecentra.nl/
// Author: René Kleyn                    - http://www.servertje.nl/

// wordpopup.js. Cycles through certain html lists and displays the list elements one by one
//               The script assumes all list elements have style="visibility: hidden"

// Defines what to show
var WordLists = new Array();

// We're setting all elements to null, since the page is potentially still loading at this point
WordLists.push({ ulElm : null, liElms : null, id : 'dutchwords' });
WordLists.push({ ulElm : null, liElms : null, id : 'englishwords' });

var showInterval = 1000;

/****************************************************************\
* Don't change anything below unless you know what you're doing! *
\****************************************************************/

var listCount;
var wordCount;

var currentListIndex;
var currentWordIndex;

initWordPopup = function()
{
  indexWordLists();        // Complete our model with references to the now loaded DOM
  checkCompleteness();     // We must ensure all lists have an equal amount of words
  showLastWords();         // Show the language links first for the impatient
  startShowingWords();     // Gogogo
}

iterateWordLists = function(callBack)
{
  for (var i = 0; i < listCount; i++)
    callBack(WordLists[i]);
}

indexWordLists = function()
{
  listCount = WordLists.length;
 
  iterateWordLists(function(wl)
  {
    if (wl.ulElm == null)
      wl.ulElm = document.getElementById(wl.id);
      
    wl.liElms = wl.ulElm.getElementsByTagName('LI');
  });
}

checkCompleteness = function()
{
  var error = false;
  
  iterateWordLists(function(wl)
  {
    var currentCount = wl.liElms.length;
    if (wordCount === undefined)
      wordCount = currentCount;
    
    if (!error)
      error = (wordCount != currentCount);
  });
  
  if (error)
    alert('Not all lists have an equal amount of words.\nPlease report this bug to the web master.');
}

showLastWords = function()
{
  var lastWord = wordCount - 1;
  
  iterateWordLists(function(wl)
  {
    wl.liElms[lastWord].style.visibility = 'visible';
  });
}

startShowingWords = function()
{
  currentListIndex = 0;
  currentWordIndex = 0;
  
  setTimeout(showWord, showInterval);
}

showWord = function()
{
  WordLists[currentListIndex].liElms[currentWordIndex].style.visibility = 'visible';

  currentListIndex++;
  
  if (currentListIndex == listCount)
  {
    currentListIndex = 0;
    currentWordIndex++;
  }
      
  // The last items are already shown, so -1  
  if (currentWordIndex < wordCount - 1)
    setTimeout(showWord, showInterval);
}

addEvent(window, 'load', initWordPopup);
