﻿var xmlReq;
var programHolder = new Array();
var informationFeed = new Array();
var informationIndex = 0;
var informationLimit = 0;
var programIndex = 0;
var programLimit = 0;
var contactID = "";
var imageID = "";
var addressID = "";
var cityStateID = "";
var phoneID = "";
var blurbID = "";
var commandParsed = false;
var dimensionX = 0;
var dimensionY = 0;
var dimensionZ = 0;
var dimension4 = 0;
var theDowJonesClusteredIndex;
var mirror = "";
var bToggleControllerLight = true;
var programSecondCounter = 0;

function init() 
{
    xmlDoc3 = new window.XMLHttpRequest();
    xmlDoc3.open("GET", "info.xml", false);
    xmlDoc3.send("");

    var xOb = xmlDoc3.responseXML
    var infoItems = xOb.getElementsByTagName("informationItem");
    numInformationItems = infoItems.length;

    for (var k = 0; k < infoItems.length; k++) 
    {
        informationFeed.push(infoItems[k].getAttribute("text"));
        informationLimit = k;
    }

    document.getElementById('txtInformationCenter').value = informationFeed[informationIndex];
    informationIndex++;

    startTimer();
}



function startTimer() 
{
    theDowJonesClusteredIndex = document.getElementById("txtTimer");
    dimension4 = Math.round(1000 * Math.random())
    intervalID = setInterval("sync()", 100);
}

function sync() 
{
     mirror = "              " + dimensionX + " : " + dimensionY + " : " + dimensionZ + "        " + dimension4;

    theDowJonesClusteredIndex.value = mirror;
    dimensionX++;//100ms interval


    if (dimensionX == 10) //one second has elapsed
    {
        programSecondCounter++;
        if (programSecondCounter == 61) 
        {
            programSecondCounter = 0;
        }
        if (programSecondCounter == 10 || programSecondCounter == 20 || programSecondCounter == 30 || programSecondCounter == 40 || programSecondCounter == 60) 
        {
            document.getElementById('txtInformationCenter').value = informationFeed[informationIndex];
            informationIndex++;
            if (informationIndex > informationLimit) 
            {
                informationIndex = 0;
            }
        }



        dimensionX = 0;
        dimensionY++;

        //toggle controller light
        if (bToggleControllerLight == true) 
        {
            document.getElementById('imgController').style.display = 'none';
            bToggleControllerLight = false;
        }
        else 
        {
            document.getElementById('imgController').style.display = 'block';
            bToggleControllerLight = true;
        }
    }

    if (dimensionY == 10) 
    {
        dimensionY = 0;
        dimensionZ++;
        dimension4 = Math.round(100 * Math.random());
    }


    if (dimensionZ == 10) 
    {
        dimensionX = 0;
        dimensionY = 0;
        dimensionZ = 0;
        dimension4 = 0;
    }


}



function loadStartupProgram() 
{
    //alert("load startup");
    if (window.XMLHttpRequest) 
    {
        initializeGenericServiceVariables();
        //alert("XMLHttpRequest");


        document.getElementById('txtInput').focus();
        xmlReq = new XMLHttpRequest();
        var url = "ProgramHandler.ashx?channel=food&";
        xmlReq.open("Get", url, false);
        xmlReq.onreadystatechange = programReceive;
        xmlReq.send(null);
    }
}

function programReceive() 
{
    if (xmlReq.readyState != 4) 
    {
        return;
    }

    if (xmlReq.status == 200) 
    {
       
        var results = xmlReq.responseText.split('&');
        var resnode = 0;
        for (resnode = 0; resnode < results.length; resnode++) 
        {
            programHolder.push(results[resnode]);
            //alert(programHolder[resnode]);
            programLimit = resnode -1; //empty result
            flowplayer('player').stop();
        }        
    }
}


function play() 
{


    var info = programHolder[programIndex];
    programIndex++;
    if (programIndex > programLimit) 
    {
        programIndex = 0;
    }
    var programResults = info.split(';');

    document.getElementById(contactID).innerHTML = programResults[0]; //name
    document.getElementById(addressID).innerHTML = programResults[1];
    var loc = programResults[2] + ', ' + programResults[3];
    document.getElementById(cityStateID).innerHTML = loc;
    document.getElementById(blurbID).innerHTML = programResults[4];
    document.getElementById(phoneID).innerHTML = programResults[5];
    document.getElementById(imageID).src = programResults[6];
    flowplayer('player').play(programResults[7]);
    
}

function parseCommand() 
{

    var command = document.getElementById('txtInput').value;
    //alert(command);
    document.getElementById('txtInput').value = "";
    document.getElementById('txtInput').focus();

    switch (command.toUpperCase()) 
    {
        case "NEXT":
            flowplayer('player').stop();
            commandParsed = true;
            play();
            break;
        case "PREVIOUS":
            if (programIndex == 0) {
                programIndex = programLimit;
                programIndex--;
            }
            else if (programIndex == 1) //first clip
            {
                programIndex = programLimit;
            }
            else {
                programIndex--;
                //alert("first subtract: " + programIndex);
                programIndex--;
                //alert("second subtract: " + programIndex);
            }

            //alert('index now ' + programIndex);


            flowplayer('player').stop();
            commandParsed = true;
            play();
            break;
    }
}

