var LoginPleaseTimeOut = null;
var MainAutoToggleBoxTimeOut = new Array();
var GameXML;

function GetFavAddGameList() {
	ToggleLoadingBox("show",170,40,LANG["DownloadData"]);

	var category = document.getElementById("category").value;
	var index = document.getElementById("index").value;

	document.getElementById("category").disabled = document.getElementById("index").disabled = true;

	var InnerFunctionValue = new Array();
	var XML = GetHttpRequestXML("/game.xml","category="+category+"&index="+index,"GetFavAddGameListInner",InnerFunctionValue);
}

function GetFavAddGameListInner(XML) {
	var root = XML.documentElement;
	var text = new Array();
	var value = new Array();

	for (var i=0, total=root.childNodes.length;i<total;i++) {
		text[i] = root.childNodes.item(i).getAttribute("title");
		value[i] = root.childNodes.item(i).getAttribute("no");
	}

	text[0] = text.length==0 ? '해당게임없음' : text[0];
	value[0] = value.length==0 ? '' : value[0];

	InsertSelectBox("gamelist",text,value,1);

	document.getElementById("category").disabled = document.getElementById("index").disabled = false;

	ToggleLoadingBox("hidden","","","");
}

function GetFavGameList() {

}

function AddFavGame() {
	var AddGame = GetSelectBox("selected","gamelist");
	var AddGameText = GetSelectBox("selectedText","gamelist");
	var MyGame = GetSelectBox("list","favlist");

	if (!AddGame) {
		alert("추가할 게임을 선택해 주세요");
		return false;
	}

	var AddGame = AddGame.split(",");
	var AddGameText = AddGameText.split(",");
	var MyGame = MyGame.split(",");

	for (var i=0, total=AddGame.length;i<total;i++) {
		for (var k=0, totalMyGame=MyGame.length;k<totalMyGame;k++) {
			if (AddGame[i]==MyGame[k]) {
				AddGame[i] = "";
				break;
			}
		}
	}

	var AddListText = new Array();
	var AddListValue = new Array();
	var AddFavGameValue = "";

	for (var i=0, k=0, total=AddGame.length;i<total;i++) {
		if (AddGame[i]!="") {
			AddFavGameValue+= AddFavGameValue!="" ? ","+AddGame[i] : AddGame[i];
			AddListValue[k] = AddGame[i];
			AddListText[k] = AddGameText[i];
			k++;
		}
	}

	GetHTTPRequest("/doGame.do","exec=addFavGame&alert=1&gno="+AddFavGameValue);
	InsertSelectBox("favlist",AddListText,AddListValue,0);
}

function DeleteFavGame() {
	var MyGame = GetSelectBox("list","favlist").split(",");
	var MyGameText = GetSelectBox("listText","favlist").split(",");
	var DelGame = GetSelectBox("selected","favlist");
	var DelGameValue = DelGame;

	if (!DelGame) {
		Alertbox("삭제할 게임을 선택하세요");
		return false;
	}

	if (confirm("정말 삭제하시겠습니까?")==true) {
		var DelGame = DelGame.split(",");

		for (var i=0, total=MyGame.length;i<total;i++) {
			for (var k=0, totalDel=DelGame.length;k<total;k++) {
				if (MyGame[i]==DelGame[k]) {
					MyGame[i] = "";
					break;
				}
			}
		}

		var ListText = new Array();
		var ListValue = new Array();

		for (var i=0, k=0, total=MyGame.length;i<total;i++) {
			if (MyGame[i]!="") {
				ListText[k] = MyGameText[i];
				ListValue[k] = MyGame[i];
				k++;
			}
		}

		GetHTTPRequest("/doGame.do","exec=delFavGame&gno="+DelGameValue);
		InsertSelectBox("favlist",ListText,ListValue,1);
	}
}

function GameIconOver(num,title) {
	document.images["GameIcon"+num].className = "";
	ShowTip(title);
}

function GameIconOut(num) {
	document.images["GameIcon"+num].className = "opacity50";
	HideTip();
}

function MainToggleBox(id,mode) {
	for (var i=1;i<=4;i++) {
		if (document.getElementById(id+"ToggleBox"+i).style.display=="") {
			nowViewBox = i;
			break;
		}
	}

	if (mode=="left") {
		nextViewBox = nowViewBox==1 ? 4 : nowViewBox-1;
		while(!document.getElementById(id+"ToggleBox"+nextViewBox))
			nextViewBox--;
	} else {
		nextViewBox = nowViewBox==4 ? 1 : nowViewBox+1;
		while(!document.getElementById(id+"ToggleBox"+nextViewBox)){
			nextViewBox++;
			if(nextViewBox>4) nextViewBox=1;
		}
	}

	document.getElementById(id+"ToggleBox"+nowViewBox).style.display = "none";
	document.getElementById(id+"ToggleBox"+nextViewBox).style.display = "";
}

