document.bgColor='#BBFF77';

carry=0;

completed=0;

flag=-1;

function start() {
setTimeout('rightwing(5)',400);
}

move=0;

checkflag=false;
resetflag=false;
helpflag=false;
hideflag=false;
quitflag=false;

this_word=0;

number_used=new Array(total_items+1);

text_used=new Array(total_items+1);
for(i=1;i<(total_items+1); i++) {
text_used[i]=0;
}

app="I";
if(navigator.appName=="Netscape") app="N";
if(app=="N" && parseInt(navigator.appVersion)>4) app="N6";
mac=1;if(navigator.appVersion.indexOf("Macintosh")==-1) mac=0;

if (app=="N")  {
doc_ref='document.layers["';
style_ref='"]';
}
else {
if (app=="N6") {
doc_ref='document.getElementById("';
style_ref='").style';
}
else {
doc_ref='document.all["';
style_ref='"].style';
}
}

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


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


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


if (app=="I") {
document.ondragstart = drag_initIE;
document.onmousedown = start_dragIE;
document.onmousemove = dragIE;
document.onmouseup = end_dragIE;
}

if (app=="N") {
document.captureEvents(Event.MOUSEUP|Event.MOUSEDOWN);
document.onmousedown=start_dragNS;
document.onmouseup=end_dragNS;
}

if (app=="N6") {
window.onmousedown=start_dragNS;
window.onmouseup=end_dragNS;
}

function drag_initIE() {
event.returnValue=false;
}

function dragIE() {
if(drag>-1) moveLayer(mapbit,window.event.x-offsetX,window.event.y-offsetY);
lx=window.event.x+20;ly=window.event.y;
if(mac==1) {lx+=document.body.scrollLeft;ly+=document.body.scrollTop;}
move_eyes(lx,ly);
}

function dragNS(e) {
if(drag>-1) moveLayer(mapbit,e.pageX-offsetX,e.pageY-offsetY);
}

function end_dragNS(e) {
if (app=="N") {
document.onmousemove=0; 
document.releaseEvents(Event.MOUSEMOVE);
}
else window.onmousemove=0;
if(drag>-1) {
dragpointX=e.pageX;
dragpointY=e.pageY;
thisitem=drag;
drag=-1;
drop_check();
}
return false;
}

function start_dragIE() {
drag=click_check(window.event.x,window.event.y);
}

function start_dragNS(e) {
drag=click_check(e.pageX,e.pageY);
if (drag>-1){
if (app=="N") {
document.captureEvents(Event.MOUSEMOVE);
document.onmousemove=dragNS;
}
else window.onmousemove=dragNS;
return false;
}
}

function click_check(clickX,clickY) {
if(app=="N") {
if(checkflag==true) {do_check();return;}
if(resetflag==true) {do_reset();return;}
if(helpflag==true) {do_help();return;}
if(hideflag==true) {hidetext();return;}
if(quitflag==true) {window.close();return;}
}
if(completed==1) return;
if(this_word>0) {
drag=this_word;
if(text_used[this_word]>0) number_used[text_used[this_word]]=0;
text_used[this_word]=0;
mapbit='block'+this_word;
eval(doc_ref+'block'+this_word+style_ref+'.zIndex=1000');
leftedge=parseInt(eval(doc_ref+mapbit+style_ref+'.left'));
topedge=parseInt(eval(doc_ref+mapbit+style_ref+'.top'));
offsetX=clickX-leftedge;
offsetY=clickY-topedge;
return i;
}
return false;
}

function end_dragNS(e) {
if (app=="N") {
document.onmousemove=0; 
document.releaseEvents(Event.MOUSEMOVE);
}
else window.onmousemove=0;
if(drag>-1) {
dragpointX=e.pageX;
dragpointY=e.pageY;
thisitem=drag;
drag=-1;
drop_check();
}
return false;
}

function end_dragIE() {
if(drag>-1) {
dragpointX=window.event.x;
dragpointY=window.event.y;
thisitem=drag;
drag=-1
drop_check();
}
}



