// JavaScript Document	
var 난이도 = "normal";
var 잠깐멈춤 = true;
var gameover = false;
var UNIT = new Array();
var unit = new Array();
var debug = new Array();
var debug_div;
var title;
var 디버그출력안하기 = false;
디버그출력안하기 = true;
var status;
var delay = 30; // 딜레이값
var control_button;
var 감점 = -10;
var 득점 = 10;

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 난이도판별() {
	var str=location.href;
	var num=str.substring(str.indexOf("?")+1,str.length);
		if (num == "easy" || num == "normal" || num == "hard" || num == "veryhard") {
			난이도 = num;
		}
		
	var html_body = document.getElementsByTagName("body");
	html_body[0].setAttribute("class",난이도); 
}

시작등록(환경변수지정) ;
function 환경변수지정() {
	if (navigator.appName == "Microsoft Internet Explorer") return false;
	var _enemy = new Array;
	if (난이도 == "easy") _enemy[0] = 3;
	else if (난이도 == "normal") _enemy[0] = 10;
	else if (난이도 == "hard") _enemy[0] = 15;
	else if (난이도 == "veryhard") _enemy[0] = 20;
	_enemy[1] = _enemy[0];
	var _shot = 10;
	var _item = 3;
	var _effect = 2;


	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"]  = "player1-0.png";
	var i=_cnt;
	for (i=_cnt; i< _cnt + _effect; i++) {
		UNIT[i] = new Array();	
		UNIT[i]["name"] = "effect";
		UNIT[i]["width"] = 50;
		UNIT[i]["height"] = 50;
		UNIT[i]["top"] = -100;
		UNIT[i]["left"] = -100;
		UNIT[i]["display"] = "block";
		UNIT[i]["pic"] = "effect-03-0.png";
	}
	_cnt = _cnt + _effect;
	for (j=_cnt; j< _cnt + _enemy[0]; j++){
		UNIT[i] = new Array();	
		UNIT[i]["name"] = "enemy1";
		UNIT[i]["width"] =  Math.ceil(Math.random(3)*10)+100;
		UNIT[i]["height"] =  Math.ceil(Math.random(3)*10)+70;
		UNIT[i]["top"] =  Math.ceil(Math.random(3)*250)-20;
		UNIT[i]["left"] = Math.ceil(Math.random(2)*500)+500;
		UNIT[i]["display"] = "block";
		UNIT[i]["pic"]  = "enemy-01-1-0.png";
		i++;
		UNIT[i] = new Array();	
		UNIT[i]["name"] = "enemy2";
		UNIT[i]["width"] = UNIT[i-1]["width"]*2;
		UNIT[i]["height"] =  UNIT[i-1]["height"]-5;
		UNIT[i]["top"] = UNIT[i-1]["top"];
		UNIT[i]["left"] = UNIT[i-1]["width"]+ UNIT[i-1]["left"]-20;
		UNIT[i]["display"] = "block";
		UNIT[i]["pic"]  = "effect-02-0.png";
		i++
	}
	_cnt = _cnt + _enemy[0];
	_cnt = _cnt + _enemy[1];
	
	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"] = "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"]  = "icon_energy.png";
	}
	_cnt = _cnt + _item;

}