function MainAutoToggleBox(id,looper) {
	if (looper==1) {
		for (var i=1;i<=4;i++) {
			if (document.getElementById(id+"ToggleBox"+i).style.display=="") {
				nowViewBox = i;
				break;
			}
		}

		nextViewBox = nowViewBox==4 ? 1 : nowViewBox+1;
		document.getElementById(id+"ToggleBox"+nowViewBox).style.display = "none";
		document.getElementById(id+"ToggleBox"+nextViewBox).style.display = "";
	}

	MainAutoToggleBoxTimeOut[id] = setTimeout("MainAutoToggleBox('"+id+"',1)",7000);
}

function MainAutoToggleBoxStop(id) {
	clearTimeout(MainAutoToggleBoxTimeOut[id]);
	MainAutoToggleBoxTimeOut[id] = null;
}

function MainAutoToggleBoxResume(id) {
	MainAutoToggleBoxTimeOut[id] = setTimeout("MainAutoToggleBox('"+id+"',1)",5000);
}

function RankToggleBox(code) {
	if (document.getElementById("Rank"+code+"1").style.display=="none") {
		document.getElementById("Rank"+code+"1").style.display = "";
		document.getElementById("Rank"+code+"2").style.display = "none";
	} else {
		document.getElementById("Rank"+code+"1").style.display = "none";
		document.getElementById("Rank"+code+"2").style.display = "";
	}
}

function TabBox(id,code) {
	for (var i=1;i<=10;i++) {
		if (document.getElementById(id+"TabBox"+i)) {
			document.getElementById(id+"TabBox"+i).className = "TabBoxUnSelect";
			document.getElementById(id+"TabBoxArea"+i).style.display = "none";
		} else {
			break;
		}
	}

	document.getElementById(id+"TabBox"+code).className = "TabBoxSelect";
	document.getElementById(id+"TabBoxArea"+code).style.display = "";
}

function LoginPlease() {
	var object = document.getElementById("PlzLogin");

	if (object) {
		clearTimeout(LoginPleaseTimeOut);
		object.style.display = "";
		LoginPleaseTimeOut = setTimeout("document.getElementById('PlzLogin').style.display='none';",8000);
	} else {
		alert('로그인을 하여주십시오.');
	}
}

function ToggleSubTabMenu(id,tab) {
	document.getElementById(id+"TabBg").className = "SubMenuTab"+tab;
	if (tab=="Left") {
		document.getElementById(id+"TabLeftButton").className = "SubMenuTabButtonLeft SubMenuTabSelected";
		document.getElementById(id+"TabRightButton").className = "SubMenuTabButtonRight SubMenuTabUnSelected";
		document.getElementById(id+"TabLeftArea").style.display = "";
		document.getElementById(id+"TabRightArea").style.display = "none";
	} else {
		document.getElementById(id+"TabLeftButton").className = "SubMenuTabButtonLeft SubMenuTabUnSelected";
		document.getElementById(id+"TabRightButton").className = "SubMenuTabButtonRight SubMenuTabSelected";
		document.getElementById(id+"TabLeftArea").style.display = "none";
		document.getElementById(id+"TabRightArea").style.display = "";
	}
}

function GetSubUnionCategory(code,type,layer,QueryString) {
	ToggleLoadingBox("show",170,40,LANG["DownloadData"]);

	var InnerFunctionValue = new Array();
	InnerFunctionValue[0] = layer;
	InnerFunctionValue[1] = QueryString;
	var XML = GetHttpRequestXML(ENV["dir"]+"/live.xml","mode=getUnionCategoryDepth1&code="+code+"&type="+type,"GetSubUnionCategoryInner",InnerFunctionValue);
}

