
var currentString = '';
var currentIndex = 0;
var currentCharIndex = 0;
var tickerCursor = 0;
var currentFlashCount = 0;
var currentFlashCursor = 0;

//tickerCursor:
//    0 = BLANK
//    1 = UNDERSCODE
//    2 = HYPHEN
//    3 = CHARACTER

var tickerContainerElement;
var tickerLinkElement;

function initialiseTicker() {

    tickerContainerElement = document.getElementById(tickerContainer);
    clearTickerContainer();
    addTickerLink();

    processTickerItem();
}

function clearTickerContainer() {

    while (tickerContainerElement.firstChild) { tickerContainerElement.removeChild(tickerContainerElement.firstChild); }

}

function addTickerLink() {

    if (typeof(tickerContainerElement) == 'undefined') 
        return;

    tickerLinkElement = document.createElement('A');
    tickerContainerElement.appendChild(tickerLinkElement);
}

function setLinkHref(link) {
    tickerLinkElement.setAttribute('href', link);
}
function setLinkText(text) {
    tickerLinkElement.innerHTML = text;
}

function rotateTickerCursor() {
    tickerCursor++;
    if (tickerCursor > 3) tickerCursor = 0;    
}
function rotateFlashCursor() {
    currentFlashCursor++;
    if (currentFlashCursor > 1) currentFlashCursor = 0;
}

function rotateTickerItem() {
    currentIndex++;
    if (currentIndex >= tickerItems.length) currentIndex = 0;
}

function processTickerItem() {
    
    setLinkHref(tickerLinks[currentIndex]);
    
    if (typeof(tickerCursorFlashCount) != 'undefined' && tickerCursorFlashCount > 0) {
        // they want the cursor to flash before starting
        currentFlashCount = 0;
        currentFlashCursor = 0;
        processTickerCursorFlash();
    }
    else {
        // they want to go straight into the ticker
        processTickerStep();
    }
}

function processTickerCursorFlash() {
    
    if (currentFlashCursor == 0) {
        setLinkText('');
        currentFlashCount ++;
    }
    else if (currentFlashCursor == 1) {
        setLinkText('_');
    }
    
    rotateFlashCursor();
    
    if (currentFlashCount > tickerCursorFlashCount) {
        window.setTimeout(processTickerStep, tickerCursorFlashSpeed);
    }
    else {
        window.setTimeout(processTickerCursorFlash, tickerCursorFlashSpeed);
    }
    
    
}

function processTickerStep() {
    
    // add the relevant character to the current string
    var outputString = currentString;
    
    if (tickerCursor == 1) {
        outputString += '_';
    }
    else if (tickerCursor == 2) {
        outputString += '-';
    }
    else if (tickerCursor == 3) {
        outputString += tickerItems[currentIndex].substring(currentCharIndex, currentCharIndex + 1);
        currentString = outputString
        currentCharIndex++;
    }
    
    // move the cursor round
    rotateTickerCursor();
    
    setLinkText(outputString);
    
    if (currentCharIndex >= tickerItems[currentIndex].length) {
        window.setTimeout(startFadingTickerLink, tickerPause);
    }
    else {
        window.setTimeout(processTickerStep, tickerSpeed);
    }
}

function startFadingTickerLink() {
    
    setOpacity(tickerContainerElement, 100);    
    fadeTickerLink(100);
    
}

function fadeTickerLink(currentOpacity) {

    currentOpacity -= 5;

    //alert(currentOpacity);
    
    if (currentOpacity < 0) {
        // faded completely so move to next
        moveToNextTickerItem();
    }
    else {
        setOpacity(tickerContainerElement, currentOpacity);
        window.setTimeout(function() {fadeTickerLink(currentOpacity)}, tickerFadeSpeed);
    }

}

function moveToNextTickerItem() {
    
    // reset the current string, cursor state and character index
    currentString = '';
    tickerCursor = 0;
    currentCharIndex = 0;
    
    // increment the current item index
    rotateTickerItem();
    
    // blank out the link text
    setLinkText('');
    
    // reset the opacity
    setOpacity(tickerContainerElement, 100);
    
    // start again
    processTickerItem();
    
}

addToOnload(initialiseTicker);