function drop_check() {

eval(doc_ref+mapbit+style_ref+'.zIndex=110');

leftedge=parseInt(eval(doc_ref+mapbit+style_ref+'.left'));
topedge=parseInt(eval(doc_ref+mapbit+style_ref+'.top'));

ok=0;
for(i=1;i<(total_items+1); i++) {
ttop=i*35-25;
if((leftedge>250)&&(leftedge<330)&&(topedge>(ttop-12))&&(topedge<(ttop+23))) {
ntop=ttop;
if(number_used[i]>0) ok=-1;
else ok=i;
}
}
if(ok==-1)  moveLayer(mapbit,410,ntop);
if(ok>0) {
moveLayer(mapbit,290,ntop);
number_used[ok]=this_word;
text_used[this_word]=ok;
}
checklist();
}


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

function checklist() {
finished=0;
for(i=1;i<(total_items+1); i++) {
if(text_used[i]==i) finished++;
}
if(finished==total_items) {
rightwing(5);
leftwing(5);
displayText('<br>'+endmessage,0);
completed=1;
}
}

function do_check() {
finished=0;
for(i=1;i<(total_items+1); i++) {
if(text_used[i]==i) finished++;
else if(text_used[i]>0) {
moveLayer('block'+i,tpos+50,text_used[i]*35-25);
number_used[text_used[i]]=0;
text_used[i]=0;
}
}
finitem=" items";
if(finished==1) finitem=" item";
rightwing(5);
displayText("<br>You have put "+finished+finitem+" in the right order.",1);
}

function displayText(text,okbutton) {
str=messa+text;
if(okbutton==1)str=messa+text+messb;
write_to_layer("textitem");
showLayer("textitem");
}


helpmessage="Drag the items onto the numbers in the right order.<br>Use the <B>check</B> button to see how well you are doing.";
eyex=127;eyey=318;

tpos=304;if(app=="N") tpos=310;
if(mac==1) tpos=298;

messa='<p align="center">';

messb='<br>&nbsp;<br><a href="javascript:hidetext()" onMouseover="hideflag=1" onMouseout="hideflag=0"><img src="../images/ok.gif" border="0" width="20" height="16"></a>';

for(i=1;i<(total_items+1); i++) {
text_used[i]=0;
number_used[i]=0;
}

bit='<table border="0" cellpadding="0" cellspacing="0" width="240" bgcolor="#D9FFFF"><tr><td align="center"><H1>'+titlemessage+'</td></tr></table>';


makelayer('back','visible',0,0,screen.width,572,0);
w('<table border="0" cellpadding="0" cellspacing="0" background="../images/back.jpg" height="572" width="'+screen.width+'"><tr><td>&nbsp; </td></tr></table>');
endbit();

makelayer('owl','visible',55,278,138,172,60);
w('<img src="../images/owl.gif" WIDTH="138" HEIGHT="172">');
endbit();

makelayer('rwing','visible',154,278,95,154,50);
w('<img name="rwing" src="../images/rwing0.gif" WIDTH="95" HEIGHT="154">');
endbit();

makelayer('lwing','visible',0,278,95,154,50);
w('<img name="lwing" src="../images/lwing0.gif" WIDTH="95" HEIGHT="154">');
endbit();

makelayer('eye','visible',84,303,82,28,60);
w('<img name="eyes" src="../images/eyes4.gif" WIDTH="82" HEIGHT="28">');
endbit();

makelayer('tree','visible',176,0,219,562,40);
w('<img src="../images/trunk.gif" width="219" height="562">');
endbit();

makelayer('branch','visible',55,429,138,21,40);
w('<img src="../images/branch.gif" width="138" height="21">');
endbit();

makelayer('title','visible',0,10,240,20,35);
w(bit);
endbit();

//-------------------------

randomise();
loopl=-1;loopr=-1;

for(i=1;i<(total_items+1); i++) {
j=text_used[i];


numb='<SPAN Class="numstyle">&nbsp;';
if(i<10) numb+='&nbsp; ';
numb=numb+i+'&nbsp; </SPAN>';

ttop=i*35-25;
tleft=500+((i+1)%2)*100;

makelayer('num'+i,'visible',256,ttop,80,26,100);

w('<table border="0" cellpadding="4" cellspacing="0" bgcolor="#804040"><tr><td>'+numb+'</td></tr></table>');
endbit();

makelayer('block'+(j+1),'visible',tleft,ttop,54,26,110);
w('<table border="0" cellpadding="4" cellspacing="0" bgcolor="#804040"><tr><td><a href="javascript:void()" onMouseover="this_word='+(j+1)+'" onMouseout="this_word=0"><span class="wordstyle">'+text_items[j]+'</a></span></td></tr></table>');

endbit();

text_used[i]=0;
}

