﻿if (window.XMLHttpRequest) {
    xhttp = new XMLHttpRequest();
}
else // Internet Explorer 5/6
{
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "news.xml", false);
xhttp.send("");
xmlDoc = xhttp.responseXML;



//FUNCTIONS FOR THE NEWS TICKER


// Ticker startup
function restartTicker() {
    //reset variable to say we can loop
    thePauseVar = 0;
    //restart running ticker
    runTheTicker();
}

// Ticker startup
function startTicker() {
    // Define run time values
    theCurrentStory = -1;
    theCurrentLength = 0;
    // Locate base objects
    if (document.getElementById) {
        theAnchorObject = document.getElementById("tickerAnchor");
        runTheTicker();
    }
    else {
        document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
        return true;
    }
}
// Ticker main run loop
function runTheTicker() {

    if (thePauseVar == 0) {
        var myTimeout;
        // Go for the next story data block
        if (theCurrentLength == 0) {
            theCurrentStory++;
            theCurrentStory = theCurrentStory % theItemCount;
            theStorySummary = theSummaries[theCurrentStory].replace(/"/g, '"');
            theTargetLink = theSiteLinks[theCurrentStory];
            theAnchorObject.href = theTargetLink;
            thePrefix = "<span class=\"tickls\">" + theLeadString + "</span>";
        }
        // Stuff the current ticker text into the anchor
        theAnchorObject.innerHTML = thePrefix +
 theStorySummary.substring(0, theCurrentLength) + whatWidget();
        // Modify the length for the substring and define the timer
        if (theCurrentLength != theStorySummary.length) {
            theCurrentLength++;
            myTimeout = theCharacterTimeout;
        }
        else {
            theCurrentLength = 0;
            myTimeout = theStoryTimeout;
        }
        // Call up the next cycle of the ticker
        setTimeout("runTheTicker()", myTimeout);
        theStoryTimeout = 5000;
    }
    // Widget generator
    function whatWidget() {
        if (theCurrentLength == theStorySummary.length) {
            return theWidgetNone;
        }

        if ((theCurrentLength % 2) == 1) {
            return theWidgetOne;
        }
        else {
            return theWidgetTwo;
        }
    }
}

function stopTicker() {
    if (startstopflag == 0) {
        storyPaused = theCurrentStory;
        lengthPaused = theCurrentLength;
        //alert("Stry paused: " + storyPaused + "    lengthPaused: " + lengthPaused);
        thePauseVar = 1;
        theCurrentLength = theStorySummary.length;
        theStoryTimeout = 1000;
        theAnchorObject.innerHTML = thePrefix + theStorySummary;
        if (document.getElementById) {
            theButton = document.getElementById("StopStartButton");
            theButton.className = "startbutton";
            theButton.title = "Start ticker";
        }
        startstopflag = 1;
    }
    else {
        restartTicker();
        startstopflag = 0;
        if (document.getElementById) {
            theButton = document.getElementById("StopStartButton");
            theButton.className = "stopbutton";
            theButton.title = "Pause ticker";
        }
    }
}

function nextStory() {
    if (theCurrentStory < theSummaries.length - 1) {
        theCurrentStory++;
        theStorySummary = theSummaries[theCurrentStory]
        theTargetLink = theSiteLinks[theCurrentStory];
    }
    else {

        theCurrentStory = 0;
        theStorySummary = theSummaries[theCurrentStory]
        theTargetLink = theSiteLinks[theCurrentStory];
    }

    //alert("Stry paused: " + storyPaused + "    lengthPaused: " + lengthPaused);
    thePauseVar = 1;
    theCurrentLength = theStorySummary.length;
    theStoryTimeout = 1000;
    theAnchorObject.innerHTML = thePrefix + theStorySummary;
    theAnchorObject.href = theTargetLink;
    if (document.getElementById) {
        theButton = document.getElementById("StopStartButton");
        theButton.className = "startbutton";
        theButton.title = "Start ticker";
    }
    startstopflag = 1;
}

function previousStory() {
    if (theCurrentStory > 0) {
        theCurrentStory = theCurrentStory - 1;
        theStorySummary = theSummaries[theCurrentStory]
        theTargetLink = theSiteLinks[theCurrentStory];
    }
    else {

        theCurrentStory = theSummaries.length - 1;
        theStorySummary = theSummaries[theCurrentStory]
        theTargetLink = theSiteLinks[theCurrentStory];
    }

    //alert("Stry paused: " + storyPaused + "    lengthPaused: " + lengthPaused);
    thePauseVar = 1;
    theCurrentLength = theStorySummary.length;
    theStoryTimeout = 1000;
    theAnchorObject.innerHTML = thePrefix + theStorySummary;
    theAnchorObject.href = theTargetLink;
    if (document.getElementById) {
        theButton = document.getElementById("StopStartButton");
        theButton.className = "startbutton";
        theButton.title = "Start ticker";
    }
    startstopflag = 1;
}

var theCharacterTimeout = 50;
var theStoryTimeout = 5000;
var theWidgetOne = "_";
var theWidgetTwo = "-";
var theWidgetNone = "";
var theLeadString = "LATEST:&nbsp;";

var theSummaries = new Array();
var theSiteLinks = new Array();



var thePauseVar = 0; //variable to say if paused or not 0=no 1=yes
var storyPaused = 0;  //variable to hold number of story paused at
var startstopflag = 0; //variable to flag started or stopped ticker

var storyPaused;
var lengthPaused;

var x = xmlDoc.getElementsByTagName("link");
for (i = 0; i < x.length; i++) {
    theSummaries[i] = (x[i].getElementsByTagName("text")[0].childNodes[0].nodeValue);
    theSiteLinks[i] = (x[i].getElementsByTagName("url")[0].childNodes[0].nodeValue);
}

var theItemCount = theSummaries.length;
startTicker();


//theSummaries[0] = "The Sunrise Coast Events Guide 2009 is now available online - Click on this headline for details";
//theSiteLinks[0] = "http://www.visit-sunrisecoast.co.uk/Pages/ThingsToDo/Whats%20on.aspx";

//theSummaries[1] = "2010 Lowestoft Seafront Air Festival dates 12th &amp; 13th August 2010"
//theSiteLinks[1] = "http://www.lowestoftairfestival.co.uk/"


