// JavaScript Document	
var UNIT = new Array();
var unit = new Array();
var debug = new Array();
var debug_div;
var title;
var 디버그출력안하기 = false;
디버그출력안하기 = true;

var status;
var delay = 90; // 딜레이값
var control_button;

function getElementsByClassName(clsName,parentNode) {
 var arr = new Array();
 if (parentNode == null) {
  var elems = document.getElementsByTagName("*");
 } else {
  var elems = parentNode.getElementsByTagName("*");
 }
 for ( var cls, i = 0; ( elem = elems[i] ); i++ ) {
  if ( elem.className == clsName ) {
   arr[arr.length] = elem;
  }
 }
 return arr;
}

function 시작등록(func) {
	var oldonLoad = window.onload;
	if (typeof window.onload != 'function') {
			window.onload = func;
	}else {
		window.onload = function(){
			oldonLoad();
			func();
		}
	}
}


시작등록(환경변수지정) ;
function 환경변수지정() {
	if (navigator.appName == "Microsoft Internet Explorer") return false;
	var _enemy = 24;
	var _shot = 20;
	var _item = 10;
	var _cnt = 1;
	UNIT[0] = new Array();
	UNIT[0]["name"] = "player";		//유닛의 종류
	UNIT[0]["width"] = 64;
	UNIT[0]["height"] = 64;			//유닛의 크기
	UNIT[0]["top"] = 0;
	UNIT[0]["left"] = 0;				//유닛의 초기위치
	UNIT[0]["display"] = "block";  
	UNIT[0]["pic"] = new Array();
	UNIT[0]["pic"][0] = "player1-0.png";
	
	for (i=1; i<= _cnt + _enemy; i++){
		UNIT[i] = new Array();	
		UNIT[i]["name"] = "enemy";
		UNIT[i]["width"] =  Math.floor(Math.random(3)*10)+100;
		UNIT[i]["height"] =  Math.floor(Math.random(3)*10)+80;
		UNIT[i]["top"] =  Math.floor(Math.random(3)*250)-20;
		UNIT[i]["left"] = Math.floor(Math.random(2)*500)+500;
		UNIT[i]["display"] = "block";
		UNIT[i]["pic"] = new Array()
		UNIT[i]["pic"][0] = "enemy-01-1-0.png";
	}
	_cnt = _cnt + _enemy;
	for (i=_cnt; i<= _cnt + _shot; i++) {
		UNIT[i] = new Array();	
		UNIT[i]["name"] = "shot";
		UNIT[i]["width"] = 20;
		UNIT[i]["height"] = 20;
		UNIT[i]["top"] = -120;
		UNIT[i]["left"] = -120;
		UNIT[i]["display"] = "block";
		UNIT[i]["pic"] = new Array()
		UNIT[i]["pic"][0] = "effect-01-0.png";
	}
	_cnt = _cnt + _shot;
	for (i=_cnt; i<= _cnt + _item; i++) {
		UNIT[i] = new Array();	
		UNIT[i]["name"] = "item";
		UNIT[i]["width"] = 40;
		UNIT[i]["height"] = 20;
		UNIT[i]["top"] = -100;
		UNIT[i]["left"] = -100;
		UNIT[i]["display"] = "block";
		UNIT[i]["pic"] = new Array()
		UNIT[i]["pic"][0] = "icon_energy.png";
	}

}

