var w=window,
	wm=w.Math,
	d=document,
	ie=w.document.all&&w.d.getElementById,
	height=0,
	ti=pcnt=mov=mst=ani=tca=tia=trt=tcm=tim=trc=cx=cy=ay=ax=mc=0,
	tj=new Object(),tp,str,l1,l2,cd,pd,cdm,pdm,agt,mbg,intro,main,dpm,proc,gcd,pcd,agby,agbn,gca,
	bpho,bres,bcon,bdec,btra,bcom,gia,grt,gcm,gim,grc,sto,tk,end,air,pro,dec,sec,pri,rea,fos,wrg,
	cdt='Click on \'Process Card\' again.',
	pdt='Please draw another process card.',
    msg2='You are incorrect.  This is not the card for your token.  ',
	msg3='You are wrong.  You can use this card.  ';
var scard=new Array();
var pcard=new Array('Respiration','Photosynthesis','Consumption','Decomposition','Transportation','Combustion');
var msg=new Array(
'Will the process card work for your token piece in its current location?',
'You are correct. Now that you know you can use this card, do you want to use it? (Is it the most direct route to the Ending Point?)','','','Please click on the location you think your token will end up by using the process described on the card.',
'You are correct! Please draw another process card.',
'Sorry, this process will not take your token to the location you chose.  Do you want to try again with this same process card.',
'O.K. Please redraw.',
'Congratulations! You\'ve made it.  Do you want to play again?'
)
var lcard=new Array(
'Air',
'Ready for Combustion',
'Fossil Fuels',
'Decomposers',
'Primary Consumers',
'Secondary Consumer',
'Producers');
function sel_card(x){return wm.floor(wm.random()*x)}
	
function sizeIt(){
	if(ie)
		height=w.d.documentElement.clientHeight
	else
		height=w.innerHeight;	
	if(main.style.display&&height<606&&intro.style.display=="none")height=665;
	smbg(height);
}

function smbg(h){
	mbg.style.height=h+"px";
}

function ge(x){
	return d.getElementById(x);
}

function loc(){
	if(this.value.length<15){
		var card=lcard[sel_card(7)];
		if(l1.innerHTML==''){
			tfil(l1,card);
			this.disabled=true;
			w.setTimeout('str.disabled=false',1000)
		}else{
			while(card=='Fossil Fuels')card=lcard[sel_card(7)];
			tfil(l2,card);
			this.disabled=true;
			w.setTimeout("str.disabled=false;str.value='.| Start Game |.'",1300);
		}
	}else{
		if(tp){
			agt.innerHTML='Please select the Process you think will move carbon from its current location';
			msg[2]+=pdt;
			msg[3]+=pdt;
			chg_view(proc,dpm);
		}else{
			shuffle();
			agt.innerHTML='Please click on \'Process Card\'.';
			msg[2]+=cdt;
			msg[3]+=cdt;
			chg_view(dpm,proc);						
		}//if
		chg_view(intro,main) //change view mode
	}
}

function sani(x,i){
document.title='mov='+mov+'\r\nani='+ani+'\r\ntmp='+tmp+'\r\ni='+i
if(mov&&ani&&tmp==i){
	if(i==2)i=0;
	l1.innerHTML=lcard[i];++mc;oj=x;clearTimeout(sto);mani(0);
	if(mc>0&&ani&&l1.innerHTML==l2.innerHTML){mg(8);dsb(true,false);mst=3}else{
	bdsb(false);mg(5);chgc(gcm,++tcm);setTimeout("gcm.style.color='white'",1100)}
}else{mg(6);mst=2;dsb(true,false);
clearTimeout(wrg);wrgm(0);
chgc(gim,++tim);setTimeout("gim.style.color='white'",1100)}mov=0;ani=0;
}

function mani(i){
var i;
var x=(w.parseInt(oj.style.left)-w.parseInt(tk.style.left)+32);
var y=(w.parseInt(oj.style.top)-w.parseInt(tk.style.top)+31);
cx+=(((20*Math.sin(x/0x60))-cx)/16);
cy+=(((20*(y/0x60))-cy)/16);
ax+=cx;ay+=cy;
tk.style.top=ay+"px";
tk.style.left=ax+"px";
if(x==0&&y==0)++i;
if(i>10)clearTimeout(sto);else sto=setTimeout('mani('+i+')',10)
}

