globals.width = 40;
globals.height = 30;
globals.tailLength = 5;
globals.gameOver = true;
globals.score = 0;
globals.highScore = 0;
globals.blocks = [];
globals.cells = [];
globals.direction=1;
while(globals.blocks.length<globals.height)
{
	globals.blocks.push(0);
}
globals.tail = [];
while(globals.tail.length<globals.tailLength)
{
	globals.tail.push(Math.floor(globals.width/2));
}
globals.cellElements = [];
globals.scoreElement=null;
globals.highScoreElement=null;
while(globals.cells.length<globals.height)
{
	globals.cellElements.push([]);
	globals.cells.push([]);
	while(globals.cells[globals.cells.length-1].length<globals.width)
	{
		globals.cellElements[globals.cells.length-1].push(null);
		globals.cells[globals.cells.length-1].push("#000000");
	}
}
function keyPressHandler(e)
{
	var keynum;
	if(window.event)
	{
		keynum = e.keyCode;
	}
	else
	{
		keynum = e.which;
	}
	if(globals.gameOver)
	{
		if(keynum==13)
		{
			initializeGame();
			globals.gameOver=false;
		}
	}
	else
	{
		if(keynum==44)
		{
			globals.direction=-1;
		}
		else if(keynum==46)
		{
			globals.direction=1;
		}
	}
}
function updateBoard()
{
	var element = null;
	for(var y = 0; y < globals.height; ++y)
	{
		for(var x = 0; x < globals.width; ++x)
		{
			if(globals.cellElements[y][x].style.backgroundColor!=globals.cells[y][x])
			{
				globals.cellElements[y][x].style.backgroundColor=globals.cells[y][x];
			}
		}
	}
	globals.highScoreElement.innerHTML=globals.highScore;
	globals.scoreElement.innerHTML=globals.score;
}
function showWalls()
{
	for(var y=0;y<globals.height;++y)
	{
		globals.cells[y][0]="#0000FF";
		globals.cells[y][globals.width-1]="#0000FF";
	}
}
function showTail()
{
	for(var y=0;y<globals.tailLength;++y)
	{
		if(y==globals.tailLength-1)
		{
			globals.cells[y][globals.tail[y]]="#FF0000";
		}
		else
		{
			globals.cells[y][globals.tail[y]]="#FFFF00";
		}
	}
}
function showBlocks()
{
	for(var y=0;y<globals.height;++y)
	{
		globals.cells[y][globals.blocks[y]]="#ffffff";
	}
}
function clearBoard()
{
	for(var y=0;y<globals.height;++y)
	{
		for(var x=0;x<globals.width;++x)
		{
			globals.cells[y][x]="#000000";
		}
	}
}
function showBoard()
{
	clearBoard();
	showBlocks();
	showWalls();
	showTail();
	updateBoard();
}
function initializeGame()
{
	var y;
	for(y=0;y<globals.height;++y)
	{
		globals.blocks[y]=0;
	}
	for(y=0;y<globals.tailLength;++y)
	{
		globals.tail[y]=Math.floor(globals.width/2);
	}
	globals.score=0;
	globals.direction=1;
}
function updateGame()
{
	if(!globals.gameOver)
	{
		globals.score++;
		if(globals.score>globals.highScore)
		{
			globals.highScore=globals.score;
		}
		var y;
		for(y=0;y<globals.height-1;++y)
		{
			globals.blocks[y]=globals.blocks[y+1];
		}
		globals.blocks[globals.height-1]=Math.floor(Math.random()*(globals.width)+1);
		for(y=0;y<globals.tailLength-1;++y)
		{
			globals.tail[y]=globals.tail[y+1];
		}
		globals.tail[globals.tailLength-1]=globals.tail[globals.tailLength-1]+globals.direction;
		if((globals.tail[globals.tailLength-1]==globals.blocks[globals.tailLength-1])||(globals.tail[globals.tailLength-1]==0)||(globals.tail[globals.tailLength-1]==(globals.width-1)))
		{
			globals.gameOver=true;
			if(userData.userVerified)
			{
				addScore(userData.userEMail,userData.userPassword,globals.score);
			}
		}
	}
	showBoard();
}
generator["main"]=function()
{
	var result="";
	result+="<table cellspacing=\"0\" cellpadding=\"0\">";
	result+="<tr>";
	result+="<td id=\"highScore\" style=\"color:#00FF00;text-align:left;background-color:#808080;\" colspan=\""+globals.width/2+"\">0</td>";
	result+="<td id=\"score\" style=\"color:#00FF00;text-align:right;background-color:#808080;\" colspan=\""+globals.width/2+"\">0</td>";
	result+="</tr>";
	for(var y = 0; y < globals.height; ++y)
	{
		result+="<tr>";
		for(var x = 0; x < globals.width; ++x)
		{
			result+="<td id=\"cell_"+x+"_"+y+"\" style=\"width:16px;height:16px;background-color:#000000;\"></td>";
		}
		result+="</tr>";
	}
	result+="</table>";
	return(result);
}
var stats = {};
stats.highScore = 0;
stats.gamesPlayed = 0;
stats.cumulativeScore = 0;
stats.averageScore = 0;
function updateStats()
{
	var content = "";
	if(userData.userVerified)
	{
		content+="<p>";
		content+="High Score: "+stats.highScore+" Games Played: "+stats.gamesPlayed+" Cumulative Score: "+stats.cumulativeScore+" Average Score: "+stats.averageScore;
		content+="</p>";
	}
	document.getElementById("jsjetlagStats").innerHTML=content;
}
function fetchScoresCallback(result)
{
	var status = result.toString().parseJSON();
	if(status.Result)
	{
		stats.highScore = status.HighScore;
		stats.gamesPlayed = status.GamesPlayed;
		stats.cumulativeScore = status.CumulativeScore;
		stats.averageScore = ((status.GamesPlayed>0)?(Math.floor(stats.cumulativeScore/stats.gamesPlayed)):(0));
	}
	else
	{
		stats.highScore = 0;
		stats.gamesPlayed = 0;
		stats.cumulativeScore = 0;
		stats.averageScore = 0;
	}
	updateStats();
}
function fetchScores(userEMail,userPassword)
{
	ajax("http://www.playdeez.com/hiscores/highscoresilentMySQL.asp","axn=fetch&GameName=jsjetlag&UserEmail="+userEMail+"&UserPassword="+userPassword,fetchScoresCallback);
}
function addScore(userEMail,userPassword,score)
{
	ajax("http://www.playdeez.com/hiscores/highscoresilentMySQL.asp","axn=add&GameName=jsjetlag&UserEmail="+userEMail+"&UserPassword="+userPassword+"&Score="+score,fetchScoresCallback);
}
function onUserDataNotify(message)
{
	if(message=="status")
	{
		if(userData.userVerified)
		{
			fetchScores(userData.userEMail,userData.userPassword);
		}
	}
}
setGameState("main");
for(var y = 0; y < globals.height; ++y)
{
	for(var x = 0; x < globals.width; ++x)
	{
		globals.cellElements[y][x] = document.getElementById("cell_"+x+"_"+y);
	}
}
globals.highScoreElement=document.getElementById("highScore");
globals.scoreElement=document.getElementById("score");
initializeGame();
showBoard();
userData.addNotification(onUserDataNotify);
setInterval("updateGame()",50);
if(userData.userVerified)
{
	fetchScores(userData.userEMail,userData.userPassword);	
}
else
{
	updateStats();
}