시작등록(유닛정의);
function 유닛정의() {
	var j=0;
	unit_element = document.getElementById("GameUnit"); //움직임을 제어할 유닛 정의
	for (i=0; i<UNIT.length; i++){
		var para = document.createElement("img");
		para.setAttribute ("class",UNIT[i]["name"] );
		para.setAttribute ("src",UNIT[i]["pic"][0]);
		para.setAttribute ("alt",UNIT[i]["pic"][0]);
		var style = "width:"+UNIT[i]["width"]+"px;height:"+UNIT[i]["height"]+"px;";
		style = style + "top:"+UNIT[i]["top"]+"px;left:"+UNIT[i]["left"]+"px;";
		style = style + "display:"+UNIT[i]["display"]+";";
		para.setAttribute ("style",style);
		unit_element.appendChild(para);
	}

	unit[0] = getElementsByClassName("player",unit_element);
	unit[1] = getElementsByClassName("enemy",unit_element);
	unit[2] = getElementsByClassName("item",unit_element);
	unit[3] = getElementsByClassName("shot",unit_element);
	unit[4] = document.getElementById("GameBG").getElementsByTagName("p");
	unit[0][0].point = 0;
	unit[0][0].xx = parseInt(unit[0][0].style.left);
	unit[0][0].yy = parseInt(unit[0][0].style.top);
	unit[0][0].style.top = "0px";
	unit[0][0].style.left = "0px";
	unit[0][0].speed = 80;		//이동속도
	unit[0][0].power = 80;		//힘     
	unit[0][0].life = 100;  	//생명력
	unit[0][0].energy = 100;	//에너지
	unit[0][0].move_x = 0; 		//가로방향 이동수치. 
	unit[0][0].move_y = 0;		//세로방향 이동수치.
	unit[0][0].final_x = unit[0][0].xx + unit[0][0].move_x; 		//가로 목적 좌표. 
	unit[0][0].final_y = unit[0][0].yy + unit[0][0].move_y;			//세로 목적 좌표.
	unit[0][0].anime_type = 0;		//애니메이션 타입
	unit[0][0].anime_cnt = 0;		//애니메이션 프레임번호.
	
	unit[0][0].pic = new Array();
	unit[0][0].pic[0] =  new Array();
	unit[0][0].pic[0][0] = "player1-0.png";
	unit[0][0].pic[0][1] = "player1-1.png";
	unit[0][0].pic[0][2] = "player1-2.png";
	unit[0][0].pic[0][3] = "player1-3.png";
	unit[0][0].pic[0][4] = "player1-3.png";
	unit[0][0].pic[0][5] = "player1-3.png";
	unit[0][0].pic[0][6] = "player1-2.png";
	unit[0][0].pic[0][7] = "player1-1.png";
	unit[0][0].pic[1]=  new Array();
	unit[0][0].pic[1][0] = "player2-0.png";
	unit[0][0].pic[1][1] = "player2-1.png";
	unit[0][0].pic[1][2] = "player2-2.png";
	unit[0][0].pic[1][3] = "player2-3.png";
	unit[0][0].pic[1][4] = "player2-3.png";
	unit[0][0].pic[1][5] = "player2-3.png";
	unit[0][0].pic[1][6] = "player2-3.png";
	unit[0][0].pic[1][7] = "player2-2.png";
	unit[0][0].pic[1][8] = "player2-1.png";	
	unit[0][0].pic[1][9] = "player1-0.png";
	unit[0][0].pic[2] = new Array();
	unit[0][0].pic[2][0] = "player3-0.png";
	unit[0][0].pic[2][1] = "player3-1.png";
	unit[0][0].pic[2][2] = "player3-2.png";
	unit[0][0].pic[2][3] = "player3-3.png";
	unit[0][0].pic[2][4] = "player3-3.png";
	unit[0][0].pic[2][5] = "player3-3.png";
	unit[0][0].pic[2][6] = "player3-3.png";
	unit[0][0].pic[2][7] = "player3-2.png";
	unit[0][0].pic[2][8] = "player3-1.png";	
	unit[0][0].pic[2][9] = "player1-0.png";
	unit[0][0].pic[3] =  new Array();
	unit[0][0].pic[3][0] = "player4-4.png";
	unit[0][0].pic[3][1] = "player4-3.png";
	unit[0][0].pic[3][2] = "player4-4.png";
	unit[0][0].pic[3][3] = "player4-3.png";
	unit[0][0].pic[3][4] = "player4-4.png";
	unit[0][0].pic[3][5] = "player4-3.png";
	unit[0][0].pic[3][6] = "player4-3.png";
	unit[0][0].pic[3][7] = "player4-2.png";
	unit[0][0].pic[3][8] = "player4-1.png";
	unit[0][0].pic[3][9] = "player4-0.png";
	unit[0][0].pic[4] = new Array();
	unit[0][0].pic[4][0] = "player5-0.png";
	unit[0][0].pic[4][1] = "player5-1.png";
	unit[0][0].pic[4][2] = "player5-2.png";
	unit[0][0].pic[4][3] = "player5-3.png";
	
	
	for (i=0; i<unit[1].length;i++) {

		unit[1][i].power = 10; 		//힘
		unit[1][i].life = 200		//생명력
		unit[1][i].move_x = -Math.floor(Math.random(1)*14); 		//가로방향 이동수치. 
		unit[1][i].move_y = 0;		//세로방향 이동수치.
		unit[1][i].anime_type = 0;		//애니메이션 타입
		unit[1][i].anime_cnt = 0;		//애니메이션 프레임번호.
		unit[1][i].pic =  new Array();
		unit[1][i].pic[0] =  new Array();
		unit[1][i].pic[0][0] = "enemy-01-1-0.png";
		unit[1][i].pic[0][3] = "enemy-01-1-1.png";
		unit[1][i].pic[0][1] = "enemy-01-1-2.png";	
		unit[1][i].pic[0][2] = "enemy-01-1-3.png";	
		unit[1][i].pic[1] =  new Array();
		unit[1][i].pic[1][3] = "enemy-01-1-1.png";
		unit[1][i].pic[1][1] = "enemy-01-1-2.png";	
		unit[1][i].pic[1][2] = "enemy-01-1-3.png";	
	}
	
	for (i=0; i<unit[4].length; i++) {
		var a = unit[4].length-i-1;
		var b = (i*40)+670;
		var c = (i*60)-100;
		var style = "background:url(back1-"+a+".png) repeat-x bottom left;";
		style = style + "width: 5000px; height:"+b+"px; top:0px; left:0px;";
		unit[4][i].setAttribute ("style",style);
		unit[4][i].style.top = c+"px";
		unit[4][i].style.left = "0px";	
		unit[4][i].move_x = 0; 		//가로방향 이동수치. 
		unit[4][i].move_y = 0;		//세로방향 이동수치.
	}
	//unit[4][4].setAttribute ("style","height:600px");
	
	unit[4][0].speed = .2;
	unit[4][1].speed = -.2;
	unit[4][2].speed = -.6;
	unit[4][3].speed = -1;
	unit[4][4].speed = -2;

}
function 유닛좌표계산(i,j,type) {
	unit[i][j].xx = parseInt(unit[i][j].style.left);
	unit[i][j].yy = parseInt(unit[i][j].style.top);	
	unit[i][j].width =parseInt(unit[i][j].style.width);	
	unit[i][j].height =parseInt(unit[i][j].style.height);
	unit[i][j].end_x = unit[i][j].xx + unit[i][j].width;
	unit[i][j].end_y = unit[i][j].yy + unit[i][j].height;
	
	
	unit[i][j].dead_x = unit[i][j].xx + Math.floor(unit[i][j].width/4);
	unit[i][j].dead_y = unit[i][j].yy+ Math.floor(unit[i][j].height/4);
	unit[i][j].dead_end_x = unit[i][j].end_x - Math.floor(unit[i][j].width/4);
	unit[i][j].dead_end_y = unit[i][j].end_y - Math.floor(unit[i][j].height/4);
	if(type != true) {
		unit[i][j].final_x = unit[i][j].xx + unit[i][j].move_x; 	 
		unit[i][j].final_y = unit[i][j].yy + unit[i][j].move_y;	
	}
}

