GAUTH_DOMAIN = "gauth.tetris.game.daum.net";
// for nClicks
var nsc = "tetris.main";
var ccsrv="cc.hangame.com";

// json 형태의 ajax response 처리 
function JSONResponse(res) {	
	try{
		eval(res);
	} catch(e){
		
	}
}



function startGame(gameString, gId, gGrup) {	
	//C.debug("4/5: before launching game.");
	try {
		setRecentHangameCookie("tet4");
		objActiveX.hgs_startGame(gameString);
		gameButtonClicked = false;
		//C.debug("5/5: after launching game.");
	} catch(e) {
		//C.debug("exception: " + e.message );
	}
}

// game 시작
var gameButtonClicked = false;	// HTTP 중복 호출 방지용
function stadiumClick( idx, state ) {	
	if( gameButtonClicked ) { return; }
	
	if( !isLogin() ){
		if(typeof(isIdpLogin) != 'undefined' && isIdpLogin()) {
			alert("동의 절차를 진행하셔야 합니다.");
			location.href = 'http://' + C.getPhaseDomain(GAUTH_DOMAIN, {local:"dev"}) + '/register.nhn';
		} else {		
			alert("로그인이 필요합니다.");
		}
		
		return;
	}
	
	if( idx == 100 && state == "off"){
		alert("평일 오후 9시~10시 30분, 주말 오후 8시~10시에만 내공 2배 경기장이 열립니다.");
		return;
	} else if( idx == 99){
		alert("내공2배 경기장은 하수~지존까지만 이용하실 수 있습니다.");
		return;
	}
	
	if( state == "off") {
		alert( "등급에 맞는 경기장만 이용하실 수 있습니다." );
		return;
	}		
	
	var gId = "TET4";
	var gGrup="";
	
	if( idx == 100 )
		gGrup = "double";
	else 
		gGrup = roomSubIds[idx];
	
	var isObs = "false";	
	if(state == "obs"){
		if(idx == "0090" || idx == "01D0"){		
			gGrup = idx;
			isObs = "true";
		}
	}
	
	if( state == "on" || state == "obs") {		
		//C.debug("2/5: before ajax call.");
		var ajax = $Ajax( "/gamestart.nhn?m=getGSTR&subid="+gGrup+"&isTest=y&isObs="+isObs, {
			onload:function(res) {
					var resObj = new JSONResponse(res.text());
					if (resObj.result == 'success') {
						//C.debug(resObj.str);						
						if(typeof resObj.isTest != 'undefined' && resObj.isTest == 'true')
							alert(resObj.str);
						startGame(resObj.str, gId, gGrup);
					} else if (resObj.result == 'error') {						
						if( resObj.message != "" ) {
							alert( resObj.message );
							if(resObj.errCode == 'junior')
								location.href = "http://member.hangame.com/register/changeRegister.nhn";
						} else if( resObj.errCode == 'notlogin') {							
							goWithLoginForGameStart("/index.nhn", false);							
							
						} else if ( resObj.errCode == 'notdomestic' ) {
							openChangeWin("http://" + C.getPhaseDomain("tetris.hangame.com", {local:"dev"}));
						} else if ( resObj.errCode == 'notItemUse'){
							openWindow("/popup/buy.nhn", "buyPopupWindow", 300, 300, "no");							
						} else if ( resObj.errCode == 'noEvtPlay'){						
							alert("평일 오후9시~10시 30분, 주말 오후 8시~10시에만 내공 2배 경기장이 열립니다.");
							window.location.reload();
						}
						gameButtonClicked = false;
					} else {						
						goWithLoginForGameStart("/index.nhn", false);	
						
					}
				}
			} );
		gameButtonClicked = true;
		ajax.request();
		//C.debug("3/5: after ajax call.");
	}
}

function openChangeWin(url){
	window.open( "https://" + C.getPhaseDomain("members.hangame.com", {local:"dev"}) + "/register/changeRegister.nhn?nxtURL=" + url, "hangame", 630, 499);
}


var url_loginform = '';
var bDev  = (location.href.indexOf("http://dev")>=0 || location.href.indexOf("http://local")>=0);
var bAlpha = (location.href.indexOf("http://alpha")>=0);
if(bDev) {
	url_loginform = 'http://dev-id.hangame.com/wlogin.nhn';
} else if(bAlpha){
	url_loginform = 'http://alpha-id.hangame.com/wlogin.nhn';
} else {
	url_loginform = 'http://id.hangame.com/wlogin.nhn';
}
function openPopLogin() {
	var url = url_loginform + "?sbanner=no&popclose=Y&nxtURL=";		
	openWin(url, "communityWlogin", 360, 420, "no", "yes", "no");
}

function goBbs(bbsGameId, bbsId, docId) {
	var bbsUrl = "";
	
	if(bbsGameId.indexOf("TETRIS_NOTICE") >= 0)
		bbsGameId = "TETRIS_NOTICE";
	
	if(bbsGameId.indexOf("TETRIS_STORY") >= 0)
		bbsGameId = "TETRIS_STORY";
	
	switch(bbsGameId){
	case "TETRIS_NOTICE":
		bbsUrl = "/zone/notice.nhn?bbsid=" + bbsId;
		break;
	case "TETRIS_BEST":
		bbsUrl = "/zone/best.nhn?bbsid=" + bbsId;
		break;
	case "TETRIS_STORY":
		bbsUrl = "/zone/story.nhn?bbsid=" + bbsId;
		break;
	}
	
	
	if( docId ) {
		bbsUrl += "&docid=" + docId;
	}
	
	location.href = bbsUrl;
}

