function flame() {
if(game==0) document.candlepic.src="objects/candle.gif";
}

function movebodyup() {
head1Y-=2;head2Y-=2;bodyY-=2;baseY-=2;netY-=2;head_top-=2;candleY-=2;
bodyspan--;
move_body();
if((bodyspan>0)&&(stopping==0)) setTimeout("movebodyup()",10);
else {
bflag=1;
if(mode==2) run_unit();
}
}

function movebodydown() {
head1Y+=2;head2Y+=2;bodyY+=2;baseY+=2;netY+=2;head_top+=2;candleY+=2;
bodyspan--;
move_body();
if((bodyspan>0)&&(stopping==0)) setTimeout("movebodydown()",10);
else {
bflag=0;
if(mode==2) run_unit();
}
}

function movehead1down() {
head1Y+=2;netY+=2;candleY+=2;
head_top=head1Y;
head1span--;
move_part("head1",head1X,head1Y);
if(game==0) move_part("candle",candleX,candleY);
if(game==1) move_part("net",netX,netY);
if((head1span>0)&&(stopping==0)) setTimeout("movehead1down()",10);
else {
hflag=0;
if(mode==2) run_unit();
}
}

function movehead2up() {
head2Y-=2;netY-=2;candleY-=2;
head_top=head2Y;
head2span--;
move_part("head2",head2X,head2Y);
if(game==0) move_part("candle",candleX,candleY);
if(game==1) move_part("net",netX,netY);
if((head2span>0)&&(stopping==0)) setTimeout("movehead2up()",10);
else {
hflag=1;
if(mode==2) run_unit();
}
}

function movehead2down() {
head2Y+=2;netY+=2;candleY+=2;
head_top=head2Y;
head2span--;
move_part("head2",head2X,head2Y);
if(game==0) move_part("candle",candleX,candleY);
if(game==1) move_part("net",netX,netY);
if((head2span>0)&&(stopping==0)) setTimeout("movehead2down()",10);
else {showLayer("head1");hideLayer("head2");head1span=19;movehead1down();}
}

function move_body() {
move_part("head1",head1X,head1Y);
move_part("head2",head2X,head2Y);
move_part("body",bodyX,bodyY);
move_part("base",baseX,baseY);
if(game==0) move_part("candle",candleX,candleY);
if(game==1) move_part("net",netX,netY);
}

function move_all() {
move_part("head1",head1X,head1Y);
move_part("head2",head2X,head2Y);
move_part("body",bodyX,bodyY);
move_part("base",baseX,baseY);
move_part("tube",tubeX,tubeY);
move_part("wheels",wheelsX,wheelsY);
if(game==0) move_part("candle",candleX,candleY);
if(game==1) move_part("net",netX,netY);
}

function move_part(part,left,top){
left+=unitx;
top+=unity;
eval(doc_ref+part+style_ref+'.top=top');
eval(doc_ref+part+style_ref+'.left=left');
if(part=="candle") candle_check();
if((part=="net")&&((this_action==0)||(this_action==2))) up_check();
}

function hideLayer(layerName){eval(doc_ref+layerName+style_ref+'.visibility="hidden"');}
function showLayer(layerName){eval(doc_ref+layerName+style_ref+'.visibility="visible"');}

function moveLayer(layerName,left,top){
eval(doc_ref+layerName+style_ref+'.top=top');
eval(doc_ref+layerName+style_ref+'.left=left');
return false;
}

function new_program() {
yesno('Your program will be removed.<br>Continue?',4,270,"new_program1");
}

function new_program1() {
document.clearbut.src="buttons/clear2.gif";
setTimeout('new_program2()',200);
}
function new_program2() {
document.clearbut.src="buttons/clear.gif";
init();
for(i=0;i<=cells_total; i++) {
c_status[i]=6;
distances[i]="";
update_number(i);
if(i>0) eval('hideLayer("cell'+i+'")');
cellpic(i);
commands=0;
}
}


// **** run UNIT ****

function run_unit() {
if(count<commands) setTimeout("process()",100);
else moveLayer("pointer",-51,celly);
}

