unit_state=new makearray(30);
checker=new makearray(30);
unit_object=new makearray(30);
waku_object=new makearray(30);
tama_object=new makearray(15);
tama_state=new makearray(15);
flashcheck=0;
clearcheck=0;
now=0;
hour=0;
min=0;
sec=0;

infocount=0;

starttime=0;
finishtime=0;
timecharge=100;

gamemode=0;

//■■■■■配列生成
function makearray(n) {
        this.length = n;
        for (i = 0; i < n; i++) {
		this[i] = null
	}
}

function getnowtime()
{
	p=sec+min*60+hour*3600;
	if(starttime>p)p+=86400;
	
	return p;
}

function loadup()
{
	ttt="<table>";
		ttt=ttt+"<tr>";
		ttt=ttt+"<td>";
		ttt=ttt+"<td><pre><b><center><font id='tamax1' color=#ffcc00 size=1>　</font></b>";
		ttt=ttt+"<td><pre><b><center><font id='tamax2' color=#ffcc00 size=1>　</font></b>";
		ttt=ttt+"<td><pre><b><center><font id='tamax3' color=#ffcc00 size=1>　</font></b>";
		ttt=ttt+"<td><pre><b><center><font id='tamax4' color=#ffcc00 size=1>　</font></b>";
		ttt=ttt+"<td><pre><b><center><font id='tamax5' color=#ffcc00 size=1>　</font></b>";
		ttt=ttt+"</tr>";
	for(y=0;y<5;y++)
	{
		ttt=ttt+"<tr>";
		ttt=ttt+"<td><pre><b><center><font id='tamay"+(y+1)+"' color=#ffcc00 size=1>　</font></b>";

			for(x=0;x<5;x++)
			{
				if(x!=4 || y!=4)
				{ttt=ttt+"<td><table id='waku"+(y*5+x+1)+"' bgcolor='#ffcc00' width=24 height=24><tr><td bgcolor=#000000 onmousedown='unitclick("+(x+y*5+1)+")'><b><center><font size=2 id='unit"+(x+y*5+1)+"' color=#ffcc00>"+(x+y*5+1)+"</font></center></b></tr></table>";}
				else
				{ttt=ttt+"<td><table id='waku"+(y*5+x+1)+"' bgcolor='#000000' width=24 height=24><tr><td bgcolor=#000000 onmousedown='unitclick("+(x+y*5+1)+")'><b><center><font size=2 id='unit"+(x+y*5+1)+"' color=#ffcc00></font></center></b></tr></table>";}
			}
		ttt=ttt+"</tr>";
	}
	ttt=ttt+"</table>";
	
	document.all.gamefirld.innerHTML=ttt;
	
	unit_object[1]=document.all.unit1;
	unit_object[2]=document.all.unit2;
	unit_object[3]=document.all.unit3;
	unit_object[4]=document.all.unit4;
	unit_object[5]=document.all.unit5;
	unit_object[6]=document.all.unit6;
	unit_object[7]=document.all.unit7;
	unit_object[8]=document.all.unit8;
	unit_object[9]=document.all.unit9;
	unit_object[10]=document.all.unit10;
	unit_object[11]=document.all.unit11;
	unit_object[12]=document.all.unit12;
	unit_object[13]=document.all.unit13;
	unit_object[14]=document.all.unit14;
	unit_object[15]=document.all.unit15;
	unit_object[16]=document.all.unit16;
	unit_object[17]=document.all.unit17;
	unit_object[18]=document.all.unit18;
	unit_object[19]=document.all.unit19;
	unit_object[20]=document.all.unit20;
	unit_object[21]=document.all.unit21;
	unit_object[22]=document.all.unit22;
	unit_object[23]=document.all.unit23;
	unit_object[24]=document.all.unit24;
	unit_object[25]=document.all.unit25;
	
	waku_object[1]=document.all.waku1;
	waku_object[2]=document.all.waku2;
	waku_object[3]=document.all.waku3;
	waku_object[4]=document.all.waku4;
	waku_object[5]=document.all.waku5;
	waku_object[6]=document.all.waku6;
	waku_object[7]=document.all.waku7;
	waku_object[8]=document.all.waku8;
	waku_object[9]=document.all.waku9;
	waku_object[10]=document.all.waku10;
	waku_object[11]=document.all.waku11;
	waku_object[12]=document.all.waku12;
	waku_object[13]=document.all.waku13;
	waku_object[14]=document.all.waku14;
	waku_object[15]=document.all.waku15;
	waku_object[16]=document.all.waku16;
	waku_object[17]=document.all.waku17;
	waku_object[18]=document.all.waku18;
	waku_object[19]=document.all.waku19;
	waku_object[20]=document.all.waku20;
	waku_object[21]=document.all.waku21;
	waku_object[22]=document.all.waku22;
	waku_object[23]=document.all.waku23;
	waku_object[24]=document.all.waku24;
	waku_object[25]=document.all.waku25;
	
	tama_object[1]=document.all.tamax1;
	tama_object[2]=document.all.tamax2;	
	tama_object[3]=document.all.tamax3;	
	tama_object[4]=document.all.tamax4;	
	tama_object[5]=document.all.tamax5;	
	
	tama_object[6]=document.all.tamay1;	
	tama_object[7]=document.all.tamay2;	
	tama_object[8]=document.all.tamay3;	
	tama_object[9]=document.all.tamay4;	
	tama_object[10]=document.all.tamay5;	
	
	document.all.bt1.value="START";	
	gamemode=1;
	timer();
}