function openMyInfo(tab) {
	
	
	var url = "/myinfo.nhn?m=" + tab;
	
	if("personal" == tab){
		if(!isLogin()) {
			url += "&src=event";
			goWithLoginForTetris(url);
		} else 
			openWinNative( url, "wndTetrisMyInfo", 600, 500);
	}
	else {
		if( !isLogin() ){			
			document.location.reload();
			return;
		}	
		openWinNative( url, "wndTetrisMyInfo", 600, 500);
	}
	
}

function goPollPage(pageId,setId){
	switch(pageId){
		case "poll" : 
			location.href = "/zone/poll.nhn?m=getTetrisPoll&setid="+setId;
			break;
		case "result" :
			location.href = "/zone/poll.nhn?m=getResult&setid="+setId;
			break;
	}
}

function frameSelfResize() {	
	try {
		var objname = window.name;		
		if( !(typeof(opener) == "object" || objname == null || objname == "" || parent == null) )	{
			var objFrame = eval("parent.document.all." + objname);		
			var objSize = eval("parent.document." + objname);
			var giboObjectHeight = (document.all.tblGiboEditToolbar) ? parseInt(document.all.tblGiboEditToolbar.height) + parseInt(document.all.objGibo2005.height) + parseInt(document.all.tblGiboViewToolbar.height) : 0;
			if(typeof(objFrame) != "undefined") { //gamemain 의견달기는 resizing안함 
				objFrame.style.height = parseInt(objSize.document.body.scrollHeight) + giboObjectHeight;
				objFrame.height = parseInt(objSize.document.body.scrollHeight) + giboObjectHeight;
	
				var parentWindowName = parent.window.name;
				if( !(parentWindowName==null || parentWindowName=='') ) {
					var strEval = "parent.parent.document.all." + parentWindowName;
					var ppObjFrame = eval(strEval);
					
					if( typeof(ppObjFrame) != "undefined" ) {
						ppObjFrame.style.height= parseInt(objFrame.height)+200
					}
				}
			}
		}
	} catch(e) {
		if( (typeof(ENV_isDev) != "undefined" && ENV_isDev)
			|| (typeof(ENV_isAlpha) != "undefined" && ENV_isAlpha) 
		) {
			alert(e.message);
		}
	}
}

function frameSelfResizeForTETRIS(){
	try {
		var objname = window.name;
		
		if( !(typeof(opener) == "object" || objname == null || objname == "" || parent == null) )	{
			var objFrame = eval("parent.document.all." + objname);	
			
			var objSize = eval("parent.document." + objname);
		
			var giboObjectHeight = (document.all.tblGiboEditToolbar) ? parseInt(document.all.tblGiboEditToolbar.height) + parseInt(document.all.objGibo2005.height) + parseInt(document.all.tblGiboViewToolbar.height) : 0;
			
			if(typeof(objFrame) != "undefined") { //gamemain 의견달기는 resizing안함 
				objFrame.style.height = parseInt(objSize.document.body.scrollHeight) + giboObjectHeight;
				objFrame.height = parseInt(objSize.document.body.scrollHeight) + giboObjectHeight;
				
				
				//최소값 설정
				if(parseInt(objFrame.style.height) < 400){
					objFrame.style.height = 400;				
				}
				
				var parentWindowName = parent.window.name;
				if( !(parentWindowName==null || parentWindowName=='') ) {
					var strEval = "parent.parent.document.all." + parentWindowName;
					var ppObjFrame = eval(strEval);
					
					if( typeof(ppObjFrame) != "undefined" ) {
						ppObjFrame.style.height= parseInt(objFrame.height)+200
					}
				}
			}
		}
	} catch(e) {
		if( (typeof(ENV_isDev) != "undefined" && ENV_isDev)
			|| (typeof(ENV_isAlpha) != "undefined" && ENV_isAlpha) 
		) {
			alert(e.message);
		}
	}
}



function goSubPage(subId){
	if("possess" == subId)
		location.href = "/myinfo.nhn?m=buyinfo&tabinfo=possess";
	else if("autopay" == subId)
		location.href = "/myinfo.nhn?m=buyinfo&tabinfo=autopay";
		//alert("준비 중입니다.");
	else if("sendgift" == subId)
		location.href = "/myinfo.nhn?m=buyinfo&tabinfo=sendgift";
	else if("recvgift" == subId)
		location.href = "/myinfo.nhn?m=buyinfo&tabinfo=recvgift";
	else if("recharge" == subId)
		location.href = "/myinfo.nhn?m=recharge";	
	else;		
}

function goShop(){

	if(!opener)
		return;

	opener.location.href = "/shop/pass.nhn";
	window.close();	
}

//팝업 window
function openWindow(sURL, sWindowName, width, height, scrollyn) {
	var left = (screen.availWidth - width) / 2;
	var top = (screen.availHeight - height) / 2;

	var features = "toolbar=no, channelmode=no, location=no, directories=no, resizable=no, menubar=no";
	features = features + ", scrollbars=" + scrollyn + ", left=" + left + ", top=" + top + ", width=" + width + ", height=" + height;

	var wndPopup = window.open( sURL, sWindowName, features);
	return wndPopup;	
}

function openInfo(tab, id) {	
	var url = "/myinfo.nhn?m=" + tab + "&encoding=utf-8&encMemberId=" + id;	
	openWinNative( url, "wndTetrisMyInfo", 600, 500);
}

function goPcCheck(){
	var expectedWidth = 550;
	var expectedHeight = 400;
	var WindowLeft = (screen.availWidth - expectedWidth)/2;
	var WindowTop = (screen.availHeight - expectedHeight)/2;
	openWinNative( "/common/publish/popupSpecAnalyzer_standalone.jsp?action=/popup/checkpc.nhn?m=specAnalyze", "pccheck", expectedWidth, expectedHeight );
}


