﻿function StartTimer() {
    setTimeout('CatchTimer( )', 1000);
}
function CatchTimer() {
    var input = document.getElementById("timer");
    var time = parseInt(input.value);
    if (time < 1) {
        input.value = 10;
        ForwardOne();
    }
    else {
    }
    input.value = (time - 1);
    setTimeout("CatchTimer( )", 1000);
}
function Visible(iNum) 
{
    //First get variables to access elements (including the ASP table)
    var table = document.getElementById("DivWrapper");  //THIS ID MAY NEED TO CHANGE
    var divs = table.getElementsByTagName("div");
    
    //Reset all the elements to be hidden, excepting the one to be visible
    for (var i = 0; i < divs.length; i++) 
    {
        var div = divs[i];
        div.style.display = "none";
        if (div.id == ("section" + iNum)) 
        {
            div.style.display = "block";
        }
    }
}
function BackTwo()
{
    document.getElementById("repeat").value = "true";
    BackOne( );
}
function BackOne() 
{
    //Remove the selected state from the cell
    var table = document.getElementById("Scroller").getElementsByTagName("table")[0];
    var cells = table.getElementsByTagName("td");  
    var selected = cells[2];
    selected.className = "";

    //Shuffle the cells around to wrap like a white boy
    var lastCell = cells[cells.length - 1];
    var firstCell = cells[0];
    lastCell.parentNode.removeChild(lastCell);
    firstCell.parentNode.insertBefore(lastCell, firstCell);

    //Set the left position to accommodate an additional cell
    table.style.left = "-55px";
    
    //Move the table to the left (to the left)
    setTimeout('ScrollLeft( )', 1);
}
function ScrollLeft() 
{
    var table = document.getElementById("Scroller").getElementsByTagName("table")[0];
    var startLeft = table.style.left;

    //Get the CSS style, if there is none it hasn't been indexed yet and set it to 0
    if (startLeft == null || startLeft == "") 
    {
        startLeft = 0;
    }
    else 
    {
        startLeft = parseInt(startLeft.replace("px", ""));
    }

    //Move one pixel
    table.style.left = (startLeft + 5) + "px";

    //As long as there is more scrolling to be done to align it left again
    //do so, until the after function is to be called
    if (startLeft < 0) 
    {
        setTimeout('ScrollLeft( )', 1);
    }
    else 
    {
        PostLeft();
    }
}
function PostLeft() 
{
    var place = document.getElementById("place");
    var table = document.getElementById("Scroller").getElementsByTagName("table")[0];
    var cells = table.getElementsByTagName("td");

    //Readjust the cell that is selected
    var toBeSelected = cells[2];
    toBeSelected.className = "Selected";

    //Its aligned to the left, fo sho
    table.style.left = "0px";

    //Get the ID value and then set the associated content area visible
    var sectionID = toBeSelected.getElementsByTagName("input")[0];
    place.value = sectionID.value;
    Visible(place.value);

    //Set the correct image
    var image = toBeSelected.getElementsByTagName("img")[0];
    var url = image.src.replace("small", "large");
    var alt = image.alt;
    SetImage(url, alt, place.value);

    //Preload the large images surrounding the selected one
    PreLoad(toBeSelected);
    
    //MoveToFirst again if you are moving two spaces
    if (document.getElementById("repeat").value == "true") 
    {
        document.getElementById("repeat").value = "false";
        BackOne();
    }
}
function ForwardTwo( )
{
    document.getElementById("repeat").value = "true";
    ForwardOne( );
}
function ForwardOne() 
{
    //Readjust the cell that is selected
    var table = document.getElementById("Scroller").getElementsByTagName("table")[0];
    var cells = table.getElementsByTagName("td");
    var selected = cells[2];
    selected.className = "";
    
    //Right, right?  Right!
    setTimeout('ScrollRight( )', 1);
}
function ScrollRight() 
{
    var table = document.getElementById("Scroller").getElementsByTagName("table")[0];
    var startLeft = table.style.left;
    //Gets the right style, and sets the value, catching it if it does not exist
    if (startLeft == null || startLeft == "") 
    {
        startLeft = 0;
    }
    else 
    {
        startLeft = parseInt(startLeft.replace("px", ""));
    }

    //Right one pixel, obvious
    table.style.left = (startLeft - 5) + "px";

    //Repeats until its gone too far
    if (startLeft > -55) 
    {
        setTimeout('ScrollRight( )', 1);
    }
    else 
    {
        PostRight();
    }
}
function PostRight() 
{
    var place = document.getElementById("place");
    var table = document.getElementById("Scroller").getElementsByTagName("table")[0];
    var cells = table.getElementsByTagName("td");

    //Get the first cell to move it to the end of the row
    var lastCell = cells[cells.length - 1];
    var firstCell = cells[0];
    firstCell.parentNode.removeChild(firstCell);
    lastCell.parentNode.appendChild(firstCell);

    //Readjust the cell that is selected
    var toBeSelected = cells[2];
    toBeSelected.className = "Selected";

    //Make SURE the thing is at baseline
    table.style.left = "0px";

    //set the associated text area
    var sectionID = toBeSelected.getElementsByTagName("input")[0];
    place.value = sectionID.value;
    Visible(place.value);

    //Set the correct image
    var image = toBeSelected.getElementsByTagName("img")[0];
    var url = image.src.replace("small", "large");
    var alt = image.alt;
    SetImage(url, alt, place.value);

    //Preload the large images surrounding the selected one
    PreLoad(toBeSelected);

    //Repeat the process if it is moving two spaces
    if (document.getElementById("repeat").value == "true") 
    {
        document.getElementById("repeat").value = "false";
        ForwardOne();
    }
}
function SetImage(url, alt, num) 
{
    //Specify the attributes for the main image
    var image = document.getElementById("Hero");
    image.src = url;
    image.alt = alt;

    var link = image.parentNode;
    var section = document.getElementById("section" + num);
    var linkTo = section.getElementsByTagName("a")[0];
    link.href = linkTo.href;
}
function PreLoad(cell) 
{
    if (cell == null) { return; }
    //Preload for the first, second, fourth, and fifth cells
    var cells = cell.parentNode.getElementsByTagName("td");
    Loader(cells[0]);
    Loader(cells[1]);
    Loader(cells[3]);
    Loader(cells[4]);

    //Set the link attributes so that the links in the cells will work correctly
    SetLinks(cells[0], "BackTwo");
    SetLinks(cells[1], "BackOne");
    SetLinks(cells[2], "");
    SetLinks(cells[3], "ForwardOne");
    SetLinks(cells[4], "ForwardTwo");

    var timer = document.getElementById("timer");
    timer.value = 7;
}
function Loader(cell)
{
    if (cell == null) { return; }
    var img = cell.getElementsByTagName("img")[0];
    var src = img.src.replace("small", "large");
    //Create a new image with the specifications of the large one
    var load = new Image(300, 200);
    load.src = src;
}
//Resets the click links each time it rotates to keep is consistant
function SetLinks(cell,call)
{
    if (cell == null) { return; }
    var link = cell.getElementsByTagName("a")[0];
    if (call != "")
    {
        link.href = "javascript:" + call + "();";
    }
    else 
    {
        link.href = "";
    }
}
//Moves the cells so that the first element is the one that is in the middle
function MoveToFirst()
{
    var table = document.getElementById("Scroller").getElementsByTagName("table")[0];
    var cells = table.getElementsByTagName("td");
    //Get the last cell to move it to the beginning of the row
    var lastCell = cells[cells.length - 1];
    var firstCell = cells[0];
    lastCell.parentNode.removeChild(lastCell);
    firstCell.parentNode.insertBefore(lastCell, firstCell);
    //Get the last cell to move it to the beginning of the row
    lastCell = cells[cells.length - 1];
    firstCell = cells[0];
    lastCell.parentNode.removeChild(lastCell);
    firstCell.parentNode.insertBefore(lastCell, firstCell);
}
//Incraments the counter used in IE to specify how many items are in the list
function UpCount() {
    var field = document.getElementById("count");
    var num = parseInt(field.value);
    field.value = num + 1;
}
//Used only for IE to scroll through all of the items in the list to make it not $%!& up
function RollThrough() {
    var field = document.getElementById("count");
    var num = parseInt(field.value);
    for (var i = 0; i < num; i++) {
        BackOne();
    }
}