// JavaScript Document
//************************************************************************************
//GLOBAL VARIABLES
var filetype;
var games;
var totalGames;
var name;
var counter = 0;
var loopTimer;
var timer = false;
var auto;	
var team1;
var score1;	
var team2;
var score2;	
var info1;
var info2;	
var team3;
var score3;
var team4;
var score4;	
var info3;
var info4;
var status1;
var period1;
var time1;
var box1;
var status2;
var period2;
var time2;
var box2;

var title;
var daynext;
var dayprev;
var fileday;
var filemonth;
var fileyear;
var sportname;
var sportpath;
var sportnext;
var sportprev;

//************************************************************************************
//MAKE AN ASYNCRONOUS XMLHTTP REQUEST
function jsonRequest(url) 
{
	initFields();
	var script = document.createElement("script");
    script.src = url;
	
	var newdiv = document.createElement("div");
  	var divIdName = "prepSportsDiv";
  	newdiv.setAttribute('id',divIdName);
	
	newdiv.appendChild(script);
	
    // document.body.appendChild(newdiv);
    document.body.insertBefore(newdiv,document.body.firstChild);
}

//************************************************************************************
//TRAVERSE THE DOM TO LOCATE REQUIRED FIELDS
function initFields()
{
	if(timer)
	{
		clearTimeout(loopTimer);
		timer = false;
	}
	counter = 0;
	
	auto = document.getElementById("ps-autoplay");
	team1 = document.getElementById("ps-t1");
	score1 = document.getElementById("ps-s1");
	team2 = document.getElementById("ps-t2");
	score2 = document.getElementById("ps-s2");
	info1 = document.getElementById("ps-i1");
	info2 = document.getElementById("ps-i2");
	team3 = document.getElementById("ps-t3");
	score3 = document.getElementById("ps-s3");
	team4 = document.getElementById("ps-t4");
	score4 = document.getElementById("ps-s4");
	info3 = document.getElementById("ps-i3");
	info4 = document.getElementById("ps-i4");
        title = document.getElementById("ps-title");
        daynext = document.getElementById("ps-next");
        dayprev = document.getElementById("ps-prev");

	team1.innerHTML = "";
	score1.innerHTML = "";
	team2.innerHTML = "";
	score2.innerHTML = "";
	team3.innerHTML = "";
	score3.innerHTML = "";
	team4.innerHTML = "";
	score4.innerHTML = "";
	info1.innerHTML = "";
	info2.innerHTML = "";
	info3.innerHTML = "";
	info4.innerHTML = "";

        title.innerHTML = "";
}

//CREATE A GAMES ARRAY FROM THE RECEIVED JSON
function prepData(obj)
{	

        sportname = obj.sport;
        fileday = obj.day;
        filemonth = obj.month;
        fileyear = obj.year;
        sportpath = obj.path;
        sportnext = obj.nextsport;
        sportprev = obj.prevsport;

   	games = obj.games;

	totalGames = games.length;

	if(totalGames % 2 == 1)
	{
		var game = 	{
			"game": 
			{
			"home_team":" ",
			"home_team_score":" ",
                        "home_team_index":" ",
			"visiting_team":" ",
			"visiting_team_score":" ",
                        "visiting_team_index":" ",
                        "site_team":" ",
			"period":" ",
			"status":" ",
			"time":" ",
			"boxscore":" "
			} 
		}
		games.push(game);
		totalGames = games.length;
	}
	
	setJSON();
	
	if(loopTimer)
	{
		clearTimeout(loopTimer);	
	}
	loopTimer = setTimeout("setJSON()",5000);

	timer = true;
	auto.innerHTML = "<a href='javascript:autoPlay()'>AutoPlay On</a>";
	
	var prepDiv = document.getElementById("prepSportsDiv");
	document.body.removeChild(prepDiv);
}

