var currentFact;
var factArray;

function getFactText() {
    var thisFact = factArray[currentFact];
    thisFact = thisFact.replace(/\\"/, '"');
    return thisFact;
}

var tickerTimer = 0;
function nextFact() {
    clearTimeout(tickerTimer);
    currentFact == factArray.length - 1 ? currentFact = 0 : currentFact ++ ;
    $('ticker').innerHTML = '<strong>' + getFactText(factArray, currentFact) + '</strong>';
    $('tickerStatus').innerHTML = (currentFact + 1) + ' '+snippet('of')+' ' + factArray.length;
    tickerTimer = setTimeout('nextFact()', 30*1000); // show the next one every x seconds
}

function previousFact() {
    currentFact == 0 ? currentFact = factArray.length - 1 : currentFact -- ;
    $('ticker').innerHTML = '<strong>' + getFactText() + '</strong>';
    $('tickerStatus').innerHTML = (currentFact + 1) + ' '+snippet('of')+' ' + factArray.length;
}

function selectFactCat(factCategory) {
    factArray = tickersArray[factCategory]; // change the current array
    currentFact = Math.floor(Math.random() * factArray.length); // select a random fact.
    $('ticker').innerHTML = '<strong>' + getFactText() + '</strong>'; // 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();
        });
    }

});
