var _bDev  = (location.href.indexOf("http://dev-")==0)  ;
var _local = (location.href.indexOf("http://www-local."));
var _bAlpha = location.href.indexOf("http://www-alpha2.")==0;
var _bAlpha2 = location.href.indexOf("http://www-alpha.")==0;
var _bBeta = location.href.indexOf("http://www-beta.")==0;
var _bReal = location.href.indexOf("http://www.r2online.")==0;
var _urlPath  = (_local)? "http://www-local.r2online.cn" : ((_bAlpha2)? "http://www-alpha2.r2online.cn" : ((_bBeta)? "http://www-beta.r2online.cn" : "http://www.r2online.cn"));





function addEventHandler( eventName, handler ) {
	if( window.addEventListener ) {
		window.addEventListener( eventName, handler, false );
	} else if( window.attachEvent ) {
		window.attachEvent( eventName, handler );
	}
}

function gethbCookie(name)
{
	var nameOfCookie = name + "=";
	var x = 0;
	while (x <= document.cookie.length)
	{
		var y = (x+nameOfCookie.length);
		if (document.cookie.substring(x, y) == nameOfCookie)
		{
			if ((endOfCookie = document.cookie.indexOf(";", y)) == -1)
				endOfCookie = document.cookie.length;
			return unescape(document.cookie.substring( y, endOfCookie));
		}
		x = document.cookie.indexOf(" ", x) + 1;
		if (x == 0)
			break;
	}
	return "";
}
function goTop(bbsid,gameid){
	var s = (bbsid!="") ? "/bbs.nhn?bbsid="+bbsid : "bbs.nhn?gameid=" + gameid;
	s += "&p=Y";
	document.location.href = s;
}
function goMyBugList(memberid) {
	var f = document.BbsList;
	f.searchKey.value = memberid;
	f.searchType.value = "writeuid";
	checkSearchKey();
}

function toggleObject(obj, disp) {
	if (typeof(obj) == "object") {
		if (typeof(disp) != "undefined") {
			obj.style.display = disp;
		}
		else {
			if (obj.style.display == "block") {
				obj.style.display = "none";
			} else {
				obj.style.display = "block";
			}
		}
	}
}

function goWriteForm(bbsid) {
	var s = _urlPath + "/bbs.nhn?m=insertForm&bbsid=" + bbsid;
	location.href = s;
}



function openPatchWin() {
	openWin(_urlPath + "/edit.nhn?m=patch", "editorPatchWin", 357, 249);
}

function showPhoto(attachurl) {
	var win = openWinFreePos(_urlPath + "/showPhoto.nhn?attachurl=" + attachurl, "showPhotoWin", "500", "400", 0, 0);
	//win.focus();
}

function showWallpaper(bbsid, docid, idx) {
	var s = _urlPath + "/download.nhn?m=downloadWallpaper&bbsid="+bbsid+"&docid="+docid+"&idx="+idx
	var win = openWinFreePos(s, "showPhotoWin", "500", "400", 0, 0);
	//win.focus();
}
 
function initOnLoad() {
	document.bbsWriteForm.title.focus();
}



function setEditMode() {
	var bHTML = document.all.htmlinput.checked;
	if (bHTML) {

		document.bbsWriteForm.htmlyn.value = "Y";
	} else {

		document.bbsWriteForm.htmlyn.value = "N";		
	}
}


function read(docid, bbsid, targetWin) {
	BbsList.docid.value = docid;
	BbsList.m.value="read";
	BbsList.view.value="";
//	if (typeof(bbstype) != "undefined" && typeof(BbsList.bbstype) == "object") {
//		BbsList.bbstype.value = bbstype;
//	}
	BbsList.action= _urlPath + "/bbs.nhn";	
	BbsList.target = "_self";
	if (targetWin == "new") {
		BbsList.target = "_blank";
	}
	if(targetWin == "top") {
		BbsList.target = "_top";
	}

	BbsList.submit();

}

function openWinNormal(sURL, sWindowName, w, h, sScroll, x, y) {
	if (typeof(x) != "number" && typeof(y) != "number") {
		x = (screen.width - w) / 2;
		y = (screen.height - h) / 2;
	}
	if (sScroll==null) sScroll = "no";
	var sOption = "";
	sOption += "toolbar=no,channelmode=no,location=no,directories=no,resizable=no,menubar=no";
	sOption += ",scrollbars="+sScroll+",left="+x+",top="+y+",width="+w+",height="+h;

	var win = window.open(sURL, sWindowName, sOption);
	return win;
}


function checkSearchKeyDown() {
	if (event.keyCode == 13) {
		checkSearchKey();
	}
}

function viewReplyStep(cnt, img) {
	var len = parseInt(cnt);
	var step = "";
	if (len > 1) {
		for (i=1;i<len;i++) {
			step = step + "<img src=\"" + img + "\" hspace=2>";
		}
	}
	return step;
	//document.write(step);
}


