// 댓글에 대한 댓글을 등록하려고 할때 input 영역을 이동시키는 함수
function rearrangeNode(target, pars)
{
	// Next Level 이 존재할 경우에만 답글 허용
	if(pars.childNodes[2].value)
	{
		var commentRow = $('commentRow');
		$('commentNode').insertBefore(commentRow, target.nextSibling);
		$('sequence').value = pars.childNodes[0].value;
		$('parent').value = pars.childNodes[1].value;
		$('level').value = pars.childNodes[2].value;
	}
	else
	{
		alert("답글은 5단계 이상 작성할 수 없습니다");
	}
}

// 삭제 query 를 받아 들이는 함수
function deleteBBS(mno, request)
{
	if(mno==0)
	{
		IEprompt("비밀번호를 입력하세요<br>게시글과 관련 댓글도 모두 삭제됩니다.", "",request);
	}
	else
	{
		var chk = confirm('게시물을 삭제하시겠습니까?\n관련 댓글도 모두 삭제됩니다.');
		if(chk)
		{
			promptCallback(request, null);
		}
	}
}

// 삭제 request를 날리는 함수
function promptCallback(request, val)
{
	if(val)
	{
		request += "&password=" + val;
	}
	window.location.href=request;
}

// 텍스트 편집기 함수
function startXquared()
{
	window.xed;
	window.xed = new xq.Editor("editor");
	window.xed.setEditMode('wysiwyg');
	window.xed.focus();
}

// 플래시 업로드
function startSwfUpload(limit, session, bno, requestUrl)
{
	var settings =
	{
		flash_url : "/Haja/common/libs/swfUpload/Flash/swfupload.swf",
		upload_url: requestUrl,
		post_params:
		{
			"PHPSESSID": session,
			"bno": bno,
			"mode": "upload"
		},

		// Button Settings
		button_image_url : "/Haja/common/libs/swfUpload/Flash/XPButtonUploadText_61x22.png",
		button_placeholder_id : "spanButtonPlaceholder",
		button_width: 61,
		button_height: 22,

		file_size_limit : limit,
		file_types : "*.*",
		file_types_description : "All Files",
		file_upload_limit : 20,
		file_queue_limit : 0,
		custom_settings :
		{
			progressTarget : "fsUploadProgress",
			cancelButtonId : "btnCancel"
		},
		debug: false,

		// The event handler functions are defined in handlers.js
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,
		queue_complete_handler : queueComplete	// Queue plugin event
	}

	window.swfu = new SWFUpload(settings);
}

// 이미지를 xquard 에 붙여 놓기
function putImage(bno, obj)
{
	var file = obj.options[obj.selectedIndex].title;
	var fileName = obj.options[obj.selectedIndex].className;

	if(!file)
	{
		alert("삽입할 이미지를 선택하여 주십시요.");
	}
	else
	{
		var doc = window.xed.getDoc();
		var block = doc.createElement("div");
		var img = doc.createElement("img");
		img.src = "http://haja.net/common/download.php?mode=view&type=bbsData&id=" + bno + "&file=" + fileName;

		// 이미지 가로 길이 확이해서 스쿼드 body 길이보다 길 경우 사이즈 줄임
		var oImg = new Image();
		oImg.src = "http://haja.net/common/download.php?mode=view&type=bbsData&id=" + bno + "&file=" + fileName;
		if(oImg.width > doc.body.offsetWidth)
		{
			img.style.width = doc.body.offsetWidth + "px";
			img.style.height = (oImg.height*doc.body.offsetWidth) / oImg.width + "px";
		}

		block.appendChild(img);
		doc.body.appendChild(block);
	}
}

// 게시물 관리 레이어 보이기
function manageList(event)
{
	var x = event.clientX;
	var y = event.clientY;

	var yOffSet = document.documentElement.scrollTop;
	var xOffSet = document.documentElement.scrollLeft;
			
	$("adminLayer").style.left = x + "px";
	$("adminLayer").style.top = y + "px";

	var checkboxes = getElementsByClass("input", null, $("listForm"));
	for(var i=0; checkboxes.length > i; i++)
	{
		if(checkboxes[i].type=="checkbox", checkboxes[i].checked==true)
		{
			$("adminLayer").style.display = "block";
			return true;
		}
	}

	alert("선택된 게시물이 없습니다");
	return false;
}

// 삭제 query 를 받아 들이는 함수
function deleteList()
{
	var checkBox = true;
	var checkboxes = getElementsByClass("input", null, $("listForm"));
	for(var i=0; checkboxes.length > i; i++)
	{
		if(checkboxes[i].type=="checkbox", checkboxes[i].checked==true)
		{
			var checkBox = false;
			break;
		}
	}

	if(checkBox)
	{
		alert("선택된 게시물이 없습니다");
	}
	else
	{
		var chk = confirm('게시물들을 삭제하시겠습니까?\n관련 댓글도 모두 삭제됩니다.');
		if(chk)
		{
			$("requestType").value = "deleteList";
			$("listForm").submit();
		}
	}
}

// 게시물 이동
function moveList()
{
	var chk = confirm('선택된 게시물들을 이동하시겠습니까?');
	if(chk)
	{
		$("requestType").value = "moveList";
		$("target1").value = $("groupSelect").options[$("groupSelect").selectedIndex].value;
		$("target2").value = $("bbsSelect").options[$("bbsSelect").selectedIndex].value;
		$("listForm").submit();
	}
}

// 게시물 복사
function copyList()
{
	var chk = confirm('선택된 게시물들을 복사하시겠습니까?');
	if(chk)
	{
		$("requestType").value = "copyList";
		$("target1").value = $("groupSelect").options[$("groupSelect").selectedIndex].value;
		$("target2").value = $("bbsSelect").options[$("bbsSelect").selectedIndex].value;
		$("listForm").submit();
	}
}

// 게시판 목록 가져오기
function getBBSList(gno, requestURL)
{
	// JSON 데이터 처리 함수
	function getJSON(originalRequest)
	{
		var jsonData = originalRequest.responseText.evalJSON();

		while($("bbsSelect").firstChild)
		{
			//The list is LIVE so it will re-index each call
			$("bbsSelect").removeChild($("bbsSelect").firstChild);
		}

		var defaultOption = document.createElement("option");
		defaultOption.value = "";
		var defaultName = document.createTextNode("게시판 선택");
		defaultOption.appendChild(defaultName);
		$("bbsSelect").appendChild(defaultOption);

		for(var i=0; jsonData.Result.length > i; i++)
		{
			var listName = document.createTextNode(jsonData.Result[i].BoardName);
			var listOption = document.createElement("option");
			listOption.value = jsonData.Result[i].BNO;
			listOption.appendChild(listName);
			$("bbsSelect").appendChild(listOption);
		}
	}

	var pars = "mode=admin&requestType=getBBSList&gno=" + gno;
	var result = new Ajax.Request(requestURL, {method: 'get', asynchronous: true, parameters: pars, onComplete: getJSON, onFailure: function(){ alert('ajax request error') }});
}