function process() {
this_action=c_status[count];
moveLayer("pointer",cellx+count*51,celly);
count++;
if(this_action==0) {
if(hflag==0) {head1span=19;movehead1up();}
else process();
}
if(this_action==1) {
if(hflag==1) {head2span=13;movehead2down();}
else process();
}
if(this_action==2) {
if(bflag==0) {bodyspan=19;movebodyup();}
else process();
}
if(this_action==3) {
if(bflag==1) {bodyspan=19;movebodydown();}
else process();
}
if(this_action==4) goleft1();
if(this_action==5) goright1();
}


//**** subs ****

function cellpic(cl) {
eval('document.cellpic'+cl+'.src="icons/'+iconpics[c_status[cl]]+'.gif"');
}

function limit() {
alert("You cannot add any more commands.")
hidecontrol();
}


//**** delete cell ****

function delete_cell() {
document.del.src="buttons/deli.gif";
if(cell<commands) setTimeout('delete_cell2()',200);
else hidecontrol();
}
function delete_cell2() {
for(i=cell;i<commands; i++) {
c_status[i]=c_status[i+1];
distances[i]=distances[i+1];
cellpic(i);
}
eval('hideLayer("cell'+commands+'")');
commands--;
hidecontrol();
for(i=0;i<(commands+1); i++) {update_number(i);}
}


//**** insert command ****

function insert_command(a) {
action=a;
if(cell==commands) {
commands++;
c_status[commands]=6;
distances[commands]="";
if(commands<cells_total) eval('showLayer("cell'+commands+'")');
}
c_status[cell]=action;
if(action<4) distances[cell]="";
cellpic(cell);
hidecontrol();
for(i=0;i>(commands+1); i++) {update_number(i);}
}

//**** insert cell ****

function insertright() {
document.insr.src="buttons/insri.gif";
if(commands<cells_total) setTimeout('insertion(cell+1)',200);
else limit();
}

function insertleft() {
document.insl.src="buttons/insli.gif";
if(commands<cells_total) setTimeout('insertion(cell)',200);
else limit();
}

function insertion(this_cell) {
commands++;
if(commands<cells_total) eval('showLayer("cell'+commands+'")');
for(i=commands;i>this_cell; i--) {
c_status[i]=c_status[i-1];
distances[i]=distances[i-1];
cellpic(i);
}
c_status[this_cell]=6;
distances[this_cell]="";
cellpic(this_cell);
hidecontrol();
for(i=0;i<(commands+1); i++) {
update_number(i);
}
}

function do_run() {
stopping=1;
pick=-1;
document.runbut.src="buttons/run2.gif";
setTimeout('do_run2()',200);
}
function do_run2() {
document.runbut.src="buttons/run.gif";
count=0;
init();
run_unit();
}

function headup() {
eval('document.hu'+mode+'.src="buttons/hui.gif"');
flame();
setTimeout('headup2()',200);
}
function headup2() {
eval('document.hu'+mode+'.src="buttons/huo.gif"');
if(mode==1) {if(hflag==0) {head1span=19;movehead1up();}}
else insert_command(0);
}

function headdown() {
eval('document.hd'+mode+'.src="buttons/hdi.gif"');
flame();
setTimeout('headdown2()',200);
}
function headdown2() {
eval('document.hd'+mode+'.src="buttons/hdo.gif"');
if(mode==1) {if(hflag==1) {head2span=13;movehead2down();}}
else insert_command(1);
}

function bodyup() {
eval('document.bu'+mode+'.src="buttons/bui.gif"');
flame();
setTimeout('bodyup2()',200);
}
function bodyup2() {
eval('document.bu'+mode+'.src="buttons/buo.gif"');
if(mode==1) {if(bflag==0) {bodyspan=19;movebodyup();}}
else insert_command(2);
}

function bodydown() {
eval('document.bd'+mode+'.src="buttons/bdi.gif"');
flame();
setTimeout('bodydown2()',200);
}
function bodydown2() {
eval('document.bd'+mode+'.src="buttons/bdo.gif"');
if(mode==1) {if(bflag==1) {bodyspan=19;movebodydown();}}
else insert_command(3);
}

function unitleft() {
eval('document.ul'+mode+'.src="buttons/uli.gif"');
flame();
setTimeout('unitleft2()',200);
}
function unitleft2() {
eval('document.ul'+mode+'.src="buttons/ulo.gif"');
if(mode==1) {if(move_flag==0) {count=1;distances[0]=1;goleft1();}}
else {
if(c_status[cell]==4) eval(form_ref+'=distances[cell]');
insert_command(4);
numbox();
}
}

