/* 
 * Menu Link, Location Map, Menu Code 관리 
 * @author HJin
*/ 

var xmlURL = "/common/menuLink.xml";
var menuXml = parseXML(xmlURL); // xml 파싱
var depth = 3; // 현재 사이트의 Depth

//페이지 링크
function menu_link(pMenuCode) {
//	alert(pMenuCode);
	var isMove = false;

	if(pMenuCode != "") {
		jQuery(menuXml).find("uLink").each(function(){
			if(pMenuCode == jQuery(this).attr("menucode")) {				
				if(jQuery(this).attr("gUrl") != "") {
					if(jQuery(this).attr("gTarget") == "_blank") {
						if(typeof(jQuery(this).attr("width")) != "undefined" && typeof(jQuery(this).attr("height")) != "undefined") {
							window.open(jQuery(this).attr("gUrl"), 'popup', 'width='+jQuery(this).attr("width")+',height='+jQuery(this).attr("height")+',top=150,left=250,toolbar=no,menubar=no,location=no,scrollbars=no,status=no');
						}
						else {
							window.open(jQuery(this).attr("gUrl"));
						}

						isMove = true;
						return false;
					}
					else {
						location.href = jQuery(this).attr("gUrl");
						isMove = true;
						return false;
					}
				}
				else {
					alert("준비중입니다.");
					isMove = true;
					return false;
				}
			}
		});

		if(isMove) return false;

		for(var i=0; i<=depth; i++) {
			jQuery(menuXml).find("depth"+i).each(function(){
				if(pMenuCode == jQuery(this).attr("menucode")) {
					if(jQuery(this).attr("gUrl") != "") {
						if(jQuery(this).attr("gTarget") == "_blank") {
							if(typeof(jQuery(this).attr("width")) != "undefined" && typeof(jQuery(this).attr("height")) != "undefined") {
								window.open(jQuery(this).attr("gUrl"), 'popup', 'width='+jQuery(this).attr("width")+',height='+jQuery(this).attr("height")+',top=150,left=250,toolbar=no,menubar=no,location=no,scrollbars=no,status=no');
							}
							else {
								window.open(jQuery(this).attr("gUrl"));
							}
							return false;
						}
						else {
							location.href = jQuery(this).attr("gUrl");
							return false;
						}
					}
					else {
						alert("준비중입니다.");
						return false;
					}
				}
			});
		}
	}
	else {
		alert("올바른 링크가 아닙니다.");
	}
}

//현재페이지 메뉴코드 리턴
function getMenuCode(){

	var linkType = "/";
	var arrTemp, strCompareOrigin, strCompare, strCompareQueryString, strMenuCode;

	arrTemp = location.href.split("/");

	strCompare = arrTemp[3];
	for(var i=4; i<arrTemp.length; i++) {
		strCompare = strCompare + "/" + arrTemp[i];
	}
	strCompareOrigin = linkType + strCompare;
	strCompare = strCompareOrigin.split("?")[0];
	strCompareQueryString = strCompareOrigin.split("?")[1];

	jQuery(menuXml).find("uLink").each(function(){
		if(typeof(jQuery(this).attr("compareQueryString")) == "undefined") {
			if(strCompare == jQuery(this).attr("gUrl")) {
				strMenuCode = jQuery(this).attr("menucode");
				return false;
			}
		}
		else {
			if(strCompareQueryString != null) {
				strCompare += "?";

				for(var j=0; j<jQuery(this).attr("compareQueryString"); j++) {
					strCompare += strCompareQueryString.split("&")[j];

					if(j != jQuery(this).attr("compareQueryString")-1) {
						strCompare += "&";
					}
				}

				if(strCompare == jQuery(this).attr("gUrl")) {
					strMenuCode = jQuery(this).attr("menucode");
					return false;
				}
			}
		}

		strCompare = strCompareOrigin.split("?")[0];
	});

	for(var i=0; i<=depth; i++) {
		jQuery(menuXml).find("depth"+i).each(function(){
			if(typeof(jQuery(this).attr("compareQueryString")) == "undefined") {
				if(strCompare == jQuery(this).attr("gUrl")) {
					strMenuCode = jQuery(this).attr("menucode");
					return false;
				}
			}
			else {
				if(strCompareQueryString != null) {
					strCompare += "?";

					for(var j=0; j<jQuery(this).attr("compareQueryString"); j++) {
						strCompare += strCompareQueryString.split("&")[j];

						if(j != jQuery(this).attr("compareQueryString")-1) {
							strCompare += "&";
						}
					}

					if(strCompare == jQuery(this).attr("gUrl")) {
						strMenuCode = jQuery(this).attr("menucode");
						return false;
					}
				}
			}

			strCompare = strCompareOrigin.split("?")[0];
		});
	}

	return strMenuCode;
}

// Location Map 출력
function callLocationMap(pULClass, pSelectClass) {
	var strTemp = "";
	var strMenuCode = getMenuCode();
	var arrTemp = new Array();

	for(var i=0; i<strMenuCode.length/2; i++) {
		arrTemp[i] = strMenuCode.substr(0, (i+1)*2);
	}

	strTemp += '<ul class="'+pULClass+'">\n';
	strTemp += '<li class="home"><a href="menu_link(1)"><a href="/index.jsp">HOME</a></li>\n';
	for(var i=0; i<arrTemp.length; i++) {
		if(i != arrTemp.length-1) {
			strTemp += '<li><a href="javascript:menu_link(\''+arrTemp[i]+'\')">' + getNameAsMenuCode(arrTemp[i]) + '</a></li>\n';
		}
		else {
			strTemp += '<li class='+pSelectClass+'>' + getNameAsMenuCode(arrTemp[i]) + '</li>\n';
		}
	}
	strTemp += '</ul>';

	document.write(strTemp);
}