function wrgm(n) {
if(n<20){
var m=5*Math.sin(.16*n);
tk.style.left=ax+m*Math.sin(n)+"px";
tk.style.top=ay+m*Math.cos(n)+"px";
wrg=setTimeout('wrgm('+ ++n +')',30)
}else clearTimeout(wrg)}

function chg_view(x,y){
x.style.display='none';
y.style.display='block';
sizeIt();
if(l1.innerHTML)status(l1.innerHTML,l2.innerHTML)
}

function rest(){msg[2]=msg2;msg[3]=msg3;
proc.style.display='none';
dpm.style.display='none';
tp=0;selm(pdm,cdm,1);pd.checked=true;bdsb(false);
dsb(false,true);pcd.innerHTML=agt.innerHTML=gcd.innerHTML='';
gca.innerHTML=gia.innerHTML=gcm.innerHTML=gim.innerHTML=grc.innerHTML=grt.innerHTML=pcnt=mov=ani=mst=tca=tia=tcm=tim=trc=trt=cx=cy=ay=ax=mc=0;
str.value='.| Location |.';clearTimeout(sto);
l1.innerHTML='';l2.innerHTML='';
}

function icx(x){
	var ar=new Array();
	switch(x){
		case "Air":ar[0]=420,ar[1]=62;break;
		case "Rea":ar[0]=590,ar[1]=160;break;
		case "Fos":ar[0]=590,ar[1]=352;break;
		case "Dec":ar[0]=420,ar[1]=446;break;
		case "Pri":ar[0]=248,ar[1]=352;break;
		case "Sec":ar[0]=248,ar[1]=160;break;
		case "Pro":ar[0]=420,ar[1]=254;break;
		default:break;
	}
	return ar;
}

function status(x,y){
tk1=icx(x.substr(0,3));
end1=icx(y.substr(0,3));
x=31+tk1[0];
y=32+tk1[1];
tk.style.left=x+"px";
tk.style.top=y+"px";
ax=x;
ay=y;
end.style.left=10+end1[0]+"px";
end.style.top=10+end1[1]+"px";
}

function shuffle(){
	var shuff=new Array();
	var tmax=60;
	for(i=0;i<tmax;++i){
		if(i<15)scard[i]='a'+i;      //15 respir
		else if(i<30)scard[i]='b'+i; //15 photosyn
		else if(i<35)scard[i]='c'+i; //5 consump
		else if(i<40)scard[i]='d'+i; //5 decomp
		else if(i<50)scard[i]='e'+i; //10 trans
		else scard[i]='f'+i			 //10 comb
	}

	for(i=0;i<tmax;i++){
		rand=scard[Math.round(Math.random()*tmax)];
		for(j=0;j<tmax;j){
			if(rand==shuff[j]){
				rand=scard[Math.round(Math.random()*tmax)];j=0
			}j++
		}
		shuff[i]=rand
	}
	
	for(i=0;i<tmax;++i){
		if(shuff[i].substr(0,1)=='a')scard[i]=pcard[0];
		else if(shuff[i].substr(0,1)=='b')scard[i]=pcard[1];
		else if(shuff[i].substr(0,1)=='c')scard[i]=pcard[2];
		else if(shuff[i].substr(0,1)=='d')scard[i]=pcard[3];
		else if(shuff[i].substr(0,1)=='e')scard[i]=pcard[4];
		else scard[i]=pcard[5]
	}
}

function tfil(d,x){
if(ie){
d.style.filter='progid:DXImageTransform.Microsoft.Pixelate(MaxSquare=10)';
d.filters[0].apply();
d.innerText=x;
d.filters[0].play()
}else{
tj=d;
ti=0;
d.style.MozOpacity=0;
d.innerHTML=x;
tfil2();
}
}

function tfil2(){
	tj.style.MozOpacity=ti*.01;
	ti+=2.25;
	if(ti<100)window.setTimeout(tfil2)
}

function selm(x,y,z){
tp=z;
x.style.color='white';
x.style.textDecoration='underline';
x.style.fontWeight='bold';
y.style.textDecoration='';
y.style.color='';
y.style.fontWeight=''
}

function btnP(){
procrd();
++pcnt;if(pcnt>55){shuffle();pcnt=0}
}

