
var seq = new Array();
var playback = false;
var pos = 0;

function loadImages()
{
pic1= new Image(48,48); 
pic1.src="/sml/colmem/red-light.png"; 

pic2= new Image(48,48); 
pic2.src="/sml/colmem/yellow-light.png"; 

pic3= new Image(48,48); 
pic3.src="/sml/colmem/green-light.png"; 

pic4= new Image(48,48); 
pic4.src="/sml/colmem/blue-light.png"; 
}

function click(col)
{
if (!playback)
{	
	flash(col);

	if (seq[pos] != col)
	{
		gameOver();
	}
	else if (seq.length-1 == pos)
	{
		gameProgress();
	}
	else
	{
		pos++;
	}
}
}

function flash(col)
{
setTimeout("swapimage"+col+"Light()", 0);
setTimeout("swapimage"+col+"()", 400);
}

function clickRed()
{
click("Red");
}

function clickYellow()
{
if (!playback)
{
click("Yellow");
}
}

function clickGreen()
{
if (!playback)
{
click("Green");
}
}

function clickBlue()
{
if (!playback)
{
click("Blue");
}
}

function gameStart()
{
seq = new Array();
gameProgress();
}

function showSequence()
{
playback = true;
document.getElementById("currentTxt").innerHTML = "Remember...";
document.getElementById("lengthTxt").innerHTML = seq.length;
setTimeout("showSeqNo(0);", 1200);
}

function showSeqNo(p)
{
flash(seq[p]);
if (seq.length-1 == p)
{
setTimeout("userStart();", 750);
playback = false;
}
else
{
q = p+1;
setTimeout("showSeqNo("+q+");", 750);
}
}

function userStart()
{
document.getElementById("currentTxt").innerHTML = "Key in sequence";
}

function gameProgress()
{
next = Math.round(4*Math.random());
if (next == 0)
{
seq[seq.length] = "Red";
}
else if (next == 1)
{
seq[seq.length] = "Yellow";
}
else if (next == 2)
{
seq[seq.length] = "Green";
}
else if (next == 3)
{
seq[seq.length] = "Blue";
}
else
{
seq[seq.length] = "Red";
}

showSequence();
pos = 0;
}

function gameOver()
{
document.getElementById("currentTxt").innerHTML = "<a href='javascript:gameStart()'>Start Game</a>";
}

function swapimageRed()
{
swapimage("red", "/sml/colmem/red.png");
}
function swapimageRedLight()
{
swapimage("red", "/sml/colmem/red-light.png");
}
function swapimageYellow()
{
swapimage("yellow", "/sml/colmem/yellow.png");
}
function swapimageYellowLight()
{
swapimage("yellow", "/sml/colmem/yellow-light.png");
}
function swapimageGreen()
{
swapimage("green", "/sml/colmem/green.png");
}
function swapimageGreenLight()
{
swapimage("green", "/sml/colmem/green-light.png");
}
function swapimageBlue()
{
swapimage("blue", "/sml/colmem/blue.png");
}
function swapimageBlueLight()
{
swapimage("blue", "/sml/colmem/blue-light.png");
}
function swapimage(name, src)
{
document.getElementById(name).src = src;
}