function unitright() {
eval('document.ur'+mode+'.src="buttons/uri.gif"');
flame();
setTimeout('unitright2()',200);
}
function unitright2() {
eval('document.ur'+mode+'.src="buttons/uro.gif"');
if(mode==1) {if(move_flag==0) {count=1;distances[0]=1;goright1();}}
else {
if(c_status[cell]==5) eval(form_ref+'=distances[cell]');
insert_command(5);
numbox();
}
}

function numbox() {
if ((app=="I")&&(navigator.appVersion.indexOf("Macintosh")!=-1)) {;}
else moveLayer("number_input",cx-36,controly+70);
showLayer("number_input");
if (app=="I") document.numform.num.focus();
else document.number_input.document.numform.num.focus();
}

function insert_number(){
hideLayer("number_input");
eval('thisnumber='+form_ref);
eval(form_ref+'=""');
distances[cell]=thisnumber;
update_number(cell);
}

function update_number(tn) {
t=distances[tn]
f="&nbsp; ";
if(t>9) f="&nbsp;";
if(t>99) f="";

str='<p id="numstyle">'+f+distances[tn]+'</p>';
write_to_layer("num"+tn);
}


//**** actions ****

function reset() {
stopping=1;
document.resetbut.src="buttons/reset2.gif"
setTimeout('do_reset2()',200);
}
function do_reset2() {
if(game>-1) hideLayer("next");
hideLayer("hide");
document.resetbut.src="buttons/reset.gif";
init();
}

function help() {
document.helpbut.src="buttons/help2.gif";
closehelp();
setTimeout('help2()',200);
}
function help2() {
document.helpbut.src="buttons/help.gif";
showLayer("help"+mode);
}

function closehelp() {
hideLayer("help0");
hideLayer("help1");
hideLayer("help2");
}

function closebox() {
if(game>-1) hideLayer("challenge");
hideLayer("hide");
}


//**** show/hide controls ****

function command(c) {
cell=c;
cx=controlx+cell*52;
moveLayer("controlsback",cx,controly);
moveLayer("controls",cx+control_offset,controly+control_offset);
showLayer("controls");
showLayer("controlsback");
}

function close_window() {
yesno('Are you sure you want to quit?',4,4,"close_window2");
}

function close_window2() {
parent.window.close();
}


//**** set mode ****

function mode1() {
closehelp();
if(mode==2) yesno('UNIT will be reset.<br>Continue?',modeX-16,298,"mode1b");
else if(mode==0) {closebox();mode1b();}
}

function mode1b() {
closehelp();
stopping=1;
document.prac.src="buttons/prac2.gif";
document.prog.src="buttons/prog1.gif";
mode=1;
hideLayer("cell0");
hideLayer("buttons1");
showLayer("one_step");
setTimeout("new_program2()",500);
}

function mode2() {
closehelp();
if(mode==1) yesno('UNIT will be reset.<br>Continue?',modeX+118,298,"mode2b");
else if(mode==0) {closebox();mode2b();}
}

function mode2b() {
stopping=1;
document.prac.src="buttons/prac1.gif";
document.prog.src="buttons/prog2.gif";
mode=2;
showLayer("cell0");
showLayer("buttons1");
hideLayer("one_step");
setTimeout("init()",500);
}


//**** yes/no ****

function yesno(message,ynx,yny,thisfunc) {
func=thisfunc;
showLayer("hide");
str=messagebit1+message+messagebit2;
if(app=="I") {
this_row=yesno_table.rows(0);
this_row.cells(0).innerHTML = str;
}
else write_to_layer("yesno");
moveLayer("yesno",ynx,yny);
}

function no(){
hideLayer("hide");
moveLayer("yesno",-500,-500);
}

function yes(){
hideLayer("hide");
moveLayer("yesno",-500,-500);
eval(func+"()");
}

//**** help ****