function procrd(){
var tmp=0;
tfil(gcd,scard[pcnt])
for(i=0;i<6;++i)if(pcard[i]==scard[pcnt]){tmp=i;break}
setProc(tmp);
agh(tmp);
mg(0);
mst=0;
ani=0;
dsb(true,false)
}

function mg(x){
agt.innerHTML=msg[x]
}

function dsb(x,y){
proc.disabled=x;
agby.disabled=y;
agbn.disabled=y;
}

function bdsb(x){
proc.disabled=x;
bpho.disabled=x;
bres.disabled=x;
bcon.disabled=x;
bdec.disabled=x;
btra.disabled=x;
bcom.disabled=x
}

function setProc(x){//test to see if the token can move or not in comp mode
if(x==0){if('3456'.indexOf(getArray())>-1)mov=1;else mov=0}
else if(x==1){if(getArray()==0)mov=1;else mov=0}
else if(x==2){if('64'.indexOf(getArray())>-1)mov=1;else mov=0}
else if(x==3){if('456'.indexOf(getArray())>-1)mov=1;else mov=0}
else if(x==4){if('62'.indexOf(getArray())>-1)mov=1;else mov=0}
else if(x==5){if(getArray()==1)mov=1;else mov=0}
}

function getArray(){
for(i=0;i<7;++i)if(lcard[i]==l1.innerHTML)break;return i;
}

function agh(y){//y is the card order
switch(y){
case 0:tmp=0;
pcd.innerHTML="<b>Respiration</b><p>Energy stored in glucose is released when carbon bonds are broken. The energy is used for cellular metabolism and heat is released as well.<p><b>Status of Energy</b><p>Used to energize the metabolic process, converted to kinetic energy, and released as heat.";break;
case 1:tmp=6;
pcd.innerHTML="<b>Photosynthesis</b><p>Solar energy is used to combine carbon in the atmosphere and water to for carbon bonds in glucose.<p><b>Status of Energy</b><p>Stored in chemical bonds";break;
case 2:
if(l1.innerHTML=='Producers')tmp=4;else tmp=5;
pcd.innerHTML="<b>Consumption</b><p>A consumer eats another organism.<p><b>Status of Energy</b><p>Stored in chemical bonds";break;
case 3:tmp=3;
pcd.innerHTML="<b>Decomposition</b><p>The organism generates waste materials or dies and is broken down by bacteria and other organisms to smaller particles.<p><b>Status of Energy</b><p>Stored in chemical bonds";break;
case 4:tmp=1;
pcd.innerHTML="<b>Transportation</b><p>The stored energy in the material is moved to a place where it can be burned.<p><b>Status of Energy</b><p>Stored in chemical bonds";break;
case 5:tmp=2;
pcd.innerHTML="<b>Combustion</b> (burned)<p>Fossil fuels, wood, and biomass are burned to release energy stored in carbon bonds.<p><b>Status of Energy</b><p>Released energy is used for space heating or to spin turbines to generate electricity (eventually converted to heat).";break;
default:pcd.innerHTML="<b>ERROR - code:"+i+"</b><p>There's an error while process card infos.";break;
}}

function btnY(){
switch(mst){
case 0:
if(mov){mg(1);dsb(true,false);mst=1;
chgc(gca,++tca)
setTimeout("gca.style.color='white'",1100)
}else{mg(2);dsb(false,true);bdsb(false);
chgc(gia,++tia)
setTimeout("gia.style.color='white'",1100)
}break;
case 1:
mg(4);dsb(false,true);bdsb(true);ani=1;break;
case 2:
chgc(grt,++trt);
setTimeout("grt.style.color='white'",1100);
mg(4);dsb(false,true);bdsb(true);ani=1;break;
case 3:rest();chg_view(main,intro);break;
default:agt.innerHTML='Error while responding.';break;
}}

function btnN(){
switch(mst){
case 0:
if(mov){mg(3);dsb(false,true);bdsb(false);
chgc(gia,++tia)
setTimeout("gia.style.color='white'",1100)}
else{mg(7);dsb(false,true);bdsb(false);
chgc(gca,++tca)
setTimeout("gca.style.color='white'",1100)}
break;
case 1:
mg(7);dsb(false,true);ani=0;bdsb(false);
chgc(grc,++trc);
setTimeout("grc.style.color='white'",1100);break;
case 2:
mg(7);dsb(false,true);bdsb(false);break;
case 3:agt.innerHTML="GAME OVER<br /><br />Thank you for playing.";dsb(false,true);bdsb(true);break;
default:agt.innerHTML='Error while responding.';break;
}}