function getRealSize(str) {
	while (str.indexOf(' ') > -1) {
		str = str.replace(' ' , '');	
	}
	return str.length;
}


function getStringSize(str) { 
	var i, len=0;
	for(i=0;i < str.length; i++) (str.charCodeAt(i) > 255)? len+=2:len++;
	return len;
}


function isImage(str, atypes) {
	var pos = str.lastIndexOf('.');
	var ext = str.substring(pos+1).toLowerCase();
	if (atypes == null) {
		atypes = new Array('gif','jpg','jpeg');
	}
	for (var i=0; i<atypes.length; i++) {
		if (ext == atypes[i]) {
			return true;
		}
	}
	return false;
}

function toKB(bytes) {
	return (Math.ceil(bytes * 100 / 1024.0) / 100);
}


function resizeImage(num) {
    var oImg = eval("document.getElementById('userImg" + num + "')");
    if (oImg.width > 530) oImg.width = 530;
}


function HE_resizeImage(objImg) {
  	if( objImg != null && objImg.width > 530 ) {
		objImg.width = 530;
	}
}

function HE_getImgId() {
	var d = new Date();
	var curr_min = d.getMinutes();
	var curr_sec = d.getSeconds();
	var curr_msec = d.getMilliseconds();
	return (curr_min * 60 + curr_sec) * 1000  + curr_msec;
}

function saveMedia(mtype, url, alignment, width_x, height_y, from){
	if (typeof width_x=="undefined") width_x=0;
	if (typeof height_y=="undefined") height_y=0;
	if (typeof from=="undefined") from="";
	
	
	
	if( mtype == 'image' ) {
		var strImg = "";
		var showOriginPics = (from=='fromWeb') ? "" : "style=\"CURSOR: pointer;\"";
		var imgId = (from=='fromWeb') ? "": HE_getImgId();
		// In editing mode, HE_resizeImage in htmleditor.js is called. But the function in bbs.js is called in reading mode.
		var resizeImg = (from=="fromWeb") ? "" : "onload=HE_resizeImage(this);";
       
		strImg = "<img id=\"userImg" + imgId + "\" " + resizeImg +" src='" + url + "' " + showOriginPics + "><BR clear='all'>"; 

		if( alignment=='top' || alignment=='NA') {
			strImg = "<CENTER>" + strImg + "</CENTER>";
			var html = objEditor.getContent();
			objEditor.setContent(  html + strImg);
			
		} else if(alignment=='bottom'){
			// html = "<BR>" + "<CENTER>" + "<img id=userImg"+imgId+" "+ resizeImg +" src='" + url + "' " + showOriginPics + ">" + "</CENTER>";
			// rng.pasteHTML(html);
		} else if(alignment=='left'){
			// html = "<img id=userImg"+imgId+" "+ resizeImg +" src='" + url + "' " + showOriginPics + " align='" + alignment + "'>&nbsp;";
			// rng.pasteHTML(html);
		} else if(alignment=='right'){
			// html = "&nbsp;<img id=userImg"+imgId+" "+ resizeImg +" src='" + url + "' " + showOriginPics + " align='" + alignment + "'>";
			// rng.pasteHTML(html);
		} else if(alignment=='NA'){
			// html = "<img id=userImg"+imgId+" "+ resizeImg +" src='" + url + "' " + showOriginPics + ">";
			// rng.pasteHTML(html);
		}
		
		 
	}
}

function setMediaInfo(mtype, atype){
	mediaType = mtype;
	alignType = atype;
}

function setEditMode() {
	var bHTML = document.all.htmlinput.checked
	if (bHTML) {
		document.bbsWriteForm.htmlyn.value = "Y";
	} else {
		document.bbsWriteForm.htmlyn.value = "N";		
	}
}

function setIsUploading(val){
	var f = document.form0;
	f.isUploading.value = val;
}

function getIsUploading(){
	var f = document.form0;
	return f.isUploading.value;
}
function allCheck(){
	var delform = document.deleteform;
	var size = delform.docids.length;
	var checked = document.deleteform.allchk.checked;
	for(var i=0;i<size;i++){
		delform.docids[i].checked = checked;
	}
}

function isHangameLogined() {
	var hglogin = getStrCookie("login");
	if ( hglogin.length > 0 )
		return true;
	else
		return false;
}

function getStrCookie(sName)
{


	var aRec, aCook = document.cookie.split("; ");

	for (var i=0; i<aCook.length; i++) {
		aRec = aCook[i].split("=");
		if (sName.toLowerCase()==unescape(aRec[0].toLowerCase())) return aRec[1];
	}

	return "";
}

function viewActiveObject(html){
	document.write(html);
}


var arrWndOpenWindow = new Array();

