﻿var ticker = null;
var tIntervalID = null;
var NT_MAXCHARS = 45;

function Ticker() {
    this.ms = 0;
    this.increment = 10;
    this.interval = 3000;
    this.auto = true;
    this.pause = false;
    this.activeIndex = 0;
    this.transition = 1000;
    this.busy = false;
    this.count = 1;
    this.items = 0;
    this.activeItem = 0;
    this.activeRow = 0;
}

function initTicker() {
    var c1 = 0;
    var c2 = 0;
    var c3 = 0;

    ticker = new Ticker();
    tIntervalID = setInterval('doTickerTimer()', ticker.increment);
    
    $$('li.ticker-text1').each(function(item) {
        if (c1 == 0)
            item.set('opacity', 1)
        else
            item.set('opacity', 0)

        c1++;
    });

    $$('li.ticker-text2').each(function(item) {
        if (c2 == 0)
            item.set('opacity', 1)
        else
            item.set('opacity', 0)

        c2++;
    });

    $$('li.ticker-text3').each(function(item) {
        if (c3 == 0)
            item.set('opacity', 1)
        else
            item.set('opacity', 0)

        c3++;
    });

    $('ticker').addEvents({
        mouseenter: function() {
            ticker.pause = true;
        },
        mouseleave: function() {
            ticker.pause = false;
        }
    });
    
    eE($('ticker'));
}

function doTickerTimer() {
    var t = ticker;
    var index;

    if (t.activeIndex == t.count)
        index = 0;
    else
        index = t.activeIndex + 1;

    if (t.auto) {
        if (t.ms == t.interval) {
            xFadeTicker(index);
            t.ms = 0;
        }
        else {
            if (!t.pause)
                t.ms += t.increment;
        }
    }
    else
        clearInterval(tIntervalID);
}

function xFadeTicker(index) {
    var t = ticker;
    var li1;
    var li2;
    var li3;
    var li4;
    var li5;
    var li6;

    if (index == t.activeIndex || t.busy)
        return;
    else
        t.busy = true;

    li1 = $('t1_' + t.activeIndex);
    li2 = $('t1_' + index);
    li3 = $('t2_' + t.activeIndex);
    li4 = $('t2_' + index);
    li5 = $('t3_' + t.activeIndex);
    li6 = $('t3_' + index);

    var fx1 = new Fx.Morph(li1, {
        duration: t.transition
    });
    fx1.start({
        'opacity': [1, 0]
    });

    var fx2 = new Fx.Morph(li2, {
        duration: t.transition
    });
    fx2.start({
        'opacity': [0, 1]
    }).chain(function() { t.busy = false; loadItems(t.activeRow); });

    var fx3 = new Fx.Morph(li3, {
        duration: t.transition
    });
    fx3.start({
        'opacity': [1, 0]
    });

    var fx4 = new Fx.Morph(li4, {
        duration: t.transition
    });
    fx4.start({
        'opacity': [0, 1]
    })

    var fx5 = new Fx.Morph(li5, {
        duration: t.transition
    });
    fx5.start({
        'opacity': [1, 0]
    });

    var fx6 = new Fx.Morph(li6, {
        duration: t.transition
    });
    fx6.start({
        'opacity': [0, 1]
    })

    t.activeIndex = index;
}


//function getTickerHeadlines(pubID) {
function getTickerHeadlines(expDate, pubKey) {
    //var request = new Request.JSON({ url: 'tml/ajax/GetTickerHeadlines.aspx?pubid=' + pubID,
    var request = new Request.JSON({ url: 'tml/ajax/GetTickerHeadlines.aspx?expdate=' + expDate + '&pubkey=' + pubKey,
        onComplete: function(json) {
            NEWSTICKER = json;
            loadNewsTicker();
        }
    }).send();
}

function loadNewsTicker() {
    var nt = NEWSTICKER.newsticker;

    initTicker();

    ticker.items = nt.length;

    loadItems(0);
    loadItems(1);
}

function loadItems(index) {
    var nt = NEWSTICKER.newsticker;
    var title;
    var url;
    var li;
    var a;
    var ai;

    if (nt.length == 0)
        return;
    
    for (var i=1; i<=3; i++) {
        li = $('t' + i + '_' + index);
        li.innerHTML = '';

        ai = ticker.activeItem;
        text = truncate(nt[ai].title);
        url = nt[ai].url;

        title = document.createTextNode(text);
        
        if (url != '') {
            a = document.createElement('a');
            a.href = url;

            a.appendChild(title);
            li.appendChild(a);
        }
        else
            li.appendChild(title);

        if (ai == ticker.items-1)
            ticker.activeItem = 0;
        else
            ticker.activeItem++;
    }


    if (index == 0)
        ticker.activeRow++;
    else
        ticker.activeRow = 0;
}

function truncate(text) {
    var textArray = text.split(' ');
    var textChar;
    var lastSpace;
    var len = 0;

    for (var s = 0; s < textArray.length; s++) {
        len += textArray[s].length;    
    }

    len += textArray.length - 1;

    //alert(text+"::"+len);

    //if (text.length > NT_MAXCHARS)
    if (len > NT_MAXCHARS)
    {
        for (var i = 0; i < NT_MAXCHARS; i++) {
            textChar = text.substring(i, i + 1);

            if (textChar == ' ')
                lastSpace = i;
        }

        text = text.substring(0, lastSpace) + '...';
    }

    return text;
}