function gamestart()
{
	if(gamemode!=1)return;
	alert("任務を遂行せよ");
	for(z=1;z<=10;z++)
	{
		tama_state[z]=0;
	}
	for(z=1;z<=5;z++)
	{
		j=Math.floor(Math.random()*10+1);
		for(zz=0;tama_state[j]==1;zz=zz)
		{
			j++;
			if(j>=11)j=1;
		}
		tama_state[j]=1;
	}
	
	drawtama();
	
	for(z=1;z<=25;z++)
	{unit_state[z]=z;if(z==25)unit_state[z]=0;}
	for(z=1;z<25;z++)
	{
		zz=Math.floor(Math.random()*(26-z))+z;
		zzz=unit_state[zz];
		unit_state[zz]=unit_state[z];
		unit_state[z]=zzz;
	}
	drawunit();
	gettime();
	starttime=sec+min*60+hour*3600;
	timecharge=90;
	finishtime=90+starttime;
	setdead();
	gamemode=2;
	clearcheck=0;
}

function drawtama()
{
	for(z=1;z<=10;z++)
	{
		if(tama_state[z]==0)
		{
		tama_object[z].innerHTML="　";
		}
		else
		{
		tama_object[z].innerHTML="■";
		}
	}
}

function setdead()
{
	z1=Math.floor((finishtime%86400)/3600);
	z2=Math.floor((finishtime%3600)/60);
	z3=finishtime%60;

	ttt="";
	
	if(z1<10)ttt=ttt+"0";
	ttt=ttt+z1+":";
	if(z2<10)ttt=ttt+"0";
	ttt=ttt+z2+":";
	if(z3<10)ttt=ttt+"0";
	ttt=ttt+z3;
	
	document.all.deadtag.innerHTML=ttt;
}

function drawunit()
{
	checkcount=0;
	for(z=1;z<=25;z++)
	{
		if(unit_state[z]==z)
		{
		checker[z]=1;
		checkcount++;
		}
		else
		{
		checker[z]=0;
		}
		if(z==25){checker[25]=1;checkcount++;}
				
		if(unit_state[z]==0)
		{
			unit_object[z].innerHTML="";
			waku_object[z].bgColor="#000000";
		}
		else
		{
			unit_object[z].innerHTML=unit_state[z];
			waku_object[z].bgColor="#ffcc00";
		}
	}
	if(gamemode==2)
	{
		for(z=1;z<=10;z++)
		{
			if(tama_state[z]==1)
			{
				subcheck=1;
				if(z<6)
				{
					for(zz=1+(z-1);zz<=21+(z-1);zz+=5)	
					{
						if(checker[zz]==0)
						{
							subcheck=0;zz=100;
						}
					}
				}
				else
				{
					for(zz=1+(z-6)*5;zz<=5+(z-6)*5;zz++)
					{
						if(checker[zz]==0)
						{
							subcheck=0;zz=100;
						}
					}	
				}
				if(subcheck==1)
				{
					tama_state[z]=0;
					timecharge+=24;
					document.all.infotag.innerHTML="+24sec";
					infocount=25;
					finishtime=starttime+timecharge;
					setdead();
					drawtama();
				}
			}
		}
		
		if(checkcount>=25)
		{
			pp=getnowtime()-starttime;
			alert("任務達成に成功した！");
			
			tt="";
			if(pp%60 <10)
			{
			tt=Math.floor(pp/60)+":0"+pp%60;
			}
			else
			{tt=Math.floor(pp/60)+":"+pp%60;}
			alert("PLAY TIME "+tt);
			alert("CONGLATURATION!");
			gamemode=1;
			document.all.deadtag.innerHTML="24:00:00";
			clearcheck=1;
		}
	}

}

