function autosave() {

	if($('PostText').value != '') {
		var form_action = $('form1').action;
		$('form1').action = '/posts/autosave';
		$('form1').target = "IFRAME_AUTOSAVE";
		$('form1').submit();
		$('form1').target = "";
		$('form1').action = form_action;
		showMessage('글이 자동저장되었습니다.');
	}
	else {
			new Ajax.Request( '/posts/autosave/', { 
					asynchronous:true, 
					evalScripts:true, 
					onComplete:function(t) {
						
					}
				} );
	}

	setTimeout("autosave()", 30000);	
}

function autosave_init() {
	if($('IFRAME_AUTOSAVE') !=null && $('form1') != null && $('PostText') != null) {
		setTimeout("autosave()", 30000);	
	}
}

addLoadEvent(autosave_init);




function cmtview(pid, authkey, parent_id, cmt_id) {
	viewpid = pid;
	pid = pid.replace('desc','');

	if($('today_comment_page') != null) {
		parent.document.getElementById('IFRAME_TODAY_COMMENT').src = '/tops/peoples_comment/'+pid+'/page:'+$('today_comment_page').value;
		return;
	}

	new Ajax.Request( '/postscomments/getreply/' + viewpid + '/' + authkey , { 
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(t) {
				try {
					$('post_writereply_'+pid).style.display = 'none';
					$('post_writereply_'+pid).style.display = '';
					$('post_replies_'+pid).innerHTML =  t.responseText;

					if(cmt_id>0) {
						if($('PostsComments_'+cmt_id) != null) {
							new Effect.Highlight($('PostsComments_'+cmt_id), { startcolor: '#ffff99', endcolor: '#fffffff' });
						}
					}
				
					if(top != null) {
						if (parent.do_resize!=null) {
							parent.do_resize('IFRAME_TODAY_COMMENT');
						}
					}
					


				}
				catch(e) {
					alert(e);
				}
			
			}
		} );

}


function cmtview_paging(pid, authkey, page) {
	viewpid = pid;
	pid = pid.replace('desc','');

	new Ajax.Request( '/postscomments/getreply/' + viewpid + '/' + authkey+'/page:'+page , { 
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(t) {
				try {
					$('post_writereply_'+pid).style.display = 'none';
					$('post_writereply_'+pid).style.display = '';
					$('post_replies_'+pid).innerHTML =  t.responseText;

					if(top != null) {
						if (parent.do_resize!=null)
							parent.do_resize('IFRAME_TODAY_COMMENT');
					}

					if($('people_today_paging') !=null ) {
						$('people_today_paging').style.display = '';
						if(top != null) {
							if (parent.do_resize!=null)
								parent.do_resize('IFRAME_TODAY_COMMENT');
						}
					}

				}
				catch(e) {
					alert(e);
				}
			
			}
		} );

}


var LimitBytes = 4000;
// 4000바이트 이상 문장 입력시 경고
function ContentsCheck(objTextArea, objBytes) {
	try {
			$('byte_b'+objBytes).innerHTML = objTextArea.value.bytes(); 

			if(objTextArea.value.bytes()>LimitBytes) {
				alert(LimitBytes+"바이트 이상은 입력할 수 없습니다."); 
				objTextArea.value = objTextArea.value.cut(LimitBytes);
				$('byte_b'+objBytes).innerHTML = objTextArea.value.bytes(); 
				//ContentsCheck(objTextArea, objBytes);
			}

	}
	catch (e)
	{
		alert(e.message);
	}
}

var cmtLimitBytes = 400;
// 200바이트 이상 문장 입력시 경고
function CommentCheck(objTextArea, objBytes) {
	try {
			$('byte_'+objBytes).innerHTML = objTextArea.value.bytes(); 

			if(objTextArea.value.bytes()>cmtLimitBytes) {
				alert(cmtLimitBytes+"바이트 이상은 입력할 수 없습니다."); 
				objTextArea.value = objTextArea.value.cut(cmtLimitBytes);
				//CommentCheck(objTextArea, objBytes);
			}

	}
	catch (e)
	{
		alert(e.message);
	}
}


function formsubmit(form,pid) {

	if( $('PostText'+pid).value.replace(/\n\r/g,'').replace(/\r\n/g,'')=='') {
		alert("내용이 입력되지 않았습니다.");
		$('PostText'+pid).focus();
		return;
	}
	else if( $('PostText'+pid).value.bytes() > LimitBytes ) {
		alert((LimitBytes/2)+"자를 초과하였습니다.");
		$('PostText'+pid).focus();
		return;
	}

	form.submit();

	return false;
}


