/************************************************/
// 파일명 : default.js
// 작성자 : 남궁주익
// 작성일 : 2009.04.06
// 수정자 : 
// 수정일 : 
// 웹진 전반에 쓰이는 공통 스크립트
/************************************************/

/************************************************/
// 공통 Global 변수 선언
/************************************************/
var root_url = "http://game.chosun.com";

/************************************************/
// 자동완성용 스크립트
/************************************************/
var search_bg_targeted = -1;

function isIE() {
	if (navigator.userAgent.indexOf("MSIE") >= 0) {
		return true;
	} else {
		return false;
	}
}

function search_strip(a,input_box_id) {
	if (a) {
		if ((a == "K" && document.getElementById(input_box_id).value != "") || (a == "M")) {
			document.getElementById(input_box_id).style.backgroundImage = '';
			search_bg_targeted = -1;
		}
	} else {
		document.getElementById(input_box_id).style.backgroundImage = '';
		search_bg_targeted = -1;
	}
}

function searchsubmit() {
	var check = true;
	var sugCurPos = document.frames["hFrame"].curCursorPos;
//	var nil_Search = "btn";
//	if(sugCurPos > -1) nil_Search = "sugtop";
	return check;
}

function search_game(){
	if(!$('id').value && !$('q').value){
		alert('검색어를 선택하신후 선택하세요!');
		return false;
	}else if(!$('id').value && $('q').value){
		$('input_only').submit();
	}else{
		var id = $('id').value.split('-');
		document.location.href = "/article/list.php?cp_id="+id[0]+"&g_id="+id[1];
	}
}

function SchSug_setcoor(input_box_id,url,width) {
	var input_box = document.getElementById(input_box_id);
	var button_position = parseInt(input_box.style.width)+100;
	document.write('<div id="SchSug" style="position:relative; z-index:999999999999999999999999999999999999999; width:'+width+'px;float:left;visibility:visible;">');
	document.write('<iframe id="hFrame" name="hFrame" src="'+url+'&input_box_id='+input_box_id+'" style="width:'+width+'px;display:none;float:left;margin:0px;position:relative; padding:0px;" frameborder="0" scrolling="no"></iframe>');
	document.write('</div>');
	document.write('<div id="set_sugicn" style="width:13px; height:13px; position:absolute; left:'+button_position+'px; top:11px; z-index:1;"><img src="'+root_url+'/global/auto_complete/images/sug_down.gif" width="13" height="13" style="cursor:pointer;display:none;" id="sugicn"></div>');
	document.write('<div id="set_sugicnUP" style="width:13px; height:13px; position:absolute; left:'+button_position+'px; top:11px; z-index:1;"><img src="'+root_url+'/global/auto_complete/images/sug_up.gif" width="13" height="13" style="cursor:pointer;display:none;" id=s"ugicnUP"></div>');

	if(isIE()) document.getElementById('set_sugicn').style.display = '';
}

function list_SchSug_setcoor(input_box_id,url,width) {
	var input_box = document.getElementById(input_box_id);
	var button_position = parseInt(input_box.style.width)-15;
	document.write('<div id="SchSug" style="position:relative; z-index:999999999; width:'+width+'px;">');
	document.write('<iframe id="hFrame" name="hFrame" src="'+url+'&input_box_id='+input_box_id+'" style="width:'+width+'px;display:none;margin:0px; padding:0px;" frameborder="0" scrolling="no"></iframe>');
	document.write('</div>');
	document.write('<div id="set_sugicn" style="width:13px; height:13px; position:absolute; left:'+button_position+'px; top:7px; z-index:1;"><img src="'+root_url+'/global/auto_complete/images/sug_down.gif" width="13" height="13" style="cursor:pointer" id="sugicn"></div>');
	document.write('<div id="set_sugicnUP" style="width:13px; height:13px; position:absolute; left:'+button_position+'px; top:7px; z-index:1;"><img src="'+root_url+'/global/auto_complete/images/sug_up.gif" width="13" height="13" style="cursor:pointer" id=s"ugicnUP"></div>');

	if(isIE()) document.getElementById('set_sugicn').style.display = '';
}