시작등록(디버그정의);
function 디버그정의() {
	debug_div = document.getElementById("GameDebug");
	title = document.getElementById("GameHead").getElementsByTagName("h1");
		if (디버그출력안하기== true) {
			debug_div.setAttribute("style","display:none");
			return false;
		}
	
	for (i=0;i<unit.length;i++){
		var list = document.createElement("ol");
		list.setAttribute("class", "list_"+i);
		debug_div.appendChild(list);
	}
	var debug_ol = document.getElementById("GameDebug").getElementsByTagName("ol");
	for (j=0;j<unit.length;j++) {
		for (i=0;i<unit[j].length;i++){
			list = document.createElement("li");
			list.setAttribute("class", unit[j][i].getAttribute("class"));
			list.setAttribute("title", unit[j][i].getAttribute("class"));
			debug_ol[j].appendChild(list);	
		}
	}

	debug[0] = getElementsByClassName("player",debug_ol[0]);
	debug[1] = getElementsByClassName("enemy",debug_ol[1]);
	debug[2] = getElementsByClassName("item",debug_ol[2]);
	debug[3] = getElementsByClassName("shot",debug_ol[3]);
	debug[4] = getElementsByClassName("back",debug_ol[4]);
	디버그전부출력() ;	
}
시작등록(상태창정의) ;
function 상태창정의() {
	status = document.getElementById("GameStatus").getElementsByTagName("em");
	status.point = document.getElementById("GameStatus").getElementsByTagName("span");
	상태창출력();
}
function 상태창출력() {
	status[0].style.left = unit[0][0].life + "px";
	status[1].style.left = unit[0][0].energy + "px";
	var text ="HP: <strong>" + unit[0][0].life + "</strong> EP: <strong>" + unit[0][0].energy ;
	text= text + "</strong> Point: <strong>"+ unit[0][0].point+"</strong>"
	status.point[2].innerHTML = text;
}
function 디버그전부출력() {
	if (디버그출력안하기== true) return false;

	for (j=0;j<unit.length;j++) {
		for (i=0;i<unit[j].length;i++){
			디버그출력(j,i);
		}
	}
}
function 디버그출력(j,i) {
		if (디버그출력안하기== true) return false;
		var text = unit[j][i].getAttribute("class") + " left:"+ parseInt(unit[j][i].style.left);
		text = text + " top:"+ parseInt(unit[j][i].style.top);
		text = text + " width:"+ parseInt(unit[j][i].style.width);
		text = text + " height:"+ parseInt(unit[j][i].style.height);
		text = text + " final_x:"+ unit[j][i].final_x;
		text = text + " final_y:"+ unit[j][i].final_y;	
		text = text + " move_x:"+ unit[j][i].move_x;
		text = text + " move_y:"+ unit[j][i].move_y;			
		if (debug[j][i].innerHTML != null) debug[j][i].innerHTML = null;
		txt = document.createTextNode(text);
		debug[j][i].appendChild(txt);	
}