function qnasubmit(form) {

	if( $('PostSubject').value.replace(/\s/g,'')=='') {
		alert("제목이 입력되지 않았습니다.");
		$('PostSubject').focus();
		return;
	}
	else if( $('PostText').value.replace(/\n\r/g,'').replace(/\r\n/g,'')=='') {
		alert("내용이 입력되지 않았습니다.");
		$('PostText').focus();
		return;
	}


	form.submit();

	return false;
}



function cmtsubmit(pid, authkey, parent_id, t) {
	viewpid = pid;
	pid = pid.replace('desc','');

	var json = false;
	try {
		json = eval( "(" + t.responseText + ")" );
		if( json.result==true ) {
			if(parent_id>0) {
				$('PostscommentSonText'+parent_id).value = '';
			}
			else {
				$('PostscommentText'+pid).value = '';
			}

			if($('today_comment_page')!=null) {
				$('today_comment_page').value = '1';
			}

			cmtview(viewpid, authkey, parent_id, json.cmt_id);

			if($('cntrep_'+pid) !=null ) {
				var repcnt = eval($('cntrep_'+pid).innerHTML) + 1;
				if(repcnt<0) { repcnt = 0; };
				$('cntrep_'+pid).innerHTML = repcnt;
				$('byte_cmt_'+pid).innerHTML = 0;
			}

		}
		else
		{
			alert( json.message );
		}	

	}
	catch(e) {
		alert(e.message);
		return;
	}
	return;

}


function cmdSecondWrite(cid, pid, parent_id, authkey) {

		new Ajax.Request( '/postscomments/viewreplyform/' + cid + '/' +  pid + '/' +  parent_id + '/' +  authkey, { 
				asynchronous:true, 
				evalScripts:true, 
				onComplete:function(t) {
					try {
						$('PostSonreplyForm_'+parent_id).innerHTML = t.responseText;
					}
					catch(e) {
						alert(e);
						return;
					}
					
				}
			} );
}







function submit_search(form)
{
	if( getByteLength($('PostSearchString').value.replace(/\s/g,'')) < 2 )
	{
		$('PostSearchString').value = $('PostSearchString').value.replace(/\s/g,'');
		alert( '최소 한글 2자(4bytes) 이상의 검색어를 입력해주세요.' );
		$('PostSearchString').focus();
		return;
	}

	form.submit();
}


function addBookmark(userid) {
	if(userid!=null) {
		new Ajax.Request( '/users/addbookmark/' + userid, { 
				asynchronous:true, 
				evalScripts:true, 
				onComplete:function(t) {
					var json = false;

					try {
						json = eval( "(" + t.responseText + ")" );

						if(json.result==true) {
							
						}

						alert(json.message);
					}
					catch(e) {
						return;
					}
					
				}
			} );

	}
}


function post_delete( id )
{
	if( confirm( "삭제하시겠습니까?" ) )
	{
		f = $('deletepostForm');
		f.action = f.action + id;
		f.submit();
	}
}

function post_recommend( pid )
{
	if( confirm( "이 글에 대해 공감하시겠습니까?" ) )
	{
		new Ajax.Request( '/posts/recommend/' + pid , { 
				asynchronous:true, 
				evalScripts:true, 
				onComplete:function(t) {
					var json = false;

					try {
						json = eval( "(" + t.responseText + ")" );
						if( json.result==true ) {
							var reccnt = eval($('cntrec_'+pid).innerHTML) + 1;
							if(reccnt<0) { reccnt = 0; };
							$('cntrec_'+pid).innerHTML = reccnt;
							alert( json.message );
						}
						else
						{
							alert( json.message );
						}	
					}
					catch(e) {
						alert(e);
					}
				
				}
			} );

	}
	return;
}



function post_scrap( pid, cid ) {
	var scrapcnt = eval($('cntscrap_'+pid).innerHTML) + 1;
	if(scrapcnt<0) { scrapcnt = 0; };
	$('cntscrap_'+pid).innerHTML = scrapcnt;
}