sp6='<br><img src="../images/blank.gif" width="10" height="6"><br>';

sp50='<br><img src="../images/blank.gif" width="10" height="170"><br>';


makelayerwithcolour('textitem','visible',60,70,164,144,155,'#FFFFFF');
w(messa+startmessage+messb);
endbit();

makelayer('buttons1','visible',0,70,20,20,150);
w('<a href="javascript:do_help()" onMouseover="helpflag=true" onMouseout="helpflag=false"><img src="../images/help.gif" width="64" height="42" border="0"></a>'+sp6+'<a href="javascript:do_check()" onMouseover="checkflag=true" onMouseout="checkflag=false"><img src="../images/check.gif" width="64" height="42" border="0"></a>'+sp6+'<a href="javascript:do_reset()" onMouseover="resetflag=true" onMouseout="resetflag=false"><img src="../images/reset.gif" width="64" height="42" border="0"></a>'+sp50+'<a href="javascript:window.close()" onMouseover="quitflag=true" onMouseout="quitflag=false"><img src="../images/quit.gif" width="64" height="42" border="0"></a>');
endbit();


//-------------------------

function do_help() {
displayText(helpmessage,1);
}

function do_reset() {

document.lwing.src='../images/lwing0.gif';
document.rwing.src='../images/rwing0.gif';
randomise();
loopl=-1;loopr=-1;
rightwing(5);

for(i=1;i<(total_items+1); i++) {
ttop=i*35-25;
tleft=500+((i+1)%2)*100;
eval('moveLayer("block'+(text_used[i]+1)+'",tleft,ttop)');
text_used[i]=0;
number_used[i]=0;
}
completed=0;
displayText(startmessage,1);
}

function randomise() {
text_used[1]=get_random();
for(i=2;i<(total_items+1); i++) {
ok=false;
while(ok==false) {
text_used[i]=get_random();
ok2=true;
for(j=1;j<i; j++) {
if(text_used[i]==text_used[j]) ok2=false;
}
if(ok2==true) ok=true;
}
}
}

function get_random() {return parseInt(Math.random()*1000)%total_items;}

function leftwing(lw) {
if(loopl!=lw) {
lw_inc=-1;loopl=3;if(lw==5) {lw_inc=1;loopl=1;}
lw_end=lw;
lwing();
}
}

function lwing() {
eval('document.lwing.src="../images/lwing'+loopl+'.gif"');
loopl+=lw_inc;
if(loopl!=lw_end) setTimeout("lwing()",60);
}

function rightwing(rw) {
if(loopr!=rw) {
rw_inc=-1;loopr=3;if(rw==5) {rw_inc=1;loopr=1;}
rw_end=rw;
rwing();
}
}

function rwing() {
eval('document.rwing.src="../images/rwing'+loopr+'.gif"');
loopr+=rw_inc;
if(loopr!=rw_end) setTimeout("rwing()",60);
}


function hidetext() {
hideLayer('textitem');
rightwing(-1);
}

function move_eyes(mposx,mposy) {
mposx=mposx-eyex;
mposy=eyey-mposy;
mval=mposx/mposy;

if((mposx>-50)&&(mposx<50)&&(mposy>-20)&&(mposy<20)) seg=4;
else {
if((mval>=-0.414)&&(mval<=0.414)) {
if(mposy>=0) seg=7;
if(mposy<0) seg=1;
}
if((mval>=0.414)&&(mval<=2.414)) {
if(mposx>=0) seg=8;
if(mposx<=0) seg=0;
}
if((mval<=-0.414)&&(mval>=-2.414)) {
if(mposx<0) seg=6;
if(mposx>0) seg=2;
}
if((mval>=2.414)||(mval<=-2.414)) {
if(mposx>=0) seg=5;
if(mposx<=0) seg=3;
}
}
eval('document.eyes.src="../images/eyes'+seg+'.gif"');
}

// ---------------

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 makelayerwithcolour(name,vis,left,top,width,height,z,colour) {
if(app=="N") w('<layer id="'+name+'" position=absolute bgcolor='+colour+' visibility='+vis+' left='+left+' top='+top+' width='+width+' height='+height+' z-index='+z+';>');
else w('<div id="'+name+'" STYLE="position:absolute;background-color:'+colour+';visibility:'+vis+';left:'+left+';top:'+top+';width:'+width+';height:'+height+';z-index:'+z+';">');
}

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);
}

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