function makehelp() {

h1bit='<div align="center"><center><table border="0" cellpadding="4" cellspacing="0"><tr><td rowspan="';
h2bit='" valign="top"><p align="center"><strong><font face="Arial"><big>';
h3bit='</big></font></strong></p><p>&nbsp;</p><p id="helpstyle">Press ';
h4bit='</tr><tr><td valign="bottom"><form><div align="center"><center><p><input type="button" value="ok" name="q" onclick="closehelp()"></p></center></div></form></td><td align="right"><p id="helpstyle">';

n=0;

helpcont[0]='<table border="1" cellpadding="4" cellspacing="0" bgcolor="#F2F2FF"><tr><td>';
helpcont[0]+='<p id="helpstyle">Start with <strong>practice mode</strong>. This lets you control UNIT step by step.<br><br>When you know how the controls work, you can try <strong>program mode</strong>.';
helpcont[0]+='<form><div align="center"><center><p><input type="button" value="ok" name="q" onclick="closehelp()"></p></center></div></form></td></tr></table>';

makelayer('help0','hidden',60,20,400,100,3000);
w(helpcont[0]);
endbit();

n=1;

helpcont[1]='<table border="1" cellpadding="4" cellspacing="0" bgcolor="#F2F2FF"><tr><td>'+h1bit+'2'+h2bit+'Practice Mode'+h3bit;
helpcont[1]+='<img src="buttons/reset.gif" width="39" height="32" align="absmiddle"> to put UNIT at the start.</p>';
helpcont[1]+='</td><td rowspan="3">&nbsp;&nbsp; </td><td align="right"><p id="helpstyle">move head up and down</td>';
helpbit("huo");helpbit("hdo");
helpcont[1]+='</tr><tr><td align="right"><p id="helpstyle">move body up and down</td>'
helpbit("buo");helpbit("bdo");
helpcont[1]+=(h4bit+'move UNIT left and right</td>');
helpbit("ulo");helpbit("uro");
helpcont[1]+='</tr></table></center></div></td></tr></table>';

makelayer('help1','hidden',60,20,400,100,3000);
w(helpcont[1]);
endbit();

n=2;

helpcont[2]='<table border="1" cellpadding="4" cellspacing="0" bgcolor="#F2F2FF"><tr><td>'+h1bit+'4'+h2bit+'Program Mode'+h3bit;
helpcont[2]+='<img src="buttons/reset.gif" width="39" height="32" align="absmiddle"> to put UNIT at the start.</p>';
helpcont[2]+='<p id="helpstyle">Press <img src="buttons/run.gif" width="43" height="24" align="absmiddle"> to run your program.</p>';
helpcont[2]+='<td rowspan="5">&nbsp;&nbsp; </td><td align="right"><p id="helpstyle">move head up and down</td>';
helpbit("huo");helpbit("hdo");
helpcont[2]+='</tr><tr><td align="right"><p id="helpstyle">move body up and down</td>'
helpbit("buo");helpbit("bdo");
helpcont[2]+='</tr><tr><td align="right"><p id="helpstyle">move UNIT left and right</td>'
helpbit("ulo");helpbit("uro");
helpcont[2]+='</tr><tr><td align="right"><p id="helpstyle">insert a new command<br>to the left or right</td>';
helpbit("inslo");helpbit("insro");
helpcont[2]+=(h4bit+'delete a command</td>');
helpcont[2]+='<td><img src="buttons/delo.gif" width="51" height="51"></td>';
helpcont[2]+='<td></td></tr></table></center></div></td></tr></table>';

makelayer('help2','hidden',60,20,400,100,3000);
w(helpcont[2]);
endbit();
}


function helpbit(gif) {
helpcont[n]+=('<td><img src="buttons/'+gif+'.gif" width="51" height="51"></td>');
}

// **************

function write_to_layer(layer) {
if (app=="N") {with (document.layers[layer].document) {open();write(str);close();}}
else
if (app=="N6") {
eval(doc_ref+layer+'").innerHTML=str');
}
else document.all[layer].innerHTML=str;
}

function endbit() {
if (app=="N") w('</layer>');
else w('</div>');
}

function makelayer(name,vis,left,top,width,height,z) {
if(app=="N") w('<layer id="'+name+'" position=absolute visibility='+vis+' left='+left+' top='+top+' width='+width+' height='+height+' z-index='+z+';>');
else w('<div id="'+name+'" STYLE="position:absolute;visibility:'+vis+';left:'+left+';top:'+top+';width:'+width+';height:'+height+';z-index:'+z+';">');
}

function w(cont) {document.write(cont);}