/************************************************/
// 글자갯수 체크
/************************************************/
// check_length(textarea,출력될곳,제한길이);
function check_length(outobj,inobj,lengths){
	var txt = outobj.value;
	var count = txt.length;
	if(count > lengths){
		alert(lengths+"자 이상 쓰실수 없습니다!");
		//글자르는 부분 추가해야함...
	}else{
		inobj.innerHTML = count;
	}
}

/************************************************/
// 레이어 중앙으로 띄우기
/************************************************/
function centerWindow(value) {
	var bodyWidth    = document.body.clientWidth;
	var bodyHeight    = document.body.clientHeight;
	var divWidth    = document.all[value].offsetWidth;
	var divHeight    = document.all[value].offsetHeight;
	var divLeft = 0, divTop = 0;

	if(bodyWidth > divWidth) {        // left 값 구하기
		divLeft = Math.ceil((bodyWidth - divWidth) / 2);
	}
	
	if(bodyHeight > divHeight) {    // top 값 구하기
		divTop = Math.ceil((bodyHeight - divHeight) / 2);
	}
	
	document.all[value].style.left = divLeft;
	document.all[value].style.top = divTop;
}


/************************************************/
// 댓글쓰기
/************************************************/
var now_cmt_position = '';
var cmt_mode = '';
function addcomment(no){
	if (!getCookie("userid")) {
		/*
		if(!$("nickname").value){
			alert('닉네임을 입력하세요!');
			$("nickname").focus();
			return false;
		}
		if(!$("cmt_passwd").value){
			alert('비밀번호를 입력하세요!');
			$("cmt_passwd").focus();
			return false;
		}
		*/
		//alert('* 로그인을 하셔야 댓글을 작성하실 수 있습니다.');
		//return false;
	}

	if(!$("content_txt").value){
		alert('내용을 입력하세요!');
		$("content_txt").focus();
		return false;
	}
	new Ajax.Request(
	root_url+'/global/comment/a_cmt_write.php',
	{
		method: 'post',
		parameters: $("cmt_form").serialize(),
		onSuccess: function(transport) {
			//alert(transport.responseText);
			if(transport.responseText == 2){
				alert("이미 사용되고 있는 닉네임입니다.");
				return false;
			}else if(transport.responseText == 1){
				$("content_txt").value = "";
				//position_comment_write(0,0,0,0);
				//comment_list(no,cmt_mode);
				location.reload();
			}else if(transport.responseText == 3){
				alert("* 로그인을 하셔야 댓글을 작성하실 수 있습니다.");
				return false;
			}else{
				alert('댓글 쓰기중 오류가 발생했습니다.');
			}
		}
	});
}

function delcomment(article_no,no,ord,depth,group,temp){
	if(confirm("정말로 삭제하시겠습니까?")){
		if(article_no && !no && !ord && !depth){
			var pars = $("pars").value+'&pw='+$("del_pw").value;
		}else{
			var pars = 'article_no='+article_no+'&no='+no+'&ord='+ord+'&depth='+depth+'&group='+group;
		}
		new Ajax.Request(
		root_url+'/global/comment/a_cmt_del.php',
		{
			method: 'get',
			parameters : pars,
			onSuccess: function(transport) {
				if( transport.responseText == 1 ){
					alert("댓글이 삭제 되었습니다.");
					//comment_list(article_no,cmt_mode);
					location.reload();
				}else if(transport.responseText == 2){
					alert('하위 글이 있어 삭제 하실 수 없습니다.');
				}else if(transport.responseText == 3){
					alert('해당 글을 삭제할 권한이 없습니다.');
				}
			}
		});
		$("cmtPwbox").style.display = "none";
		$("del_pw").value="";
	}
}

function delcomment2(article_no,no,ord,depth,group,mode){
	var pars = 'article_no='+article_no+'&no='+no+'&ord='+ord+'&depth='+depth+'&group='+group+'&temp=0';
	$("pars").value = pars;
	$("cmtPwbox").style.display = 'inline';
	$("del_pw").focus();
}

