var shot_check=new makeArray(10);
var shot_count=new makeArray(10);
var shot_x=new makeArray(10);
var shot_y=new makeArray(10);
var shot_combo=new makeArray(10);

var unit_count=new makeArray(100);
var unit_object=new makeArray(100);
var unit_type=new makeArray(100);

var color_code=new makeArray(100);

da_check=0;

enemycount=0;

backflash=0;

backcount=0;
backtype=0;
shotcharge=0;
score=0;

gamecount=0;

hp=0;

defmode=0;
defcount=0;
defpower=75;

color_code[0]="'#000000'";
color_code[1]="'#002222'";
color_code[2]="'#003333'";
color_code[3]="'#005555'";
color_code[4]="'#006666'";
color_code[5]="'#008888'";
color_code[6]="'#009999'";
color_code[7]="'#00bbbb'";
color_code[8]="'#00cccc'";
color_code[9]="'#00eeee'";
color_code[10]="'#00ffff'";

color_code[20]="'#220000'";
color_code[21]="'#550000'";
color_code[22]="'#770000'";
color_code[23]="'#aa0000'";
color_code[24]="'#cc0000'";
color_code[25]="'#ff0000'";
color_code[26]="'#ff3333'";
color_code[27]="'#ff5555'";
color_code[28]="'#ff8888'";
color_code[29]="'#ffaaaa'";
color_code[30]="'#ffdddd'";

color_code[40]="'#000000'";
color_code[41]="'#221100'";
color_code[42]="'#331800'";
color_code[43]="'#552800'";
color_code[44]="'#663300'";
color_code[45]="'#884400'";
color_code[46]="'#994800'";
color_code[47]="'#bb5800'";
color_code[48]="'#cc6600'";
color_code[49]="'#ee7700'";
color_code[50]="'#ff8800'";

gamemode=0;
draw_combo=0;
draw_combocount=0;
lifecount=0;

function gamestart()
{
	if(gamemode!=1)return;
	

	
	for(z=0;z<10;z++)
	{
		shot_check[z]=0;
		shot_count[z]=0;
		shot_x[z]=0;
		shot_y[z]=0;
	}
	
	for(z=0;z<100;z++)
	{
		unit_count[z]=0;
		unit_type[z]=0;
	}
	backflash=0;
	gamemode=2;
	gamecount=0;
	score=0;
	hp=16;
	nextscore=5000;
	nextscoresub=5000;
	document.all.scoretag.innerHTML="0";
	document.all.nexttag.innerHTML="[5000]";
	document.all.combotag.innerHTML="";
	for(x=0;x<8;x++)
	{
		for(y=0;y<8;y++)
		{
			eval(unit_object[x*10+y]+"'#000000'");
		}
	}
	alert("GAME START!");
	timer();
}

function debagalert()
{
	if(da_check==0)
	{
		da_check=1;
		alert();
	}
}

function lifetimer()
{
	if(gamemode!=3)return;
	lifecount--;
	ttt="";
	switch(lifecount)
	{
		case 27:ttt="‚q@@@@@@.";break;
		case 26:ttt="‚q‚d@@@@@.";break;
		case 25:ttt="‚q‚d‚b@@@@.";break;
		case 24:ttt="‚q‚d‚b‚n@@@.";break;
		case 23:ttt="‚q‚d‚b‚n‚u@@.";break;
		case 22:ttt="‚q‚d‚b‚n‚u‚d@.";break;
		case 21:
		case 20:
		case 19:
		case 18:
		case 17:
		case 16:
		case 15:
		case 14:
		case 13:
		case 12:
		case 11:
		case 10:
		case 9:
		case 8:
		case 7:	
		ttt="‚q‚d‚b‚n‚u‚d‚q.";break;
		case 6:ttt="@‚d‚b‚n‚u‚d‚q.";break;
		case 5:ttt="@@‚b‚n‚u‚d‚q.";break;
		case 4:ttt="@@@‚n‚u‚d‚q.";break;
		case 3:ttt="@@@@‚u‚d‚q.";break;
		case 2:ttt="@@@@@‚d‚q.";break;
		case 1:ttt="@@@@@@‚q.";break;
		case 0:ttt="";break;		
	}
	document.all.infotag.innerHTML=ttt;
	if(lifecount>0)
	{
	setTimeout("lifetimer()",70);
	}
	else
	{
		gamemode=2;
		timer();
	}
}