function chgc(x,y){
x.style.color='red';
x.innerHTML=y;
}

function showLoc(){procrd();mg(4);dsb(false,true);bdsb(true);ani=1}

w.onload=function(){
	mbg=ge("mbg");
	intro=ge("intro");
	main=ge("main");
	str=ge("str");
	tk=ge("tk");
	end=ge("end");
	dpm=ge("dpm");
	proc=ge("proc");
	sizeIt();
	this.onresize=sizeIt;	
	l1=ge("l1");
	l2=ge("l2");
	str.onclick=loc;
	pdm=ge("pdm");
	cdm=ge("cdm");
	pcd=ge("prot");
	cd=ge("cd");
	pd=ge("pd");
	agby=ge("agby");
	agby.onclick=btnY;
	agbn=ge("agbn");
	agbn.onclick=btnN;
	gcd=ge("gs1");
	gca=ge("gs2");
	gia=ge("gs3");
	gcm=ge("gs4");
	gim=ge("gs5");
	grc=ge("gs6");
	grt=ge("gs7");
	agt=ge("agt");
	bpho=ge("bpho");
	bres=ge("bres");
	bcon=ge("bcon");
	bdec=ge("bdec");
	btra=ge("btra");
	bcom=ge("bcom");
	air=ge("air");
	pro=ge("pro");
	dec=ge("dec");
	sec=ge("sec");
	pri=ge("pri");
	rea=ge("rea");
	fos=ge("fos");
	cd.onclick=new Function("selm(cdm,pdm,0)");
	pd.onclick=new Function("selm(pdm,cdm,1)");
	proc.onclick=btnP;
	selm(pdm,cdm,1);
	rest();		
	clx=icx("Air");
	air.style.left=clx[0]+"px";
	air.style.top=clx[1]+"px";
	air.onclick=function(){
	var u=0;
	if('13456'.indexOf(getArray())>-1){
		if(l1.innerHTML.substr(0,3)=='Rea')u=2;
	mov=1;
	}else mov=0;	
	if(ani)sani(this,u)
	}
	clx=icx("Sec");
	sec.style.left=clx[0]+"px";
	sec.style.top=clx[1]+"px";
	sec.onclick=function(){
	if(4==getArray())mov=1;else mov=0;
	if(ani)sani(this,5)
	}
	clx=icx("Pro");
	pro.style.left=clx[0]+"px";
	pro.style.top=clx[1]+"px";
	pro.onclick=function(){
	if(0==getArray())mov=1;else mov=0;
	if(ani)sani(this,6)
	}
	clx=icx("Pri");
	pri.style.left=clx[0]+"px";
	pri.style.top=clx[1]+"px";
	pri.onclick=function(){
	if(6==getArray())mov=1;else mov=0;
	if(ani)sani(this,4)
	}
	clx=icx("Rea");//combustion
	rea.style.left=clx[0]+"px";
	rea.style.top=clx[1]+"px";
	rea.onclick=function(){
	if('62'.indexOf(getArray())>-1)mov=1;else mov=0;
	if(ani)sani(this,1)
	}
	clx=icx("Dec");
	dec.style.left=clx[0]+"px";
	dec.style.top=clx[1]+"px";
	dec.onclick=function(){
	if('456'.indexOf(getArray())>-1)mov=1;else mov=0;
	if(ani)sani(this,3)
	}
	clx=icx("Fos");
	fos.style.left=clx[0]+"px";
	fos.style.top=clx[1]+"px";
	fos.onclick=function(){
	mov=0;if(ani)sani(this,2)
	}
	end.onclick=function(){ge(l2.innerHTML.substr(0,3).toLowerCase()).onclick()}
	
	bpho.onclick=function(){
	scard[pcnt]='Photosynthesis';showLoc();
	}
	bres.onclick=function(){
	scard[pcnt]='Respiration';showLoc();
	}
	bcon.onclick=function(){
	scard[pcnt]='Consumption';showLoc();
	}
	bdec.onclick=function(){
	scard[pcnt]='Decomposition';showLoc();
	}
	btra.onclick=function(){
	scard[pcnt]='Transportation';showLoc();
	}
	bcom.onclick=function(){
	scard[pcnt]='Combustion';showLoc();
	}
}
