var currentFact;
var factArray;

function getFactText() {
  return factArray[currentFact];
}

function getTextArray() {
  return factTextArray[currentFact];
}

var tickerTimer = 0;
function nextFact() {
  var currentText;
  clearTimeout(tickerTimer);
  currentFact == factArray.length - 1 ? currentFact = 0 : currentFact ++ ;
  $('ticker').innerHTML = '' + getFactText(factArray, currentFact) + '';
  $('tickerStatus').innerHTML = (currentFact + 1) + ' '+snippet('of')+' ' + factArray.length;
  tickerTimer = setTimeout('nextFact()', 30*1000); // show the next one every x seconds
  if ((getTextArray(factArray, currentFact)).length > 65) {
    currentText = (getTextArray(factArray, currentFact)).substring(0,65)+' ...';
  } else {
    currentText = getTextArray(factArray, currentFact);
  }
  dcsMultiTrack('WT.cusFactTicker', currentText);
}

function previousFact() {
  var currentText;
  currentFact == 0 ? currentFact = factArray.length - 1 : currentFact -- ;
  $('ticker').innerHTML = '' + getFactText() + '';
  $('tickerStatus').innerHTML = (currentFact + 1) + ' '+snippet('of')+' ' + factArray.length;
  if ((getTextArray()).length > 65) {
    currentText = (getTextArray()).substring(0,65)+' ...';
  } else {
    currentText = getTextArray();
  }
  dcsMultiTrack('WT.cusFactTicker', currentText);
}

function selectFactCat(factCategory) {
  factArray = tickersArray[factCategory]; // change the current array
  currentFact = Math.floor(Math.random() * factArray.length); // select a random fact.
  $('ticker').innerHTML = '' + getFactText() + ''; // update the current fact
  $('tickerStatus').innerHTML = (currentFact + 1) + ' '+snippet('of')+' ' + factArray.length; // update the counter
}

// show the div with the list of tickers
document.observe('dom:loaded', function() {

  if ($('tickers') != null) {
    $('tickers').hide();
    tickerTimer = setTimeout('nextFact()', 30*1000); // show the next one every x seconds
  }
  
  if ($('ticker_container') != null) {
    $('ticker_container').observe('mouseover', function(event){
      $('tickerSelect').removeClassName('off');
      $('tickerSelect').addClassName('on');
      $('tickers').show();
    });
  }
  
  if ($('ticker_container') != null) {
    $('ticker_container').observe('mouseout', function(event){
      $('tickerSelect').removeClassName('on');
      $('tickerSelect').addClassName('off');
      $('tickers').hide();
    });
  }
  
});