시작등록(유닛정의);
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"]);
		para.setAttribute ("alt",UNIT[i]["pic"]);
		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("item",unit_element);
	unit[2] = getElementsByClassName("shot",unit_element);
	unit[3] = new Array();
	unit[3][0] = document.getElementById("GameBG");
	unit[4] = document.getElementById("GameBG").getElementsByTagName("p");
	unit[5] = getElementsByClassName("enemy1",unit_element);
	unit[6] = getElementsByClassName("enemy2",unit_element);
	unit[7] = getElementsByClassName("effect",unit_element);
	
	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;	//에너지
	
	if (난이도 == "easy") unit[0][0].energy_minus = 0.01;
	else if (난이도 == "normal") unit[0][0].energy_minus = 0.05;
	else if (난이도 == "hard") unit[0][0].energy_minus = 0.1;
	else if (난이도 == "veryhard") unit[0][0].energy_minus = 0.5;
		//에너지 소모량

	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[5].length;i++) {
		unit[5][i].power = 10; 		//힘
		unit[5][i].life = 200		//생명력
		unit[5][i].move_x = -Math.ceil(Math.random(1)*10); 		//가로방향 이동수치. 
		unit[5][i].move_y = 0;		//세로방향 이동수치.
		unit[5][i].anime_type = 0;		//애니메이션 타입
		unit[5][i].anime_cnt = 0;		//애니메이션 프레임번호.
		unit[5][i].pic =  new Array();
		unit[5][i].pic[0] =  new Array();
		unit[5][i].pic[0][0] = "enemy-01-1-0.png";
		unit[5][i].pic[0][1] = "enemy-01-1-1.png";
		unit[5][i].pic[0][2] = "enemy-01-1-2.png";	
		unit[5][i].pic[0][3] = "enemy-01-1-3.png";	
		unit[5][i].pic[0][4] = "enemy-01-1-4.png";	
		unit[5][i].pic[0][5] = "enemy-01-1-5.png";	
		unit[5][i].pic[0][6] = "enemy-01-1-6.png";	
		unit[5][i].pic[0][7] = "enemy-01-1-7.png";	
		unit[5][i].pic[0][8] = "enemy-01-1-8.png";	
		unit[5][i].pic[0][9] = "enemy-01-1-9.png";	
		unit[5][i].pic[0][10] = "enemy-01-1-10.png";	
		unit[5][i].pic[1] =  new Array();
		unit[5][i].pic[1][0] = "enemy-01-1-1.png";
		unit[5][i].pic[1][1] = "enemy-01-1-2.png";	
		unit[5][i].pic[1][2] = "enemy-01-1-3.png";	
		unit[5][i].math = 0 ; 			// sin. cos 사용위한 변수.
	}
	for (i=0; i<unit[6].length;i++) {
		unit[6][i].power = 10; 		//힘
		unit[6][i].life = 200		//생명력
		unit[6][i].move_x = unit[5][i].move_x; 		//가로방향 이동수치. 
		unit[6][i].move_y = unit[5][i].move_y;		//세로방향 이동수치.
		unit[6][i].anime_type = 0;		//애니메이션 타입
		unit[6][i].anime_cnt = 0;		//애니메이션 프레임번호.
		unit[6][i].pic =  new Array();
		unit[6][i].pic[0] =  new Array();
		unit[6][i].pic[0][0] = "effect-02-0.png";
		unit[6][i].pic[0][1] = "effect-02-1.png";
		unit[6][i].pic[0][2] = "effect-02-2.png";	
		unit[6][i].pic[0][3] = "effect-02-3.png";	
		unit[6][i].pic[0][4] = "effect-02-4.png";	
		unit[6][i].pic[0][5] = "effect-02-5.png";	
		unit[6][i].pic[0][6] = "effect-02-6.png";	
		unit[6][i].math = 0 ; 			// sin. cos 사용위한 변수.
	}
	
	unit[7][0].anime_type = 0;		//애니메이션 타입
	unit[7][0].anime_cnt = 0;		//애니메이션 프레임번호.
	unit[7][0].pic =  new Array();
	unit[7][0].pic[0] =  new Array();
	unit[7][0].pic[0][0] = "effect-04-0.png";
	unit[7][0].pic[0][1] = "effect-04-1.png";
	unit[7][0].pic[0][2] = "effect-04-2.png";	
	unit[7][0].pic[0][3] = "effect-04-3.png";	
	unit[7][0].pic[0][4] = "effect-04-4.png";	
	unit[7][0].pic[0][5] = "effect-04-5.png";	
	unit[7][0].pic[1] =  new Array();
	unit[7][0].pic[1][0] = "effect-03-0.png";
	unit[7][0].pic[1][1] = "effect-03-1.png";
	unit[7][0].pic[1][2] = "effect-03-2.png";	
	unit[7][0].pic[1][3] = "effect-03-3.png";	
	unit[7][0].pic[1][4] = "effect-03-4.png";	
	unit[7][0].pic[1][5] = "effect-03-5.png";	
	unit[7][0].pic[2] =  new Array();
	unit[7][0].pic[2][0] = "effect-01-0.png";
	unit[7][0].pic[2][1] = "effect-01-1.png";
	unit[7][0].pic[2][2] = "effect-01-2.png";	
	unit[7][0].pic[2][3] = "effect-01-3.png";	
	unit[7][0].pic[2][4] = "effect-01-2.png";	
	unit[7][0].pic[2][5] = "effect-01-1.png";	
	unit[7][0].math = 0 ; 			// sin. cos 사용위한 변수.
	unit[7][0].style.width = "90px";	
	unit[7][0].style.height = "90px";
	
	// 여기부터 배경관련 설정
	unit[3][0].setAttribute ("style","top:0px; left:-150px;");
	unit[3][0].speed =0;
	unit[3][0].move_x = 0;
	unit[3][0].move_y = 0;
	
	unit[3][0].BG_X = -170; //  전채 배경 이동 한계값 설정. 
	unit[3][0].BG_xx = 300; 
	unit[3][0].BG_y = 0;
	unit[3][0].BG_yy = 50;
	
	for (i=0; i<unit[4].length; i++) {
		var a = unit[4].length-i-1;
		var b = (i*40)+600;
		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 = .05;
	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.ceil(unit[i][j].width/10);
	unit[i][j].dead_y = unit[i][j].yy+ Math.ceil(unit[i][j].height/10);
	unit[i][j].dead_end_x = unit[i][j].end_x - Math.ceil(unit[i][j].width/10);
	unit[i][j].dead_end_y = unit[i][j].end_y - Math.ceil(unit[i][j].height/10);
	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("item",debug_ol[1]);
	debug[2] = getElementsByClassName("shot",debug_ol[2]);
	debug[3] = getElementsByClassName("GameBG",debug_ol[3]);
	debug[4] = getElementsByClassName("back",debug_ol[4]);
	debug[5] = getElementsByClassName("enemy1",debug_ol[5]);
	debug[6] = getElementsByClassName("enemy2",debug_ol[6]);
	debug[7] = getElementsByClassName("effect",debug_ol[7]);
	
	디버그전부출력() ;	
}
시작등록(상태창정의) ;
function 상태창정의() {
	status = document.getElementById("GameStatus").getElementsByTagName("em");
	status.point = document.getElementById("GameStatus").getElementsByTagName("span");
	상태창출력();
}
function 상태창출력() {
	HP_EP_감소();
	status[0].style.left = unit[0][0].life + "px";
	status[1].style.left = unit[0][0].energy + "px";
	var text ="HP: <strong>" + Math.ceil(unit[0][0].life) + "</strong> EP: <strong>" + Math.ceil(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") ;
		text = text + " 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);	
}