//***************************************//
//코멘트 가져오기
// 2009.08.19 AJAX 브라우저 호환 문제로 view.php에서 직접 불러오게 변경
// comment_list 사용안함
//***************************************//
function comment_list(no,mode){
	cmt_mode = mode;
	var pars = 'no=' + no + '&mode=' + mode;
	new Ajax.Updater(
	'boxComment',
	root_url+'/global/comment/cmt_list.php',
	{
		method:'get',
		parameters : pars,
		onLoading  : function() {
			//코멘트 불러오는중 표시
			document.getElementById("boxComment").innerHTML ='<div style="font:9pt #000;text-align:center;">댓글을 불러오는 중입니다.</div>';
		},
		onSuccess: function() {
			//alert("성공");
		}
	});
}

function position_comment_write(no,ord,depth,group){
	if(now_cmt_position == ''){
		var cmt_form_box = document.getElementById('cmt_form_box');
	}else{
		var cmt_form_box = document.getElementById('cmt_write_box'+now_cmt_position);
	}
	
	if(no){
		var cmt_write_box = document.getElementById('cmt_write_box'+no);
	}else{
		var cmt_write_box = document.getElementById('cmt_form_box');
	}

	var cmt_box = cmt_form_box.innerHTML;

	cmt_form_box.innerHTML = "";

	if (now_cmt_position == no){
		document.getElementById('cmt_form_box').innerHTML = cmt_box;
		if(no){
			cmt_write_box.style.display = "none";
		}else{
			cmt_write_box.style.display = "";
		}
		now_cmt_position = '';
		document.getElementById('depth').value = 0;
		document.getElementById('ord').value = "";
		document.getElementById('group').value = group;
	}else{
		cmt_write_box.innerHTML = cmt_box;
		cmt_write_box.style.display = "";
		now_cmt_position = no;
		document.getElementById('depth').value = depth;
		document.getElementById('ord').value = ord;
		document.getElementById('group').value = group;
	}
}

/************************************************/
// 로그인
/************************************************/
function login_check() {
	if(!$("userid").value || $("userid").value == null){
		alert("아이디를 입력하세요!");
		$("userid").focus();
		return false;
	}
	if(!$("passwd").value || $("passwd").value == null){
		alert("비밀번호를 입력하세요!");
		$("passwd").focus();
		return false;
	}
	new Ajax.Request(root_url+'/global/login/login_result.php',
	{
		method: 'post',
		postBody: $('loginForm').serialize(),
		//onLoading  : function() {
			//$("loginFormBox").style.display = "none";
		//},
		onSuccess: function(transport){
			//alert(transport.responseText);
			if( transport.responseText == 1 ){
				//alert('성공');
				//$("loginFormBox").style.display = "none";
				//location.reload();
				$('loginForm').submit();
			}else{
				alert('아이디나 비밀번호를 확인해주세요!');
				//$("loginFormBox").style.display = "";
			}
		}
	});
}

function logout2() {
	var pars = 'mode=logout';
	$('testbox').innerHTML = "1";
	new Ajax.Request(root_url+'/global/login/login_result.php',
	{
		method: 'get',
		parameters : pars,
		onLoading  : function() {
			$('testbox').innerHTML = "2";
			$("loginFormBox").style.display = "none";
			$("loginState").innerHTML += "로그아웃중입니다.";
		},
		onSuccess: function(transport){
			$('testbox').innerHTML = "3";
			location.reload();
		}
	});
}

function logout() {
	location.href=root_url+'/global/login/login_result.php?mode=logout';
	//$('blankFrame').location.href="https://login.gamechosun.co.kr/login/logoutOK.jsp";
}


/************************************************/
// 쿠키관련 함수
/************************************************/
//쿠키값 가져오기
function getCookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
	if (offset != -1) {
		offset += search.length
		end = document.cookie.indexOf(";", offset);
	if (end == -1) end = document.cookie.length;
		returnvalue=document.cookie.substring(offset, end);
	}
}
return returnvalue;
}

//쿠키생성
// ex) setCookie('popup','done',1);
function setCookie(name,value,repeat){
	var todayDate = new Date();
	var curtime= todayDate.getTime();
	todayDate.setTime( curtime + (60*60*24*1000 / repeat) );
	document.cookie = name + '=' + escape( value ) + '; path=/; expires=' + todayDate.toGMTString();
	return;
}

