// Global variables
var dObj = document.getElementById('feed');
var pArray = dObj.getElementsByTagName("p");
var pArray2 = collectionToArray(dObj.getElementsByTagName("p"));
var w = 0;
var speed = 2;
var left = 0;
var timer;
var repeatPs = 0;
var tempID = 0;
var num = 0;

// Set Width of "feed" div
for (i=0; i<pArray.length; i++)
    w += pArray[i].offsetWidth;
dObj.style.width = w+"px";

// Functions
function scrollNews()
{
    var temp;

    if (repeatPs == 1)
    {
        for (k=0; k<pArray2.length; k++)
        {
            temp = document.createElement("p");
            tempID++;
            temp.id = "newP" + tempID;
            dObj.appendChild(temp);
            document.getElementById("newP"+tempID).innerHTML = pArray[k].innerHTML;
        }

        // Reset Width of "feed" div
        var w2 = 0;
        for (i=0; i<pArray.length; i++)
            w2 += pArray[i].offsetWidth;
        dObj.style.width = w2+"px";

        repeatPs = 0;
    }

    left -= speed;
    num += speed;
    dObj.style.left = left + "px";
    timer = setTimeout('scrollNews()',30);

    if (num * 2.5 > w)
    {
        num = 0;
        repeatPs = 1;
    }
}

function pauseScroll(state,e)
{
    if (!e) var e = window.event;

    // prevent event bubbling
    if (e.relatedTarget)
        var relTarg = e.relatedTarget
    else if (e.type == 'mouseout')
        var relTarg = e.toElement
    else
        var relTarg = e.fromElement;

    while (relTarg && relTarg != dObj)
        relTarg = relTarg.parentNode;

    if (relTarg == dObj)
        return;

    // perform action
    if (state == 1)
        clearTimeout(timer);
    else if (state == 2)
        scrollNews();
}

// Configure news ticker links
function configureLinks()
{
	var linkArr = dObj.getElementsByTagName("a");
	for (i=0; i<linkArr.length; i++)
	{
		linkArr[i].onclick = function()
		{
			window.open(this.href); 
			return false;
		};
	}
}

// Convert HTMLCollection object to Array object
function collectionToArray(col)
{
    var a = new Array();
    for (i=0; i<col.length; i++)
        a[a.length] = col[i];
    return a;
}

// Start scrolling onload
function startScroll()
{
    document.getElementById('loading').style.display = "none";
    configureLinks();
    setTimeout('scrollNews()',500);

    dObj.onmouseover = function(event)
    {
        pauseScroll(1,event);
    }
    dObj.onmouseout = function(event)
    {
        pauseScroll(2,event);
    }
}
