﻿var articleHttpRequest = null;
var articleIndex = 0;
var timer = null;

function SetArticleHttpRequest()
{
    if (typeof XMLHttpRequest != "undefined")
    {
        articleHttpRequest = new XMLHttpRequest();
    }
    else
    {
        articleHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    }
}

function GetNextArticle()
{
    articleHttpRequest.open("GET", "/_layouts/FRGetNextArticle.ashx?Action=next&ArticleIndex=" + articleIndex);
    articleHttpRequest.onreadystatechange = SetArticle;
    articleHttpRequest.send();
}

function GetPreviousArticle()
{
    articleHttpRequest.open("GET", "/_layouts/FRGetNextArticle.ashx?Action=previous&ArticleIndex=" + articleIndex);
    articleHttpRequest.onreadystatechange = SetArticle;
    articleHttpRequest.send();
}

function SetArticle()
{
    if (articleHttpRequest.readyState == 4)
    {
        var articleNode = articleHttpRequest.responseXML.documentElement;

        var spotlightTitle = document.getElementById("spotlightTitle");
        var spotlightImage = document.getElementById("spotlightImage");
        var spotlightText = document.getElementById("spotlightText");
        var moreAnchor = document.getElementById("moreAnchor");
        var spotlightImageAnchor = document.getElementById("spotlightImageAnchor");
        var spotlightArchiveAnchor = document.getElementById("archiveAnchor");        

        if (navigator.appName == "Microsoft Internet Explorer")
        {            
            spotlightTitle.innerText = articleNode.childNodes[0].text;
            spotlightImage.src = articleNode.childNodes[1].text;
            spotlightImage.alt = articleNode.childNodes[2].text;
            spotlightText.innerText = articleNode.childNodes[3].text;
            moreAnchor.href = articleNode.childNodes[4].text;
            spotlightImageAnchor.href = articleNode.childNodes[4].text;
            articleIndex = articleNode.childNodes[5].text;
            if (articleNode.childNodes[6].text == "Article")
            {
                spotlightArchiveAnchor.href = "/Pages/FRArticles.aspx";
                spotlightArchiveAnchor.innerText = "Article Archive";
            }
            else
            {
                spotlightArchiveAnchor.href = "/FRBlog";
                spotlightArchiveAnchor.innerText = "Blog Archive";
            }
        }
        else
        {
            spotlightTitle.innerHTML = articleNode.childNodes[0].textContent;
            spotlightImage.src = articleNode.childNodes[1].textContent;
            spotlightImage.alt = articleNode.childNodes[2].textContent;
            spotlightText.innerHTML = articleNode.childNodes[3].textContent;
            moreAnchor.href = articleNode.childNodes[4].textContent;
            spotlightImageAnchor.href = articleNode.childNodes[4].textContent;
            articleIndex = articleNode.childNodes[5].textContent;
            if (articleNode.childNodes[6].textContent == "Article")
            {
                spotlightArchiveAnchor.href = "/Pages/FRArticles.aspx";
                spotlightArchiveAnchor.innerHTML = "Article Archive";
            }
            else
            {
                spotlightArchiveAnchor.href = "/FRBlog";
                spotlightArchiveAnchor.innerHTML = "Blog Archive";
            }
        }
    }
}

function Play()
{
    GetNextArticle();
    timer = window.setInterval("GetNextArticle()", 5000);
    var playBT = document.getElementById("playImage");
    playBT.style.display = "none";
    var pauseBT = document.getElementById("pauseImage");
    pauseBT.style.display = "inline";
}

function Pause()
{
    window.clearInterval(timer);
    timer = null;
    var playBT = document.getElementById("playImage");
    playBT.style.display = "inline";
    var pauseBT = document.getElementById("pauseImage");
    pauseBT.style.display = "none";
}

function onNextBTClicked()
{
    if (timer != null)
    {
        Pause();
    }

    GetNextArticle();
}

function onPreviousBTClicked()
{
    if (timer != null)
    {
        Pause();
    }

    GetPreviousArticle();
}

function onPausePlayBTClicked()
{
    if (timer == null)
    {
        Play();
    }
    else
    {
        Pause();
    }
}