function comment_delete( id, pid, authkey )
{
	if( confirm( "삭제하시겠습니까?" ) )
	{

		if($('PostsComments_'+id) !=null) {
			Effect.Fade($('PostsComments_'+id), { duration: 0.5 });
			if($('vline_'+id) !=null) {
				$('vline_'+id).style.display='none';
			}
		}


		new Ajax.Request( '/postscomments/delete/' + id , { 
				asynchronous:true, 
				evalScripts:true, 
				onComplete:function(t) {
					var json = false;

					try {
						json = eval( "(" + t.responseText + ")" );
						if( json.result==true ) {
							if($('cntrep_'+pid)!=null) {
								cmtview(pid, authkey, 0, 0);
								var repcnt = eval($('cntrep_'+pid).innerHTML) - json.subcnt;
								if(repcnt<0) { repcnt = 0; };
								$('cntrep_'+pid).innerHTML = repcnt;
							}

							if(parent.document.getElementById('IFRAME_TODAY_COMMENT') != null) {
								cmtview('desc'+pid, authkey, 0, id);
							}

						}
						else
						{
							alert( json.message );
						}	
					}
					catch(e) {
						alert(e);
					}
				
				}
			} );


	}
}

function toggleBlind(obj) {
	if($(obj).style.display=='') {
		Effect.BlindUp(obj, { duration: 0.3 });
		$(obj).style.display='none';
		$(obj+'_tray').className = "box_open";
		
	} 
	else {
		Effect.BlindDown(obj, { duration: 0.3 });
		$(obj).style.display='';
		$(obj+'_tray').className = "box_close";
	}
}


// 메세지 현재 커서 위치 저장
function saveCurrentPos (objTextArea) {

   if (objTextArea.createTextRange) 
         objTextArea.currentPos = document.selection.createRange().duplicate();
}

function dragtext(objTextArea)
{                                          
 var d = document.selection.createRange();//objTextArea.createTextRange();
 alert(d);

}

function viewAddLinkPost(viewId, objTextArea, objLinkTitle)
{
	displayLayer(viewId); //view를 보여줌

	if (document.getSelection)    ts = document.getSelection();
	else if (document.selection)  ts = document.selection.createRange().text;
	else if (window.getSelection) ts = window.getSelection();

	if (objTextArea.selectionStart == objTextArea.selectionEnd){
		if(/*@cc_on!@*/false){//IE일때
			objTextArea.focus();
			if(objTextArea.createTextRange){
				if(objTextArea.currentPos){
					objLinkTitle.value = objTextArea.currentPos.text;
				}
			}
		}
	}else{
		//var s1 = objTextArea.value.substring(0, objTextArea.selectionStart);
		var s2 = objTextArea.value.substring(objTextArea.selectionStart, objTextArea.selectionEnd);
		//var s3 = objTextArea.value.substring(objTextArea.selectionEnd);
		//objTextArea.value = s1 + prefix + s2 + postfix + s3;
		objLinkTitle.value = s2;
	}

}
function addLinkPost(objTextArea, prefix, postfix, objLink, objLinkTitle)
{
	if(objLink.value=="http://" || objLink.value.length<= 7){
		alert('링크 주소를 입력해 주세요');
		objTextArea.focus();
		return;
	}

	if (document.getSelection)    ts = document.getSelection();
	else if (document.selection)  ts = document.selection.createRange().text;
	else if (window.getSelection) ts = window.getSelection();

	if (objTextArea.selectionStart == objTextArea.selectionEnd){
		if(/*@cc_on!@*/false){//IE일때
			objTextArea.focus();
			if(objTextArea.createTextRange){
				if(objTextArea.currentPos){
					objTextArea.currentPos.text = prefix + objLinkTitle.value + postfix;
					objTextArea.currentPos.select();
				}else{
					objTextArea.value = objTextArea.value + prefix + objLinkTitle.value + postfix;
				}
			}
		}else{
			var s1 = objTextArea.value.substring(0, objTextArea.selectionStart);
			var s2 = objTextArea.value.substring(objTextArea.selectionStart);
			objTextArea.value = s1 + prefix + objLinkTitle.value + postfix + s2;
		}
	}else{
		var s1 = objTextArea.value.substring(0, objTextArea.selectionStart);
		var s2 = objTextArea.value.substring(objTextArea.selectionStart, objTextArea.selectionEnd);
		var s3 = objTextArea.value.substring(objTextArea.selectionEnd);
		objTextArea.value = s1 + prefix + objLinkTitle.value + postfix + s3;
	}
	objLink.value="http://";
	objLinkTitle.value="";
	objTextArea.focus;
}