//쿠키삭제
function clearCookie(name){
	var today = new Date();
	var expire_date = new Date(today.getTime() - 60*60*24*1000);
	document.cookie = name + "= " + "; expires=" + expire_date.toGMTString();
}

/************************************************/
// Good, Bad
/************************************************/
function goodbad(where,no,vote){
	var pars = 'where='+where+'&no='+no+'&vote='+vote;
	new Ajax.Request(root_url+'/global/good_bad/a_goodbad.php',
	{
		method: 'get',
		parameters : pars,
		onSuccess: function(transport){
			if( transport.responseText == 1 ){
				alert('해당 글을 추천하였습니다.');
			}else if( transport.responseText == 2 ){
				alert("이미 해당 글에 대한 평가를 하셨습니다.");
			}else if( transport.responseText == 3 ){
				alert("로그인후 평가하실 수 있습니다.");
			}
		}
	});
}

/************************************************/
// Good, Bad
/************************************************/
/*입력 글자수 체크*/
function cal_pre(cmt) {
	var tmpStr;

	tmpStr = $('content_txt').value;
	cal_byte(tmpStr,cmt);
}

function cal_byte(aquery,cmt) {
	var tmpStr;
	var temp=0;
	var onechar;
	var tcount;
	tcount = 0;

	tmpStr = new String(aquery);
	temp = tmpStr.length;

	for (k=0;k<temp;k++) {
		onechar = tmpStr.charAt(k);
		if (escape(onechar) =='%0D') { } else if (escape(onechar).length > 4) { tcount += 2; } else { tcount++; }
	}
	
	document.getElementById('txt_count').innerHTML = tcount;
	if(tcount>(600/2)) {
		reserve = tcount-600;
		alert("300자 까지만 작성하실 수 있습니다.");

		nets_check($('content_txt').value,cmt);
	return;
	}

}

function nets_check(aquery,cmt) {
	var tmpStr;
	var temp=0;
	var onechar;
	var tcount;
	tcount = 0;

	tmpStr = new String(aquery);
	temp = tmpStr.length;

	for(k=0;k<temp;k++) {
		onechar = tmpStr.charAt(k);

		if(escape(onechar).length > 4) {
			tcount += 2;
		} else {
			// 엔터값이 들어왔을때 값(\r\n)이 두번실행되는데 첫번째 값(\n)이 들어왔을때 tcount를 증가시키지 않는다.
			if(escape(onechar)=='%0A') {
			} else {
				tcount++;
			}
		}

		if(tcount>(600/2)) {
			tmpStr = tmpStr.substring(0,k);
			break;
		}
	}

	$('content_txt').value = tmpStr;

	cal_byte(tmpStr,cmt);
}


/************************************************/
// 메인 포커스
/************************************************/
function show_detail(no){
	for(i=1;i<=3;i++){
		if(i != no){
			$('IMGS_ROLL_DETAIL'+i).style.display = 'none';
			if(i == 2){
				$('IMGS_ROLL_SMALL'+i).style.borderTop = '3px solid #444444';
				$('IMGS_ROLL_SMALL'+i).style.borderBottom = '3px solid #444444';
				$('IMGS_ROLL_SMALL'+i).style.borderLeft = '2px solid #444444';
				$('IMGS_ROLL_SMALL'+i).style.borderRight = '2px solid #444444';
			}else{
				$('IMGS_ROLL_SMALL'+i).style.border = '3px solid #444444';
			}
		}else{
			$('IMGS_ROLL_DETAIL'+no).style.display = 'block';
			if(i == 2){
				$('IMGS_ROLL_SMALL'+i).style.borderTop = '3px solid #ff0000';
				$('IMGS_ROLL_SMALL'+i).style.borderBottom = '3px solid #ff0000';
				$('IMGS_ROLL_SMALL'+i).style.borderLeft = '2px solid #ff0000';
				$('IMGS_ROLL_SMALL'+i).style.borderRight = '2px solid #ff0000';
			}else{
				$('IMGS_ROLL_SMALL'+i).style.border = '3px solid #ff0000';
			}
		}
	}
}