// MenuCode에 맞는 name 리턴
function getNameAsMenuCode(pMenuCode) {
	var strName;
	for(var i=0; i<=depth; i++) {
		jQuery(menuXml).find("depth"+i).each(function(){
			if(pMenuCode == jQuery(this).attr("menucode")) {
				strName = jQuery(this).attr("name");
				return false;
			}
		});
	}

	return strName;
}

//팝업윈도우	함수
function openWindow(pUrl) {
	window.open(pUrl);
}

//로그인 페이지
/*
 * pIsPopup - 팝업 로그인 창 여부 ("Y" - 팝업 로그인 창, 그 외 - 로그인 페이지) 
 * pIsNext - 로그인 후 다음 페이지 이동 여부("Y" - 다음페이지 이동, 그 외 - 팡업창 닫고 오프너 새로고친)
 * pNextURL - 로그인 후 이동할 페이지
 * pNextType - 팝업 로그인 창일 경우 다음페이지 이동 방법 ("self" - 팝업 내에서 이동, 그 외 - 오프너창에서 이동)
 */
function loginPopup(pIsPopup, pIsNext, pNextURL, pNextType) {
	var formURL = "http://id.hangame.com/wlogin.nhn";

	if(location.href.indexOf("alpha-") >= 0 || location.href.indexOf("localhost") >= 0) {
		formURL = "http://alpha-id.hangame.com/wlogin.nhn";
	}
	else if(location.href.indexOf("beta-") >= 0) {
		formURL = "http://beta-id.hangame.com/wlogin.nhn";
	}

	var nextURL;
	var sURL;

	if(pIsNext == "Y") {
		nextURL = "&closemove=Y&nxtURL=" + pNextURL;
	}
	else {
		nextURL = "&nxtURL=" + top.location.href;
	}

	if(pNextType == "self") {
		sURL = formURL + "?sbanner=no&popup=true&closemove=Y" + nextURL;
	}
	else {
		sURL = formURL + "?sbanner=no&popclose=Y" + nextURL;
	}
	
	if(pIsPopup == "Y") {
		window.open(sURL, 'fllogin','width=360,height=400,top=150,left=250,toolbar=no,menubar=no,location=no,scrollbars=no,status=no');
	}
	else {
		if(typeof(pNextURL) == "undefined") {
			pNextURL = encodeURIComponent(location.href);
		}
		sURL = formURL + "?popup=false&nxtURL=" + pNextURL;
		location.href = sURL;
	}
}

//베타테스터 팝업
function openBetaTesterPop(pIsLogin, pIsSimple, pIsCheckName) {
	if(pIsLogin == 1) {
		if(pIsSimple == "true") {
			if(!confirm("정회원 전환 후 이용하실 수 있습니다.\n지금 정회원 전환 하시겠습니까?")) return;
			goPermissionPage(encodeURIComponent(document.URL), encodeURIComponent(document.URL));
		}
		else {
			if(pIsCheckName == "true") {
				if(!confirm("실명인증 완료 후 이용하실 수 있습니다.\n지금 실명인증 하시겠습니까?")) return;
				goPermissionPage(encodeURIComponent(document.URL), encodeURIComponent(document.URL));
			}
			else {
				window.open('/popup/popup_betatester.nhn', 'betatester','width=410,height=740,top=150,left=250,toolbar=no,menubar=no,location=no,scrollbars=yes,status=no');
			}
		}
	} //height=735
	else {
		loginPopup("Y", "Y", "http://" + location.host + "/popup/popup_betatester.nhn?isAutoSize=true", "self");
	}
}

//자동사양체크 팝업
function openAutoSpecAnalyze() {
	window.open('/popup/popup_spec_analyze.nhn', 'analyze','width=550,height=415,top=150,left=250,toolbar=no,menubar=no,location=no,scrollbars=no,status=no');
}

//마이페이지 팝업
function openMyPage() {
	window.open('/popup/popup_mypage.nhn', 'mypage','width=610,height=490,top=150,left=250,toolbar=no,menubar=no,location=no,scrollbars=no,status=no');
}

function download() {
	menu_link('010203');
}

//실명인증, 정회원 전환 안내 페이지
function goPermissionPage(pNext, pHome) {
	var formURL = "http://member.hangame.com/permissionDeniedView.nhn?code=bbswrite&next="+pNext+"&homeurl="+pHome;

	if(location.href.indexOf("alpha-") >= 0 || location.href.indexOf("localhost") >= 0) {		
		formURL = "http://alpha-member.hangame.com/permissionDeniedView.nhn?code=bbswrite&next="+pNext+"&homeurl="+pHome;
	}
	else if(location.href.indexOf("beta-") >= 0) {
		formURL = "http://beta-member.hangame.com/permissionDeniedView.nhn?code=bbswrite&next="+pNext+"&homeurl="+pHome;
	}

	location.href = formURL;
}