function timer()
{
	if(gamemode!=2)return;
	gamecount++;

	if(shotcharge>0)shotcharge--;

	if(defmode==0)
	{
		defcount-=defpower;
		if(defcount<=0)
		{
			defcount=0;
			defpower=Math.floor(Math.random()*67+33);
			defmode=1;
		}
	}
	else
	{
		defcount+=defpower;
		if(defcount>=7500)
		{
			defcount=7500;
			defpower=Math.floor(Math.random()*67+33);
			defmode=0;
		}
	}

	if(draw_combocount>0)
	{
		draw_combocount--;
		if(draw_combocount<=0)
		{
			draw_combo=0;
			document.all.combotag.innerHTML="";
		}
	}
			
	enemycount--;
	if(enemycount<=0)
	{
		enemycount=Math.floor(25*defcount/7500)+12;
		for(a=0;a<1+gamecount/300;a++)
		{
			ax=Math.floor(Math.random()*8);
			ay=Math.floor(Math.random()*8);
			if(unit_type[ax*10+ay]==0 || (unit_type[ax*10+ay]==5 && unit_count[ax*10+ay]<=5))
			{
				unit_type[ax*10+ay]=10;
				unit_count[ax*10+ay]=31;
			}
		}
	}
	
	for(z=0;z<5;z++)
	{
		if(shot_check[z]==1)
		{
			shot_count[z]+=2;
			if(shot_count[z]%2==0)
			{
			zz=Math.floor(shot_count[z]/2)-1;
			if(zz==0)
			{
				fir(shot_x[z],shot_y[z],z);
			}
			else
			{
				if(shot_x[z]-zz>=0)fir(shot_x[z]-zz,shot_y[z],z);
				if(shot_x[z]+zz<8)fir(shot_x[z]+zz,shot_y[z],z);
				if(shot_y[z]-zz>=0)fir(shot_x[z],shot_y[z]-zz,z);
				if(shot_y[z]+zz<8)fir(shot_x[z],shot_y[z]+zz,z);
			}
			
			}
			if(shot_count[z]>=17)shot_check[z]=0;
		}
	}
	
	for(tx=0;tx<8;tx++)
	{
		for(ty=0;ty<8;ty++)
		{
			if(unit_type[tx*10+ty]>0)
			{
				switch(unit_type[tx*10+ty])
				{
					case 5:
					case 6:
					
						
						unit_count[tx*10+ty]--;
						if(unit_type[tx*10+ty]==5)
						{
						eval(unit_object[tx*10+ty]+color_code[Math.floor(unit_count[tx*10+ty]/1)]);
						}
						else
						{
						eval(unit_object[tx*10+ty]+color_code[40+Math.floor(unit_count[tx*10+ty]/1)]);
						}
						if(unit_count[tx*10+ty]<=0)unit_type[tx*10+ty]=0;
					
					break;
					
					case 10:
					
						unit_count[tx*10+ty]--;
						if(unit_count[tx*10+ty]<10 && unit_count[tx*10+ty]%2==0)
						{
						eval(unit_object[tx*10+ty]+"'#ffffff'");
						}
						else
						{
						eval(unit_object[tx*10+ty]+color_code[20+Math.floor((31-unit_count[tx*10+ty])/3)]);
						}
						if(unit_count[tx*10+ty]<=0)
						{
						unit_type[tx*10+ty]=0;
						eval(unit_object[tx*10+ty]+"'#000000'");
						backcount+=2;
						}
						break;

					
				}
			}
		}
	}
	
	if(backcount>0)
	{
		backcount--;
		
		if(backcount%2==0)
		{
			document.all.gameback.bgColor="#222222";
		}
		else
		{
			document.all.gameback.bgColor="#880000";
						hp-=1;
						document.all.hpgage1.width=7*hp+1;
						document.all.hpgage2.width=113-7*hp;
						if(hp<=0)
						{gamemode=1;alert("GAME OVER");alert("LAST SCORE : "+score);alert("(C)KAESHIRODAI");return;}
								
		}
		
	}	
	
	if(backflash>0)
	{
		backflash--;
		if(backcount<=0)
		{
		if(backflash%2==0)
		{
			document.all.gameback.bgColor="#222222";
		}
		else
		{
			document.all.gameback.bgColor="#444488";
		}
		}
	}
	
	setTimeout("timer()",100);
}

function fir(fx,fy,fn)
{
	if(unit_type[fx*10+fy]==10)
	{
	shot_combo[fn]++;
	ccc=shot_combo[fn];
	if(draw_combo<=ccc)
	{
		draw_combo=ccc;
		draw_combocount=11;
		document.all.combotag.innerHTML="COMBO "+ccc;
	}
	score+=(shot_combo[fn]*100);
	if(score>=nextscore)
	{
		nextscoresub=Math.floor(1.25*nextscoresub);
		nextscore+=nextscoresub;
		document.all.nexttag.innerHTML="[ "+nextscore+" ]";
		hp=16;
						document.all.hpgage1.width=7*hp+1;
						document.all.hpgage2.width=113-7*hp;
						lifecount=28;
						gamemode=3;
						lifetimer();
						
	}
	document.all.scoretag.innerHTML=score;
	unit_type[fx*10+fy]=6;
	unit_count[fx*10+fy]=11;
	backtype=1;
	backflash+=2;
	return;
	}
	unit_type[fx*10+fy]=5;
	unit_count[fx*10+fy]=11;
}

function makeArray(n) {
        this.length = n;
        for (i = 0; i < n; i++) {
		this[i] = null
	}
}

function hit(hx,hy)
{
	if(gamemode!=2)return;
	if(shotcharge>0)return;
	shotcharge=5;
	j=0;
	for(j=j;shot_check[j]!=0;j=j)
	{
		j++;
	}
	shot_check[j]=1;
	shot_count[j]=0;
	shot_x[j]=hx;
	shot_y[j]=hy;
	shot_combo[j]=0;
	
	
	
	
}

function loadup()
{
	ttt="<table hspace=0 vspace=0 cellpadding=0 cellspacing=0 bgcolor='#222222' id='gameback'>";
	for(y=0;y<8;y++)
	{
		ttt=ttt+"<tr>";
		for(x=0;x<8;x++)
		{
			ttt=ttt+"<td onmousedown='hit("+x+","+y+");'><font size=3 id='unit"+x+y+"' color='#000000'>¡</font>";
			unit_object[x*10+y]="document.all.unit"+x+y+".color=";
		}
		ttt=ttt+"</tr>";
	}
	ttt=ttt+"</table>";
	
	document.all.gamefirld.innerHTML=ttt;
	
	gamemode=1;
	document.all.stb.value="START";
	//gamestart();
}