function GetSubUnionCategoryInner(layer,QueryString,XML) {
	if (XML) {
		var root = XML.documentElement;
		var sHTML = '';
		var title;
		var categoryName = new Array();
		categoryName[1] = '온라인게임';
		categoryName[2] = '패키지게임';
		categoryName[3] = '비디오/콘솔게임';
		categoryName[4] = '모바일게임';
		categoryName[5] = '플래시게임';
		var categoryType = new Array();
		categoryType[1] = 'online';
		categoryType[2] = 'package';
		categoryType[3] = 'video';
		categoryType[4] = 'mobile';
		categoryType[5] = 'flash';

		if (root.childNodes.length>0) {
			for (var i=0, totalList=root.childNodes.length;i<totalList;i++) {
				no = root.childNodes.item(i).getAttribute("no");
				title = root.childNodes.item(i).getAttribute("title");
				type = root.childNodes.item(i).getAttribute("type");
				category = categoryName[type];
				if (QueryString=="game") {
					moveUrl = "/game/"+categoryType[type]+".qvate?gno="+no;
				} else if (QueryString=="fansite") {
					moveUrl = "/fansite/fansite.qvate?gno="+no;
				} else if (QueryString=="cafe") {
					moveUrl = "/cafe/cafe.qvate?gno="+no;
				} else {
					moveUrl = ENV["dir"]+"/"+QueryString+"&udepth1="+no;
				}
				sHTML+= '<div title="'+title+'" class="pointer" style="height:32px;" onmouseover="this.style.background=\'#F4F4F4\';" onmouseout="this.style.background=\'#FFFFFF\';" onclick="location.href=\''+moveUrl+'\';">';
				sHTML+= '<div style="border:1px solid #DDDDDD; width:45px; height:30px; overflow:hidden;" class="LayerLeft"><img src="/common/images/game/titleimg/t'+no+'.jpg" align="middle" alt="GameTitleImage" onerror="this.src=\'/common/images/game/titleimg/t0.jpg\'" /></div>';
				sHTML+= '<div style="width:115px; height:29px; margin-left:3px; padding:3px 0px 0px 0px; overflow:hidden;" class="LayerLeft">';
				sHTML+= '<div class="dotum f11 bold nobr" style="overflow:hidden; height:15px;">'+title+'</div>';
				sHTML+= '<div class="dotum f11 gray">'+category+'</div>';
				sHTML+= '</div>';
				sHTML+= '</div>';
				sHTML+= '<div class="LayerClear height5"></div>';
			}
		}

		document.getElementById(layer).innerHTML = sHTML;
	}

	ToggleLoadingBox("hidden","","","");
}

function GameFav(gno) {
	if (ENV["isMember"]==false) {
		LoginPlease();
		return false;
	}
	if (confirm("해당게임을 즐겨찾는게임에 추가하시겠습니까?")==true) {
		execFrame.location.href = "/doGame.do?exec=addFavGame&gno="+gno;
	}
}

function GetSearchGame() {
	if (document.getElementById("GameListBox").style.display=="none") {
		document.getElementById("GameListBox").style.display = "";
		GetUnionCategory('ㄱ');
	} else {
		document.getElementById("GameListBox").style.display = "none";
	}
}

function GetUnionCategory(code) {
	ToggleLoadingBox("show",170,40,LANG["DownloadData"]);

	var InnerFunctionValue = new Array();
	var XML = GetHttpRequestXML(ENV["dir"]+"/live.xml","mode=getUnionCategoryDepth1&code="+code,"GetUnionCategoryInner",InnerFunctionValue);
}

function GetSelectGame(no,title) {
	document.getElementById("GameName").value = title;
	document.getElementById("GameNo").value = no;

	GetUnionCategoryDepth2("GameNo","GameServer","게임서버 선택안함");
	document.getElementById("GameListBox").style.display = "none";
}

function GetUnionCategoryInner(XML) {
	if (XML) {
		var root = XML.documentElement;
		var sHTML = '';
		var title;

		if (root.childNodes.length>0) {
			for (var i=0, totalList=root.childNodes.length;i<totalList;i++) {
				no = root.childNodes.item(i).getAttribute("no");
				title = root.childNodes.item(i).getAttribute("title");
				sHTML+= '<div class="pointer" onmouseover="this.style.background=\'#F4F4F4\';" onmouseout="this.style.background=\'#FFFFFF\';" onclick="GetSelectGame('+no+',\''+title+'\')">';
				sHTML+= '<div class="dotum f11" style="padding:3px 0px 2px 2px;">'+title+'</div>';
				sHTML+= '</div>';
				sHTML+= '<div style="height:1px; overflow:hidden;"><div style="border:1px dotted #CCCCCC;"></div></div>';
			}
		}

		document.getElementById("QvateGameList").innerHTML = sHTML;
	}

	ToggleLoadingBox("hidden","","","");
}