function push(array, arg){
	array[array.length] = arg;
}
function pop(array){
	var obj = array[0];
	var temp = new Array();
	for ( i = 1; i < array.length; i++ ){
		temp[i-1] = array[i];
	}
	array = temp;
	return obj;
}

function controlOpenWindow(strURL, strWndName, strOptions) {
	var arrTemp = new Array();
	var wndTemp;
	var wndName;
	
	var arrWndOpenWindow = getArrWndOpenWindow();
	if ( arrWndOpenWindow == null) {
		arrWndOpenWindow = this.arrWndOpenWindow;
	}
	for ( i = 0; i < arrWndOpenWindow.length; i++ ) {
		wndTemp = arrWndOpenWindow[i];
		if ( wndTemp == null) {
			continue;
		}
		if ( !wndTemp['window'] ) {
			continue;
		}
		if ( wndTemp['window'].closed ) {
			arrWndOpenWindow[i] = null;
			continue;
		}
		wndName = wndTemp['name'];
		if ( wndName != "" && strWndName == wndName ) {
			wndTemp['window'].location.replace(strURL);

			push(arrTemp, wndTemp);
			for ( j = 0; j < arrTemp.length; j++ ) {
				push(arrWndOpenWindow, arrTemp[j]);
			}
			return null;
		}
		push(arrTemp, wndTemp);
	}
	for ( i = 0 ; i < arrTemp.length; i++ ) {
		push(arrWndOpenWindow, arrTemp[i]);
	}

	var	wndNewWindow = window.open(strURL, '', strOptions);
	
	var wndSaveWindow = new Array();
	wndSaveWindow['name'] = strWndName;
	wndSaveWindow['window'] = wndNewWindow;

	push(arrWndOpenWindow, wndSaveWindow);
	return wndNewWindow;
}

function getArrWndOpenWindow() {
	return top.arrWndOpenWindow;
}

function openWinFreePos(sURL, sWindowName, w, h, x, y, sScroll)
{

	if (sScroll==null) sScroll = "no";
	if (screen.width == 800 && screen.height== 600 ) sScroll = "yes"; 
	
	var sOption = "";
	sOption = sOption + "toolbar=no, channelmode=no, location=no, directories=no, resizable=no, menubar=no";
	sOption = sOption + ", scrollbars=" + sScroll + ", left=" + x + ", top=" + y + ", width=" + w + ", height=" + h;
	var win = controlOpenWindow(sURL, sWindowName, sOption);
	return win;
}

function fitPopupSize_adjustPosition( toWidth, toHeight ) {
	var posLeft = (window.screenLeft) ? window.screenLeft : window.screenX;
	var posTop = (window.screenTop) ? window.screenTop : window.screenY;
	var adjustLeft = 0;	var marginWidth = 50;
	var adjustTop = 0;	var marginHeight = 50;
	var movingRequired = false;

	if( posTop + toHeight + marginHeight > screen.availHeight ) {
		adjustTop = -(posTop + toHeight + marginHeight - screen.availHeight);
		movingRequired = true;
	}
	if( posLeft + toWidth + marginWidth > screen.availWidth ) {
		adjustLeft = -(posLeft + toWidth + marginWidth - screen.availWidth);
		movingRequired = true;
	}
	if( movingRequired ) {
		window.moveBy( adjustLeft, adjustTop );
	}
}

function fitPopupSize_resize( toWidth, toHeight ) {
	var oBody = document.body;
	if( oBody == null ) { return; }

	if( typeof(window.innerHeight) != "undefined" && typeof(window.innerWidth) != "undefined" ) {
		window.innerHeight = toHeight;
		window.innerWidth = toWidth;
	} else {
		var diffX = toWidth - oBody.clientWidth;
		var diffY = toHeight - oBody.clientHeight;

		window.resizeBy( diffX, diffY );
	}

}

function fitPopupSizeEx() {
	var oBody = document.body;
	if( oBody == null ) { return; }
	
	var idPrefix = "page_content";
	var objId = "";
	var chkResult = false;
	var objChk = null;
	
	for( var i=65; i<=90; i++ ) {
		objId = idPrefix + String.fromCharCode(i);
		objChk = document.getElementById(objId);
		if( objChk == null ) { break; }
	}
	if( objChk != null ) { return; }

	var objOut = window.document.createElement("<div id='" + objId + "' style='position:absolute;left:0;top:0;'>");

	var existNodeList = window.document.body.childNodes;
	while( existNodeList.length > 0 ) {
		objOut.appendChild( existNodeList[0] );
	}
	document.body.appendChild( objOut );

	var toWidth = objOut.clientWidth;
	var toHeight = objOut.clientHeight;
	if( toWidth == null && toHeight == null ) { return; }

	fitPopupSize_adjustPosition( toWidth, toHeight );

	fitPopupSize_resize( toWidth, toHeight );
}