/************************************************/
// 실시간 한줄뉴스
/************************************************/
function banner_roll(div_id, banner_height, banner_delay, banner_speed, this_height){
	var div_tag = document.getElementById(div_id);
	var a_tag, i;

	this_height ++;
	if(this_height < banner_height) {
		div_tag.style.top = -this_height;
		setTimeout("banner_roll('" + div_id + "', " + banner_height + ", " + banner_delay + ", " + banner_speed + ", " + this_height + ");", banner_speed);
	} else {
		a_tag = div_tag.getElementsByTagName("A");
		div_tag.appendChild(a_tag[0]);
		div_tag.style.top = 0;
		setTimeout("banner_roll('" + div_id + "', " + banner_height + ", " + banner_delay + ", " + banner_speed + ", 0);", banner_delay);
	}

	return true;
}

var counter = 2;
var old_counter = 1;
function changeBanner() {
	if(counter > 4){ counter = 1; old_count = 4}

	document.getElementById('main_issue'+old_counter).style.display = 'none';
	document.getElementById('main_issue'+counter).style.display = 'inline';
	old_counter = counter;
	counter++;
}



/************************************************/
// 메인 탭 변경
/************************************************/
var now_tab = 'all';
function switch_tab(tab){
	if(now_tab){
		$('mainTab_'+now_tab).style.display = "none";
	}
	$('mainTab_'+tab).style.display = "block";
	now_tab = tab;
}

var now_tab2 = '1';
function switch_tab2(tab){
	if(now_tab2){
		$('mainTab2_'+now_tab2).style.display = "none";
	}
	$('mainTab2_'+tab).style.display = "block";
	now_tab2 = tab;
}

var now_tab3 = '1';
function switch_tab3(tab){
	if(now_tab3){
		$('mainTab3_'+now_tab3).style.display = "none";
		$('mainTab3_menu'+now_tab3).className = "rest";
	}
	$('mainTab3_'+tab).style.display = "block";
	$('mainTab3_menu'+tab).className = "rest_over";
	now_tab3 = tab;
}

/************************************************/
// 메인 달력
/************************************************/
function view_cal_note(day){
	if(day){
		$('view_note_box').innerHTML = $('note'+day).innerHTML;
	}else{
		$('view_note_box').innerHTML = '';
	}
}

function popup_calendar(){
	popHeight = 787;
	popWidth = 1011;
	winprops = 'height='+popHeight+',width='+popWidth+',top='+(screen.height - popHeight)/2+',left='+(screen.width - popWidth)/2+',menubar=no,hotkey=no,toolbar=no,dependent=no,resizable=no,location=no,scrollbars=yes,status=no'; 
	window.open('http://game.chosun.com/article/calendar.php','calendar',winprops);
}

/************************************************/
// 우측 조회수 덧글 강추 탭 변경
/************************************************/
var now_treeTab = '1';
function switch_treeTab(tab){
	if(now_treeTab){
		$('treeTab_menu'+now_treeTab).className = "treeTab_menu";
		$('right_treeTab'+now_treeTab).style.display = "none";
	}
	$('treeTab_menu'+tab).className = "treeTab_menu_over";
	$('right_treeTab'+tab).style.display = "block";
	now_treeTab = tab;
}

/************************************************/
// 뉴스 검색 탭 변경
/************************************************/
var now_news_search_Tab = '1';
function switch_searchTab(tab){
	if(tab == 1){
		left_size = '374px';
		$('news_search_tab').innerHTML = '게임/게임사';
	}else if(tab == 2){
		left_size = '458px';
		$('news_search_tab').innerHTML = '기사제목';
	}else if(tab == 3){
		left_size = '542px';
		$('news_search_tab').innerHTML = '기간/날짜';
	}
	$('news_search_tab').style.left = left_size;

	if(now_news_search_Tab){
		$('searchTab'+now_news_search_Tab).style.display = "none";
	}

	$('searchTab'+tab).style.display = "block";
	now_news_search_Tab = tab;
}