//life.js

var lblStatus, lblLife, lblGeneration;
var control;
var TheWorld;
var TheWorld1;
var TheWorld2;
var canvasWorld;
var TheHeight = 38;
var TheWidth = 58;
var TheSize = 10;
var MustStop = false;

function Loaded(sender,args)
{
	control = sender.getHost();
	lblStatus = sender.findName("lblStatus");
	lblLife = sender.findName("lblLife");
	lblGeneration = sender.findName("lblGeneration");

	canvasWorld = sender.findName("canvasWorld");
	MakeLife();
	UpdateInfo();
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function MakeLife()
{
	var xmllife;
	var xx, yy, x, y;
	yy=0;
	TheWorld = new Array();
	TheWorld1 = new Array();
	TheWorld2 = new Array();
	for (y=0;y<TheHeight;y++)
	{
		TheWorld[y] = new Array();
		TheWorld1[y] = new Array();
		TheWorld2[y] = new Array();
		xx=0;
		for (x=0;x<TheWidth;x++)
		{
			xmllife = "<Rectangle Height='"+TheSize+"' Width='"+TheSize+"' Canvas.Left='"+xx+"' Canvas.Top='"+yy+"' Fill='Blue' Visibility='Collapsed'/>"
			TheWorld[y][x] = control.content.createFromXaml(xmllife);
			TheWorld1[y][x] = 0;
			TheWorld2[y][x] = 0;
			canvasWorld.children.add(TheWorld[y][x]);
			xx+=TheSize;
		}
		yy+=TheSize;
	}
	//alert("done");
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function Button_MouseLeftButtonDown(sender, args) 
{
	var buttonname = sender.name;
	//window.location = "about-frames.html";
	//var elips = sender.findName("elips");
	//elips.Fill = "blue";
	//elips["Canvas.Top"] = 20;
	sender.findName(buttonname + "Rect").Opacity = 0.7;
}

function Button_MouseLeftButtonUp(sender, args) 
{
	var buttonname = sender.name;
	//window.location = "about-frames.html";
	//var elips = sender.findName("elips");
	//elips.Fill = "blue";
	//elips["Canvas.Top"] = 20;
	sender.findName(buttonname + "Rect").Opacity = 1;
	DoClick(buttonname);
}

function Button_MouseEnter(sender,args)
{
	var buttonname = sender.name;
	sender.findName(buttonname + "Rect").Stroke = "Black";
}

function Button_MouseLeave(sender,args)
{
	var buttonname = sender.name;
	sender.findName(buttonname + "Rect").Stroke = "Gray";
	sender.findName(buttonname + "Rect").Opacity = 1;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function DoClick(buttonname)
{
	if (buttonname == "btnStart")
	{
		if (TheStatus=="Stopped")
		{
			TheStatus = "Running";
			MustStop = false;
			setTimeout("OnTimer()",500);
		}
	}
	else if (buttonname == "btnStep")
	{
		if (TheStatus=="Stopped")
			DoStep();
	}
	else if (buttonname == "btnStop")
	{
		MustStop = true;
		TheStatus = "Stopped";
	}
	else if (buttonname == "btnClear")
	{
		DoClear();
		TheLife = 0;
	}
	else if (buttonname == "btnRandom")
	{
		DoRandom();
		CountLife();
	}
	else if (buttonname == "btnPreset1")
	{
		if (TheStatus=="Stopped")
			DoPreset1();
	}
	else if (buttonname == "btnPreset2")
	{
		if (TheStatus=="Stopped")
			DoPreset2();
	}
	else if (buttonname == "btnPreset3")
	{
		if (TheStatus=="Stopped")
			DoPreset3();
	}
	else if (buttonname == "btnPreset4")
	{
		if (TheStatus=="Stopped")
			DoPreset4();
	}
	else if (buttonname == "btnPreset5")
	{
		if (TheStatus=="Stopped")
			DoPreset5();
	}
	else if (buttonname == "btnPreset6")
	{
		if (TheStatus=="Stopped")
			DoPreset6();
	}
	UpdateInfo();
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

var TheStatus = "Stopped";
var TheLife = 0;
var TheGeneration = 0;

var LastStatus = "-";
var LastLife = -1;
var LastGeneration = -1;

function UpdateInfo()
{
	if (TheStatus != LastStatus)
	{
		lblStatus.Text = TheStatus;
		LastStatus = TheStatus;
	}
	if (TheLife != LastLife)
	{
		lblLife.Text = TheLife + '';
		LastLife = TheLife;
	}
	if (TheGeneration != LastGeneration)
	{
		lblGeneration.Text = TheGeneration + '';
		LastGeneration = TheGeneration;
	}
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function DoRandom()
{
	var x,y;
	for (i=0;i< (TheWidth * TheHeight) / 5;i++)
	{
		x = Math.round( Math.random() * (TheWidth-1) );
		y = Math.round( Math.random() * (TheHeight-1) );
		TheWorld1[y][x]=1;
		TheWorld[y][x].Visibility="Visible";
		TheWorld[y][x].Fill="Blue";
	}
}

function DoClear()
{
	var x,y;
	for (x=0;x<TheWidth;x++)
	{
		for (y=0;y<TheHeight;y++)
		{
			TheWorld1[y][x]=0;
			TheWorld[y][x].Visibility="Collapsed";
			TheWorld[y][x].Fill="Blue";
		}
	}
	TheLife = 0;
	TheGeneration=0;
}

function CountLife()
{
	var x,y;
	TheLife=0;
	for (x=0;x<TheWidth;x++)
	{
		for (y=0;y<TheHeight;y++)
		{
			if (TheWorld1[y][x]==1)
				TheLife++;
		}
	}
}

function OnTimer()
{
	if (MustStop)
	{
		TheStatus = "Stopped";
	}
	else
	{
		DoStep();
		setTimeout("OnTimer()",20);
	}
	UpdateInfo();
}

var LastCountMin1 = 1;
var LastCountMin2 = 2;
var LastCountMin3 = 3;

function DoStep()
{
	LastCountMin3 = LastCountMin2;
	LastCountMin2 = LastCountMin1;
	LastCountMin1 = TheLife;
	
	TheGeneration++;
	for (x=0;x<TheWidth;x++)
	{
		for (y=0;y<TheHeight;y++)
		{
			TheWorld2[y][x]=TheWorld1[y][x];
		}
	}	
	for (x=0;x<TheWidth;x++)
	{
		for (y=0;y<TheHeight;y++)
		{
			buren = CountNeighbours(x,y);
			if (TheWorld1[y][x] == 1)
			{
				if (buren < 2)
				{
					TheWorld1[y][x] = 0;
					TheWorld[y][x].Visibility="Collapsed";
					TheWorld[y][x].Fill="Blue";
					TheLife--;
				}
				else if (buren > 3)
				{
					TheWorld1[y][x] = 0;
					TheWorld[y][x].Visibility="Collapsed";
					TheWorld[y][x].Fill="Blue";
					TheLife--;
				}
				else
					TheWorld[y][x].Fill="Blue";

			}
			else
			{
				if (buren == 3)
				{
					TheWorld1[y][x] = 1;
					TheWorld[y][x].Visibility="Visible";
					TheWorld[y][x].Fill="Red";
					TheLife++;
				}
			}
		}
	}	
	//if ((TheLife == LastCountMin1) && (LastCountMin1 == LastCountMin2) && (LastCountMin3 == LastCountMin2))
	//	MustStop = true;
}

function CountNeighbours(x,y)
{
	var c = 0;
	var xplus = x + 1;
	var yplus = y + 1;
	var xmin = x - 1;
	var ymin = y - 1;
	if (xmin < 0)
		xmin += TheWidth;
	if (ymin < 0)
		ymin += TheHeight;
	if (xplus >= TheWidth)
		xplus -= TheWidth;
	if (yplus >= TheHeight)
		yplus -= TheHeight;

	c = c + TheWorld2[y][xmin];
	c = c + TheWorld2[y][xplus];

	c = c + TheWorld2[yplus][xplus];
	c = c + TheWorld2[yplus][x];
	c = c + TheWorld2[yplus][xmin];

	c = c + TheWorld2[ymin][xplus];
	c = c + TheWorld2[ymin][x];
	c = c + TheWorld2[ymin][xmin];
	
	return c;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function DoPreset1()
{
	DoClear();
	for (y=10;y<TheHeight;y+=10)
	{
		DoPreset(TheWidth / 3,y,"0,0 1,0 2,0 0,1 1,2");	//Glider
		DoPreset(TheWidth / 3 * 2,y,"0,0 1,0 2,0 0,1 1,2");	//Glider
	}
}
function DoPreset2()
{
	DoClear();
	for (y=10;y<TheHeight;y+=10)
	{
		DoPreset(TheWidth / 3,y,"1,0 4,0 0,1 0,2 4,2 0,3 1,3 2,3 3,3");	//LWSS
		DoPreset(TheWidth / 3 * 2,y,"1,0 4,0 0,1 0,2 4,2 0,3 1,3 2,3 3,3");	//LWSS
	}
}
function DoPreset3()
{
	DoClear();
	DoPreset(TheWidth / 2,TheHeight / 2,"0,5 2,4 2,5 4,1 4,2 4,3 6,0 6,1 6,2 7,1");	//Infinit
}
function DoPreset4()
{
	DoClear();
	DoPreset(TheWidth / 2,TheHeight / 2,"0,0 0,1 0,4 1,0 1,3 2,0 2,3 2,4 3,2 4,0 4,2 4,3 4,4");	//Infinit
}
function DoPreset5()
{
	DoClear();
	DoPreset((TheWidth-34) / 2,(TheHeight-8) / 2,"0,4 0,5 1,4 1,5 10,4 10,5 10,6 11,3 11,7 12,2 12,8 13,2 13,8 14,5 15,3 15,7 16,4 16,5 16,6 17,5 20,2 20,3 20,4 21,2 21,3 21,4 22,1 22,5 24,0 24,1 24,5 24,6 34,2 34,3 35,2 35,3");	//Gosper Glider Gun
}
function DoPreset6()
{
	DoClear();
	DoPreset((TheWidth-28) / 2,(TheHeight-18) / 2,"0,0 0,1 0,17 0,18 1,0 1,1 1,17 1,18 7,15 8,14 8,16 9,14 9,16 10,15 11,11 11,12 12,6 12,7 12,10 12,13 13,5 13,8 13,10 13,13 14,5 14,8 14,10 14,13 15,5 15,8 15,11 15,12 16,6 16,7 17,3 18,2 18,4 19,2 19,4 20,3 26,0 26,1 26,17 26,18 27,0 27,1 27,17 27,18");	//Gosper Glider Gun
}
function DoPreset(dx, dy, data)
{
	dx = Math.floor(dx);
	dy = Math.floor(dy);
	var a = data.split(' ');
	for (i=0;i<a.length;i++)
	{
		var b = a[i].split(',');
		x = parseInt(b[0]) + dx;
		y = parseInt(b[1]) + dy;
		if (x>=0 && x<TheWidth && y>=0 && y<TheHeight)
		{
			TheWorld1[y][x] = 1;
			TheWorld[y][x].Visibility="Visible";
			TheLife++;
		}
	}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var WorldDown = false;
var lastwx = 0;
var lastwy = 0;
function World_MouseLeftButtonDown(sender, args) 
{
	if (TheStatus=="Stopped")
		WorldDown = true;
}
function World_MouseLeftButtonUp(sender, args) 
{
	if (WorldDown)
	{
		x = Math.floor((args.getPosition(null).x-10) / TheSize);
		y = Math.floor((args.getPosition(null).y-10) / TheSize);
		if (x>=0 && x<TheWidth && y>=0 && y<TheHeight)
		{
			lastwx=x;
			lastwy=y;
			if (TheWorld1[y][x] == 1)
			{
				TheWorld1[y][x] = 0;
				TheWorld[y][x].Visibility="Collapsed";
				TheLife--;
			}
			else
			{
				TheWorld1[y][x] = 1;
				TheWorld[y][x].Visibility="Visible";
				TheLife++;
			}
		}
	}
	WorldDown = false;
}
function World_MouseEnter(sender,args)
{
	WorldDown = false;
}
function World_MouseLeave(sender,args)
{
	WorldDown = false;
}
function World_MouseMove(sender, args) 
{
	if (WorldDown)
	{
		x = Math.floor((args.getPosition(null).x-10) / TheSize);
		y = Math.floor((args.getPosition(null).y-10) / TheSize);
		if (x>=0 && x<TheWidth && y>=0 && y<TheHeight)
		{
			if (x!=lastwx || y!=lastwy)
			{
				lastwx=x;
				lastwy=y;
				if (TheWorld1[y][x] == 1)
				{
					TheWorld1[y][x] = 0;
					TheWorld[y][x].Visibility="Collapsed";
					TheLife--;
				}
				else
				{
					TheWorld1[y][x] = 1;
					TheWorld[y][x].Visibility="Visible";
					TheLife++;
				}
			}
		}
	}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