function toggle_profile(obj,bool) {
	var A=obj.getElementsByClassName('action_link');

	maxLoop = A.length

	try {
		if(A.length>0) {
			for(i=0;i<maxLoop;i++) {
				if(A[i].style.display=="none") A[i].show(); else A[i].hide();
			}
		}
	}
	catch(e) {
		//alert(e.message);
		return;
	}
}



function flvplayer(fid) {
	hs.htmlExpand(document.getElementById('HSframe_flv'+fid), { src:'/posts/videoget/'+fid+'/play/'+$('HSframe_flvjpg'+fid).value, outlineType:'minicle', layerResize:true, objectType: 'iframe', width: 217, height: 240,headingText: '동영상 재생'  }, '', '동영상 재생' );
}



function copypostlink(meintext)
{
	/*
　 if (window.clipboardData) {
		alert("선택하신 글로 바로갈 수 있는 링크를 복사하시겠습니까?");
　 　 window.clipboardData.setData("Text", meintext);
　 } else if (window.netscape) {
		alert("파이어폭스는 클립보드로 펌링크를 복사할 수 없습니다. \n\n아래의 펌링크 주소를 직접 복사해주세요.");
		$(meintext).style.display = '';
		$(meintext).innerHTML = meintext;
　 }
	*/
	displayLayer(meintext);
	$(meintext).innerHTML = "<input type='text' value='" + meintext+ "' size='70' onfocus='this.select();' style='font-size:12px;font-family:arial;border:1px solid #CCCCCC;'>";
	
}




/* 이미지 회전 */
function imgRotate( model, file_id, angle, post_id, oic_path, url_service ) {
	now = new Date()
   h=now.getHours() //시 구하기
   n=now.getMinutes()//분 구하기
   s=now.getSeconds()//초 구하기

   $('PostsFileModify'+file_id).innerHTML = "<img src='/img/loading.gif' width='32' height='32' style='margin:30px;'>";

	new Ajax.Request( '/posts/ImgRotate/' + model + '/' + file_id + '/' +  angle, { 
			asynchronous:true, 
			evalScripts:true, 
			onComplete:function(t) {
				var json = false;
				try {
					json = eval( "(" + t.responseText + ")" );
					if( json.result==true ) {
						$('PostsFileModify'+file_id).innerHTML = "<img src='" + oic_path + "url=" +$('PostsThumbImg'+file_id).value + "&cx=93&cy=93&crop=1&id=postThumb"+file_id+"&cache=0' width='93' height='93' /><img src='" + oic_path + "url=" + url_service + $('PostsFileImg'+file_id).value + "&id=postLarge"+file_id+"&cache=0' style='display:none;'/>";
												
						$('spanPostsFile'+file_id).innerHTML = "<img src='" + oic_path + "url=" + $('PostsThumbImg'+file_id).value + "&cx=93&cy=93&crop=1&id=postThumb"+file_id+"&cache=0' width='93' height='93' style='float:left;margin-right:4px;cursor:pointer;' onclick=\"alert('이미지를 한번 더 클릭해주세요.');location.reload();\" />";
						$('PostsFileRotate'+file_id).innerHTML = "";
						alert('이미지가 '+angle+'도 회전되어 저장되었습니다.');
					}
					else
					{
						alert( json.message );
					}	

				}
				catch(e) {
					alert(e.message);
					return;
				}
			
			}
		} );

}



function flash_contents(file,width,height){

	var ObjNo = "";
	if(document.getElementById("FlashEolasSwfBasic1")==null) {
		ObjNo = 1;
	}
	else {
		for(var i=1;i<15;i++) {
			if(document.getElementById("FlashEolasSwfBasic"+i)==null) {
				ObjNo = i;
				break;
			}
		}
	}

document.writeln("<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' WIDTH='"+width+"' HEIGHT='"+height+"' id='FlashEolasSwfBasic"+ObjNo+"' ALIGN=''>");
document.writeln("<PARAM NAME=movie value='"+file+"' />");
document.writeln("<PARAM NAME=quality VALUE=high>");
document.writeln("<PARAM NAME=bgcolor VALUE=#FFFFFF>");
document.writeln("<PARAM NAME=wmode VALUE=transparent> ");
document.writeln("<embed src='"+file+"' quality='high' bgcolor='#FFFFFF' width='"+width+"' height='"+height+"' name='FlashEolasSwfBasic"+ObjNo+"' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
document.writeln("</OBJECT>");
}