//var texts = new Array
//(
//    " test 1", 
//    " What Experts Are Saying About Ontario's Wait Time Strategy",
//    " McGuinty Government Strengthening Cancer Treatment in Ontario", 
//    " test 4"
//);
//var links = new Array
//(
//    "http://www.yahoo.ca",
//    "http://www.pulse24.com", 
//    "", 
//    "http://www.dictionary.com"
//);

var bgcolor = "#DDDDDD"; // background color, must be valid browser hex color (not color names)
var fcolor = "#000000";  // foreground or font color
var steps = 15; // number of steps to fade
var show = 5000; // milliseconds to display message
var sleep = 100; // milliseconds to pause inbetween messages

var colors = new Array(steps);
getFadeColors(bgcolor,fcolor,colors);
var color = 0;
var tickerItem = 0;
var colorStep = 1;

function fade() 
{
    var ticker = document.getElementById("ticker");
    if (ticker)
    {
        ticker.style.color = colors[color];
        if (ticker.value != texts[tickerItem]) ticker.value = texts[tickerItem];
    }

    color += colorStep; 

    if (color >= colors.length-1) 
        colorStep = -1;

    if (color == 0) 
    {
        colorStep = 1;

        tickerItem += 1;
        if (tickerItem == texts.length) tickerItem = 0;
    }

    // subtle timing logic...
    setTimeout("fade()", (color == colors.length-2 && colorStep == -1) ? show : ((color == 15 && colorStep == 1) ? sleep : 50)); // sleep and show should be defined in user script, e.g.: var sleep=30; var show=500;
}

function openLink()
{
    var linkTarget = links[tickerItem];
    if (linkTarget != " ") window.open(linkTarget);
}

function showCursorAsPointer(ctl)
{
    if (links[tickerItem] != " ") ctl.style.cursor='pointer';
}

function showCursorAsDefault(ctl)
{
    if (links[tickerItem] != " ") ctl.style.cursor='default';
}

// note: Colors.length equals the number of steps to fade
function getFadeColors(ColorA, ColorB, Colors) 
{
    len = Colors.length; 

    // strip '#' signs if present 
    if (ColorA.charAt(0)=='#') ColorA = ColorA.substring(1);
    if (ColorB.charAt(0)=='#') ColorB = ColorB.substring(1);

    // substract rgb compents from hex string 
    var r = HexToInt(ColorA.substring(0,2));
    var g = HexToInt(ColorA.substring(2,4));
    var b = HexToInt(ColorA.substring(4,6));
    var r2 = HexToInt(ColorB.substring(0,2));
    var g2 = HexToInt(ColorB.substring(2,4));
    var b2 = HexToInt(ColorB.substring(4,6));

    // calculate size of step for each color component
    var rStep = Math.round((r2 - r) / len);
    var gStep = Math.round((g2 - g) / len);
    var bStep = Math.round((b2 - b) / len);

    // fill Colors array with fader colors
    for (i = 0; i < len-1; i++) 
    {
        Colors[i] = "#" + IntToHex(r) + IntToHex(g) + IntToHex(b);
        r += rStep;
        g += gStep;
        b += bStep;
    }
    Colors[len-1] = ColorB; // make sure we finish exactly at ColorB
}

// IntToHex: converts integers between 0-255 into a two digit hex string.
function IntToHex(n) 
{
    var result = n.toString(16);
    if (result.length==1) result = "0"+result;
    return result;
}

// HexToInt: converts two digit hex strings into integer.
function HexToInt(hex) 
{
    return parseInt(hex, 16);
}