//************************************************************************************
//ASSIGN THE RECEIVED JSON DATA TO THE MODULE FIELDS
function setJSON()
{

        title.innerHTML = "High School Scoreboard<br><a href=" + sportpath + filemonth +fileday +".shtm>"+sportname + " - "+filemonth+"/"+fileday+"</a>";

        dayprev.innerHTML = "<b><a href=javascript:jsonRequest('http://prepsports3.stltoday.com/scores/"+sportprev+"')> << </a></b>";
        daynext.innerHTML = "<b><a href=javascript:jsonRequest('http://prepsports3.stltoday.com/scores/"+sportnext+"')> >> </a></b>";

        team1.innerHTML = "<a href="+sportpath+"teamschedule"+games[counter].game.visiting_team_index+".shtm>"+games[counter].game.visiting_team+"</a>";
	score1.innerHTML = games[counter].game.visiting_team_score;
	
        team2.innerHTML = "<a href="+sportpath+"teamschedule"+games[counter].game.home_team_index+".shtm>"+games[counter].game.home_team+"</a>";
        if (games[counter].game.site_team.length>1)
        {
          team2.innerHTML = team2.innerHTML + " at "+games[counter].game.site_team;
        }
	score2.innerHTML = games[counter].game.home_team_score;
	
	status1 = games[counter].game.status;
	period1 = games[counter].game.period;
	time1 = games[counter].game.time;
	box1 = games[counter].game.boxscore;
	
	if(status1 >= 21 && status1 <= 38)
	{
		info1.innerHTML = "Quarter: " + games[counter].game.period;	
		
		if(period1 > 4)
		{
			info1.innerHTML = "Overtime";
		}
	}
	else if(status1 == 39)
	{
		info1.innerHTML = "Halftime";
	}
        else if(status1 == 6)
        {
                info1.innerHTML = "Postponed";
        }
        else if(status1 == 13)
        {
                info1.innerHTML = "Forfeit";
        }
	else if(status1 == 2 || status1 == 3 || status1 == 7 || status1 == 9 || status1 == 10 || status1 == 40)
	{
		info1.innerHTML = "Final";	
	}
	else
	{
		info1.innerHTML = "Game Time: " + time1;
	}
	
	if(box1 == " ") 
	{
		info2.innerHTML = "";
	}
	else
	{
  	  info2.innerHTML = "<a href='" + sportpath + box1 + "' target='_blank'>Boxscore</a>";
        }

	counter++;
	
	if(counter >= totalGames) counter = 0;
	

        team3.innerHTML = "<a href="+sportpath+"teamschedule"+games[counter].game.visiting_team_index+".shtm>"+games[counter].game.visiting_team+"</a>";
	score3.innerHTML = games[counter].game.visiting_team_score;
	
        team4.innerHTML = "<a href="+sportpath+"teamschedule"+games[counter].game.home_team_index+".shtm>"+games[counter].game.home_team+"</a>";
        if (games[counter].game.site_team.length>1)
        {
          team4.innerHTML = team4.innerHTML + " at "+games[counter].game.site_team;
        }
	score4.innerHTML = games[counter].game.home_team_score;
	
	status2 = games[counter].game.status;
	period2 = games[counter].game.period;
	time2 = games[counter].game.time;
	box2 = games[counter].game.boxscore;


	if(status2 >= 21 && status2 <= 38)
	{
		info3.innerHTML = "Quarter: " + games[counter].game.period;	
		
		if(period2 > 4)
		{
			info2.innerHTML = "Overtime";
		}
	}
	else if(status2 == 39)
	{
		info3.innerHTML = "Halftime";
	}
        else if(status2 == 6)
        {
                info3.innerHTML = "Postponed";
        }
        else if(status2 == 13)
        {
                info3.innerHTML = "Forfeit";
        }
	else if(status2 == 2 || status2 == 3 || status2 == 7 || status2 == 9 || status2 == 10 || status2 == 40)
	{
		info3.innerHTML = "Final";	
	}
	else
	{
		info3.innerHTML = "Game Time: " + time2;
	}
	

	if(box2 == " ") 
	{
		info4.innerHTML = "";
	}
	else
	{
     	  info4.innerHTML = "<a href='" + sportpath + box2 + "' target='_blank'>Boxscore</a>";
        }


	counter++;
	
	if(counter >= totalGames) counter = 0;
	
	if(timer) loopTimer = setTimeout("setJSON()",5000);
}

//************************************************************************************
//MOVE FORWARD TO THE NEXT GAME
function nextGame()
{	
	clearTimeout(loopTimer);
	
	auto.innerHTML = "<a href='javascript:autoPlay()'>AutoPlay Off</a>";
	
	timer = false;
	
	setJSON();
}

//************************************************************************************
//MOVE BACKWARDS TO THE PREVIOUS GAME
function prevGame()
{	
	clearTimeout(loopTimer);
	
	auto.innerHTML = "<a href='javascript:autoPlay()'>AutoPlay Off</a>";
	
	timer = false;

	if(counter == 0)
	{
		counter = (totalGames - 4);	
	}
	else
	{
		counter -= 4;
		if(counter < 0)
		{
			counter = (totalGames - 2);	
		}
	}
	
	setJSON();
}

//************************************************************************************
//AUTOMATICALLY MOVE THROUGH THE AVAILABLE GAMES
function autoPlay()
{
	if(!timer)
	{
		loopTimer = setTimeout("setJSON()",5000);
		timer = true;
		auto.innerHTML = "<a href='javascript:autoPlay()'>AutoPlay On</a>";
	}
	else if(timer)
	{
		clearTimeout(loopTimer);
		auto.innerHTML = "<a href='javascript:autoPlay()'>AutoPlay Off</a>";
		timer = false;
	}
}

//************************************************************************************