function unitclick(pos)
{
	inx=(pos-1)%5;
	iny=Math.floor((pos-1)/5);
	if(gamemode!=2)return;
	
	check=0;
	buffer1=0;
	buffer2=0;
	for(z=1;z<=4;z++)
	{
		for(j=1;j<=4;j++)
		{
		switch(z)
		{
			case 1:
			x=inx-j;y=iny;
			break;
			case 2:
			x=inx+j;y=iny;
			break;
			case 3:
			x=inx;y=iny-j;
			break;
			case 4:
			x=inx;y=iny+j;
			break;
		}
		
		if(x<0 || x>4 || y<0 || y>4)
		{
			j=10;
		}
		else
		{
			if(unit_state[x+y*5+1]==0)
			{
				buffer2=j;j=10;buffer1=z;z=10;check=1;
			}
		}
		
		}
	}
	
	if(check==1)
	{
		for(z=buffer2;z>=1;z--)
		{
		switch(buffer1)
		{
			case 1:
			x=inx-z;y=iny;
			sx=inx-z+1;sy=iny;
			break;
			case 2:
			x=inx+z;y=iny;
			sx=inx+z-1;sy=iny;
			break;
			case 3:
			x=inx;y=iny-z;
			sx=inx;sy=iny-z+1;
			break;
			case 4:
			x=inx;y=iny+z;
			sx=inx;sy=iny+z-1;
			break;
		}
		unit_state[x+y*5+1]=unit_state[sx+sy*5+1];
		}
		unit_state[inx+iny*5+1]=0;
		drawunit();
	}
}


function gettime()
{
	now = new Date();
	hour = now.getHours();
	min = now.getMinutes();
	sec = now.getSeconds();
	
	ttt="";
	
	if(hour<10)ttt=ttt+"0";
	ttt=ttt+hour+":";
	if(min<10)ttt=ttt+"0";
	ttt=ttt+min+":";
	if(sec<10)ttt=ttt+"0";
	ttt=ttt+sec;
	
	document.all.timetag.innerHTML=ttt;
}

function timer()
{
	if(clearcheck==1 && gamemode==1)
	{
		for(z=1;z<25;z++)
		{
			if(Math.floor(Math.random()*3)==0)
			{
				waku_object[z].bgColor="#ffffff";
			}
			else
			{
				waku_object[z].bgColor="#ffcc00";
			}
		}
	}
	if(infocount>0)
	{
		infocount--;
		if(infocount<=0)
		{
			document.all.infotag.innerHTML="";
		}
	}
	gettime();
	
	if(gamemode==2)
	{
		if(getnowtime()<finishtime-12)
		{
			document.all.timetag.color="#ffcc00";
		}
		else
		{
			if(flashcheck==0)
			{
				flashcheck=1;
				document.all.timetag.color="#ffcc00";
			}
			else
			{
				flashcheck=0;
				document.all.timetag.color="#ffffff";
			}
		}
	}
	else
	{
	document.all.timetag.color="#ffcc00";
	}
	
	if(getnowtime()>=finishtime && gamemode==2)
	{
	alert("任務遂行に失敗した…");
	alert("GAME OVER");
	document.all.deadtag.innerHTML="24:00:00";
	for(z=1;z<=25;z++)
	{
		unit_state[z]=(z!=25?z:0);
		if(z<=10)tama_state[z]=0;
	}
	gamemode=1;
	drawunit();
	drawtama();
	
	}
	setTimeout("timer()",100);
}
