﻿
var intTickSpeed = 5000;
var intTickPos = 0;
var tickLocked = true;
var fadeTimerID;
var autoTimerID = 0;
var pauseTimerID;
var intTypeSpeed = 64;
var intCurrentPos = 0;
var currentText = '';
var actualText = '';
var currentLink = '';
var strText = '';
var isFirstPass = true;
var blnTickerTicking = false;
var isInFirstTimeout = true;
var characterLimit = 56;
var characterCount = 0;

function initButtons() {
    $("#back").click(function() {
        if (tickLocked == false) {
            if (intTickPos == 0) {
                intTickPos = arrNewsItems.length - 1;
            } else {
                intTickPos--;
            }
            setArticle(intTickPos);
        }
    });
    $("#next").click(function() {
        nextArticle();
    });
    $("#playpause").click(function() {
        if (!tickLocked) {
            if (blnTickerTicking) {
                pauseTicker();
            } else {
                $("#playpause").removeClass("TickPlay");
                $("#playpause").addClass("TickPause");
                blnTickerTicking = true;
                clearTimeout(autoTimerID);
                setArticle(intTickPos);
                playTicker();
            }
        }
    });
    $("#tickLinkId").hover(function() {
        if (blnTickerTicking && !tickLocked) {
            clearTimeout(autoTimerID);
            $("#playpause").removeClass("TickPause");
            $("#playpause").addClass("TickPlay");
        }
    }, function() {
        if (blnTickerTicking && !tickLocked) {
            clearTimeout(autoTimerID);
            autoTimerID = self.setTimeout("playTicker()", intTickSpeed);
            $("#playpause").removeClass("TickPlay");
            $("#playpause").addClass("TickPause");
        }
    });
    clearTimeout(autoTimerID);
    setArticle(intTickPos);
    playTicker();
}
function nextArticle() {
    if (tickLocked == false) {
        if (intTickPos == arrNewsItems.length - 1) {
            intTickPos = 0;
        } else {
            intTickPos++;
        }
        setArticle(intTickPos);
    }
}
function typeText() {
    strText += currentText.charAt(intCurrentPos);
    if (strText.length > characterLimit) {
        var iLen = String(strText).length;
        strText = String(strText).substring(iLen, 1);
        if (intCurrentPos <= currentText.length) {
            clearInterval(typeInterval);
            typeInterval = setInterval("typeText()", intTypeSpeed * 8);
            clearTimeout(autoTimerID);
            autoTimerID = setTimeout("playTicker()", intTickSpeed);
        }
    }
    setSpan(strText, currentLink);
    if (intCurrentPos < currentText.length) {
        intCurrentPos++;
    } else {
        clearInterval(typeInterval);
        $("#cursor").addClass("hidden");
        tickLocked = false;
    }
}
function setSpan(strText, strLink) {
    $("#tickLinkId").attr('href', strLink);
    $("#tickLinkId").attr('target', '_top');
    $("#tickLinkId").html(strText);
}
function setArticle(intPos) {
    if (arrNewsItems[intPos] != null) {
        tickLocked = true;
        intCurrentPos = 0;
        strText = '';
        setSpan('', '#');
        $("#cursor").removeClass("hidden");
        currentText = $("#" + arrNewsItems[intPos][0]).html();
        currentLink = arrNewsItems[intPos][1];
        typeInterval = setInterval("typeText()", intTypeSpeed);
    }
}
function pauseTicker() {
    if (tickLocked) { pauseTimerID = setTimeout("pauseTicker()", 500); }
    else {
        clearTimeout(pauseTimerID);
        clearTimeout(autoTimerID);
        blnTickerTicking = false;
        $("#playpause").removeClass("TickPause");
        $("#playpause").addClass("TickPlay");
        var date = new Date();
        date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
        $.cookie("TickerState", "false", { path: '/', expires: date });
    }
}

function playTicker() {
    if (isInFirstTimeout == false || $.cookie("TickerState") == "true") {
        if (autoTimerID != 0) {
            nextArticle();
        }
        autoTimerID = setTimeout("playTicker()", intTickSpeed);
    } else {
        if (autoTimerID != 0) {
            nextArticle();
        }
        isInFirstTimeout = false;
        intTickPos = intTickPos + 1;
        pauseTicker();
    }
    var date = new Date();
    date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
    $.cookie("TickerState", "true", { path: '/', expires: date });
}


initButtons();