var gImageSize = 14;
var gCellSize = 16;
var gGrid = new DGrid(46, 9, 85);
var gStateMachine = new DStateMachine();
function StartFunStuff()
{
gGrid.Initialise();
gStateMachine.Initialise();
}
var fadeImage = new Array(5);
var selectImage = new Array(5);
for (i=0; i<5; i++)
{
fadeImage[i] = new Image(14, 14);
fadeImage[i].src = i+"fade.jpg"
selectImage[i] = new Image(14, 14);
selectImage[i].src = i+"select.jpg"
}
var IE = document.all?true:false
function getAbsoluteX(obj)
{
var xPos = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
xPos += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
xPos += obj.x;
return xPos;
}
function getAbsoluteY(obj)
{
var yPos = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
yPos += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
yPos += obj.y;
return yPos;
}
function bMI(idx)
{
currentCell = idx;
eval('document.images.image'+idx+'.src = "'+gGrid.mBox[idx].mColour+'select.jpg"');
}
function bMO(idx)
{
if (currentCell==idx)
{
currentCell = -1;
eval('document.images.image'+idx+'.src = fadeImage['+gGrid.mBox[idx].mColour+'].src');
}
}
function bClick(idx)
{
gStateMachine.Clicked(idx);
}
function DBox(idx, x, y)
{
this.mIdx = idx;
this.mColour = idx%5;
this.mX = x;
this.mY = y;
this.mSize = 16;
this.mImageSize = 14;
if (typeof(DBox_prototype_created) == 'undefined')
{
DBox_prototype_created = true;
function Initialise()
{
this.SetPosition(this.mSize*this.mX, this.mSize*this.mY);
}
DBox.prototype.Initialise = Initialise
function SetGridPosition(x, y)
{
this.mX = x;
this.mY = y;
this.SetPosition(this.mSize*this.mX, this.mSize*this.mY);
}
DBox.prototype.SetGridPosition = SetGridPosition
function SetPosition(x, y)
{
var element = document.getElementById('box'+this.mIdx);
var ypos = gGrid.mTop + y;
var xpos = gGrid.mLeft + x;
element.style.top=IE ? ypos : ypos;
element.style.left=IE ? xpos : xpos;
}
DBox.prototype.SetPosition = SetPosition
function CreateHTML()
{
document.write('<DIV name="box'+this.mIdx+'" id="box'+this.mIdx+'" style="position:absolute; left:-100px; top:-100px;">');
document.write('<IMG name="image'+this.mIdx+'" id="image'+this.mIdx+'" onmouseover="bMI('+this.mIdx+')" onmouseout="bMO('+this.mIdx+')" onClick="bClick('+this.mIdx+')" src="'+this.mColour+'fade.jpg" width='+this.mImageSize+' height='+this.mImageSize+' border=0>');
document.writeln('</DIV>');
}
DBox.prototype.CreateHTML = CreateHTML
}
}
function DGrid(width, height, boxes)
{
this.mWidth = width;
this.mHeight = height;
this.mNumBoxes = boxes;
this.mBox = new Array(boxes);
this.mMap = new Array(width*height);
this.mLeft = 0;
this.mTop = 0;
for (i=0; i<this.mWidth*this.mHeight; i++)
{
this.mMap[i] = -1;
}
for (i=0; i<boxes; i++)
{
var xPos, yPos;
do
{
xPos = Math.floor(Math.random()*this.mWidth);
yPos = Math.floor(Math.random()*this.mHeight);
}
while (this.mMap[xPos+yPos*this.mWidth]!=-1)
this.mBox[i] = new DBox(i, xPos, yPos);
this.mMap[xPos+(yPos*this.mWidth)] = i;
}
if (typeof(DGrid_prototype_created) == 'undefined')
{
DGrid_prototype_created = true;
function Initialise()
{
for (i=0; i<this.mNumBoxes; i++)
{
this.mLeft = getAbsoluteX(document.getElementById('grid')); 
this.mTop = getAbsoluteY(document.getElementById('grid'));
this.mBox[i].Initialise();
}
}
DGrid.prototype.Initialise = Initialise
function CreateHTML()
{
for (i=0; i<this.mNumBoxes; i++)
{
this.mBox[i].CreateHTML()
}
}
DGrid.prototype.CreateHTML = CreateHTML
function SetCellState(x, y, val)
{
this.mMap[x+ y*this.mWidth]=val;
}
DGrid.prototype.SetCellState = SetCellState
function IsCellClear(x, y)
{
if (this.mMap[x+ y*this.mWidth]==-1)
return 1;
else
return 0;
}
DGrid.prototype.IsCellClear = IsCellClear
}
}
function DStateMachine()
{
this.mState = 0; 
this.mUpdateTime = 33; 
this.mRandomMovementState = new DRandomMovementState();
this.mFormLogo = new DFormLogo();
this.mbTransition = 0;
this.mTarget = 0;
if (typeof(DStateMachine_prototype_created) == 'undefined')
{
DStateMachine_prototype_created = true;
function Initialise()
{
this.mState = 1; 
this.mRandomMovementState.Initialise();
window.setTimeout("gStateMachine.Process()", this.mUpdateTime);
}
DStateMachine.prototype.Initialise = Initialise;
function Process()
{
switch (this.mState)
{
case 0: 
break;
case 1: 
this.mRandomMovementState.Process();
break;
case 2: 
this.mFormLogo.Process();
break;
default:
alert("");
}
if (this.mbTransition==1)
{
var hasEnded;
switch (this.mState)
{
case 0: 
hasEnded=1;
break;
case 1: 
hasEnded = this.mRandomMovementState.IsEnded();
break;
case 2: 
hasEnded = this.mFormLogo.IsEnded();
break;
default:
alert("");
}
if (hasEnded==1)
{
this.mState = this.mTarget;
this.mbTransition=0;
switch (this.mState)
{
case 0: 
break;
case 1: 
this.mRandomMovementState.Initialise();
break;
case 2: 
this.mFormLogo.Initialise();
break;
default:
alert("");
}
}
}
window.setTimeout("gStateMachine.Process()", this.mUpdateTime);
}
DStateMachine.prototype.Process = Process;
function Clicked(idx)
{
if (!this.mbTransition)
{
if (this.mState==1)
{
this.mRandomMovementState.End();
this.mbTransition=1;
this.mTarget=2;
}
if (this.mState==2)
{
this.mFormLogo.End();
this.mbTransition=1;
this.mTarget=1;
}
}
}
DStateMachine.prototype.Clicked = Clicked;
}
}
function DRandomMovementState()
{
this.mAnythingMoving=0;
this.mBoxMoving=new Array();
this.mTargetX=new Array();
this.mTargetY=new Array();
this.mPos=new Array();
this.mMoveSpeed=0.1;
this.mNumMoving=20;
this.mBoxesMoving=new Array(gGrid.mNumBoxes);
this.mbEnding=0;
if (typeof(DRandomMovementState_prototype_created) == 'undefined')
{
DRandomMovementState_prototype_created = true;
function Initialise()
{
this.mAnythingMoving=1;
this.mbEnding=0;
for (i=0; i<gGrid.mNumBoxes; i++)
this.mBoxesMoving[i] = 0;
for (i=0; i<this.mNumMoving; i++)
{
this.mBoxMoving[i] = -1;
this.mPos[i] = -i*this.mMoveSpeed;
}
}
DRandomMovementState.prototype.Initialise = Initialise;
function Process()
{
for (i=0; i<this.mNumMoving; i++)
{
if (this.mPos[i]>=0.0)
{
if (this.mBoxMoving[i]!=-1)
{
this.mPos[i] += this.mMoveSpeed;
if (this.mPos[i]>=1.0)
{
gGrid.SetCellState(gGrid.mBox[this.mBoxMoving[i]].mX,
gGrid.mBox[this.mBoxMoving[i]].mY, -1);
gGrid.mBox[this.mBoxMoving[i]].SetGridPosition(this.mTargetX[i], this.mTargetY[i]);
this.mBoxesMoving[this.mBoxMoving[i]] = 0;
this.mBoxMoving[i] = -1;
}
else
{
var cX = gGrid.mBox[this.mBoxMoving[i]].mX;
var cY = gGrid.mBox[this.mBoxMoving[i]].mY;
var newX = this.mTargetX[i];
var newY = this.mTargetY[i];
var ypos = (cY+(newY-cY)*this.mPos[i])*16;
var xpos = (cX+(newX-cX)*this.mPos[i])*16;
gGrid.mBox[this.mBoxMoving[i]].SetPosition(xpos, ypos);
}
}
else
{
if (this.mbEnding==0)
{
var isCellClear;
do
{
do
{
this.mBoxMoving[i] = Math.floor(Math.random()*gGrid.mNumBoxes);
}
while (this.mBoxesMoving[this.mBoxMoving[i]]==1);
var rand = Math.random();
var leftorright=0;
var upordown=0;
for (j=0; j<4&&leftorright==0&&upordown==0; j++)
{
if (rand<0.25 && gGrid.mBox[this.mBoxMoving[i]].mX!=0) {leftorright=-1;upordown=0;}
else if (rand>0.25 && rand<0.5 && gGrid.mBox[this.mBoxMoving[i]].mY!=(gGrid.mHeight-1)) {leftorright=0;upordown=1;}
else if (rand>0.5 && rand<0.75 && gGrid.mBox[this.mBoxMoving[i]].mX!=(gGrid.mWidth-1)) {leftorright=1;upordown=0;}
else if (rand>0.75 && rand<=1.0 && gGrid.mBox[this.mBoxMoving[i]].mY!=0) {leftorright=0;upordown=-1;}
rand+=0.25;
if (rand>1.0) rand-=1.0;
}
this.mTargetX[i] = gGrid.mBox[this.mBoxMoving[i]].mX+leftorright;
this.mTargetY[i] = gGrid.mBox[this.mBoxMoving[i]].mY+upordown;
isCellClear = gGrid.IsCellClear(this.mTargetX[i], this.mTargetY[i]);
}
while (leftorright==0 && upordown==0 || isCellClear==0)
this.mBoxesMoving[this.mBoxMoving[i]] = 1;
this.mPos[i] = 0.0;
gGrid.SetCellState(this.mTargetX[i], this.mTargetY[i], this.mBoxMoving[i]);
}
}
}
else
{
this.mPos[i] += this.mMoveSpeed;
}
}
}
DRandomMovementState.prototype.Process = Process;
function End()
{
this.mbEnding = 1;
}
DRandomMovementState.prototype.End = End;
function IsEnded()
{
for (i=0; i<this.mNumMoving; i++)
{
if (this.mBoxesMoving[i]==1)
return 0;
}
return 1;
}
DRandomMovementState.prototype.IsEnded = IsEnded;
}
}
function DFormLogo()
{
this.mbEnding=0;
if (typeof(DFormLogo_prototype_created) == 'undefined')
{
this.mBoxesMoving = 0;
this.mLogoStr = "\
..............................................\
...............................1.......1......\
.......................................1......\
.111.111..11.111.111.11..11111.1.111.111.111..\
.1.1.1...1.1.1.1.1.1.11..1.1.1.1.1.1.1.1..1...\
.111.1...111.1.1.111.111.1.1.1.1.1.1.111.111..\
...................1..........................\
.................111..........................";
this.mLogoDecode = new Array(gGrid.mWidth*gGrid.mHeight);
this.mBoxTarget = new Array(gGrid.mNumBoxes);
this.mDistances = new Array(gGrid.mNumBoxes);
this.mPossTarget = new Array(gGrid.mNumBoxes);
this.done = 0;
this.cellX=0;
this.cellY=0;
this.mover=0;
this.processMode;
DFormLogo_prototype_created = true;
function Initialise()
{
this.done = 0;
this.cellX=0;
this.cellY=0;
this.mover=0;
this.processMode=0;
for (b=0; b<gGrid.mWidth*gGrid.mHeight; b++)
{
if (this.mLogoStr.charAt(b)=='1')
{ this.mLogoDecode[b] = 1;}
else
{ this.mLogoDecode[b] = 0;}
}
this.RandomWalk();
}
DFormLogo.prototype.Initialise = Initialise;
function RandomWalk()
{
var boxIdx = 0;
var boxTargetX = new Array(gGrid.mNumBoxes);
var boxTargetY = new Array(gGrid.mNumBoxes);
var dist = new Array(gGrid.mNumBoxes);
for (b=0; b<gGrid.mNumBoxes; b++)
{
var grididx = gGrid.mBox[b].mY*gGrid.mWidth+gGrid.mBox[b].mX;
if (this.mLogoDecode[grididx]>0)
{
this.mLogoDecode[grididx] = 0;
this.mBoxTarget[b] = grididx;
}
else
{
this.mBoxTarget[b] = -1;
}
}
for (b=0; b<gGrid.mWidth*gGrid.mHeight; b++)
{
if (this.mLogoDecode[b]==1)
{
var dontcarryon = false;
do
{
if (this.mBoxTarget[boxIdx]==-1) 
{
this.mBoxTarget[boxIdx] = b;
dontcarryon = true;
}
boxTargetX[boxIdx] = this.mBoxTarget[boxIdx]%gGrid.mWidth;
boxTargetY[boxIdx] = Math.floor(this.mBoxTarget[boxIdx]/gGrid.mWidth);
var dx = gGrid.mBox[boxIdx].mX - boxTargetX[boxIdx];
var dy = gGrid.mBox[boxIdx].mY - boxTargetY[boxIdx];
dist[boxIdx] = dx*dx+dy*dy;
boxIdx++;
}
while (dontcarryon==false) 
}
}
var dx12, dy12, dx21, dy21, temp, tempX, tempY, dist12, dist21;
for (b=0; b<5000; b++)
{
var box1 = Math.floor(Math.random()*gGrid.mNumBoxes);
var box2;
do
{
box2 = Math.floor(Math.random()*gGrid.mNumBoxes);
}
while (box2==box1);
dx12 = gGrid.mBox[box1].mX - boxTargetX[box2];
dy12 = gGrid.mBox[box1].mY - boxTargetY[box2];
dx21 = gGrid.mBox[box2].mX - boxTargetX[box1];
dy21 = gGrid.mBox[box2].mY - boxTargetY[box1];
dist12 = dx12*dx12 + dy12*dy12;
dist21 = dx21*dx21 + dy21*dy21;
if (dist[box2]+dist[box1]>dist12+dist21)
{
temp = this.mBoxTarget[box1];
tempX = boxTargetX[box1];
tempY = boxTargetY[box1];
this.mBoxTarget[box1] = this.mBoxTarget[box2];
boxTargetX[box1] = boxTargetX[box2];
boxTargetY[box1] = boxTargetY[box2];
this.mBoxTarget[box2] = temp;
boxTargetX[box2] = tempX;
boxTargetY[box2] = tempY;
dist[box1] = dist12;
dist[box2] = dist21;
}
}
}
DFormLogo.prototype.RandomWalk = RandomWalk;
var movingIdx=new Array(gGrid.mNumBoxes);
var movingDistance=new Array(gGrid.mNumBoxes);
var movingPosition=new Array(gGrid.mNumBoxes);
function Process()
{
if (this.done==0)
{
if (this.cellX<gGrid.mWidth+gGrid.mHeight+6*this.processMode)
{
var cellidx;
for (d=0; d<15; d++)
{
cellidx = this.cellY*gGrid.mWidth+this.cellX;
if ((this.cellX<gGrid.mWidth && this.cellY<gGrid.mHeight) && gGrid.mMap[cellidx]!=-1)
{
if (this.processMode==0)
{
var box = gGrid.mMap[cellidx];
movingIdx[this.mover] = box;
var dx = gGrid.mBox[box].mX - (this.mBoxTarget[box]%gGrid.mWidth);
var dy = gGrid.mBox[box].mY - Math.floor(this.mBoxTarget[box]/gGrid.mWidth);
movingDistance[this.mover] = Math.sqrt(dx*dx+dy*dy);
movingPosition[this.mover] = 0.0;
this.mover++;
}
else if (this.processMode==1)
{
var box = gGrid.mMap[cellidx];
if (box>-1)
{
gGrid.mBox[box].mColour=1;
eval('document.images.image'+box+'.src = fadeImage['+gGrid.mBox[box].mColour+'].src');
}
}
}
if (this.processMode==1 && this.cellX-4>0 && this.cellX-4<gGrid.mWidth)
{
var cellidx2 = cellidx-4;
var box2 = gGrid.mMap[cellidx2];
if (box2>-1)
{
eval('document.images.image'+box2+'.src = selectImage['+gGrid.mBox[box2].mColour+'].src');
}
}
if (this.processMode==1 && this.cellX-6>0 && this.cellX-6<gGrid.mWidth)
{
var cellidx3 = cellidx-6;
var box3 = gGrid.mMap[cellidx3];
if (box3>-1)
{
eval('document.images.image'+box3+'.src = fadeImage['+gGrid.mBox[box3].mColour+'].src');
}
}
this.cellY++;
this.cellX--;
if (this.cellY>=gGrid.mHeight)
{
this.cellX+=this.cellY+1;
this.cellY=0;
}
if (this.cellX<0)
{
this.cellX+=this.cellY+1;
this.cellY=0;
}
}
}
else
{
this.processMode++;
this.cellX=0;
this.cellY=0;
if (this.processMode==2)
this.done=1;
}
var anythingMoving=false
for (b=0; b<this.mover; b++)
{
if (movingPosition[b]<1.0)
{
anythingMoving = true;
movingPosition[b] += 0.25/Math.sqrt(movingDistance[b]);
var cX = gGrid.mBox[movingIdx[b]].mX;
var cY = gGrid.mBox[movingIdx[b]].mY;
var newX = this.mBoxTarget[movingIdx[b]]%gGrid.mWidth;
var newY = Math.floor(this.mBoxTarget[movingIdx[b]]/gGrid.mWidth);
if (movingPosition[b]>=1.0)
{
movingPosition[b] = 1.0;
gGrid.mBox[movingIdx[b]].mX = newX;
gGrid.mBox[movingIdx[b]].mY = newY;
gGrid.mMap[newX+newY*gGrid.mWidth] = movingIdx[b];
}
var ypos = (cY+(newY-cY)*movingPosition[b])*16;
var xpos = (cX+(newX-cX)*movingPosition[b])*16;
gGrid.mBox[movingIdx[b]].SetPosition(xpos, ypos);
}
}
}
}
DFormLogo.prototype.Process = Process;
function End()
{
this.mbEnding = 1;
}
DFormLogo.prototype.End = End;
function IsEnded()
{
if (this.done==1)
{
for (y=0; y<gGrid.mHeight; y++)
{
for (x=0; x<gGrid.mWidth; x++)
{
gGrid.SetCellState(x, y, -1);
}
}
for (b=0; b<gGrid.mNumBoxes; b++)
{
gGrid.SetCellState(gGrid.mBox[b].mX, gGrid.mBox[b].mY, b);
}
for (b=0; b<gGrid.mNumBoxes; b++)
{
gGrid.mBox[b].mColour = b%5;
eval('document.images.image'+b+'.src = fadeImage['+gGrid.mBox[b].mColour+'].src');
}
return 1;
}
return 0;
}
DFormLogo.prototype.IsEnded = IsEnded;
}
}
