// 댓글쓰기
var comment_page = 1;
var comment_id = 0;
var isSending = false;
var delform_idx;
function reply_submit(idx){
	var myform = document.reply_form ;
	var photo_idx = myform.photo_idx.value ;
	var re_idx = myform.re_idx.value ;
	var contents = myform.contents.value ;
	if (myform.noreply.checked){
		var noreply = 0;
	} else {
		var noreply = 1;
	}

	if (document.getElementById("comment_username") != null) {
		if (document.getElementById("comment_username").value == '') {
			document.getElementById("span_username").innerHTML = 'Please specify Username';
			return false;
		} else {
			var user_name	= document.getElementById("comment_username").value;
		}

		if (document.getElementById("comment_password").value == '') {
			document.getElementById("span_username").innerHTML = 'Please specify Password';
			return false;
		} else {
			var user_pass	= hex_md5(document.getElementById("comment_password").value);
		}
		var url = "/video/reply_ok.html?re_idx="+re_idx+"&noreply="+noreply+"&video_idx="+photo_idx+"&username="+encodeURIComponent(user_name)+"&password="+user_pass+"&contents="+encodeURIComponent(contents);
	} else {
		var url = "/video/reply_ok.html?re_idx="+re_idx+"&noreply="+noreply+"&video_idx="+photo_idx+"&contents="+encodeURIComponent(contents);
	}

	
	
	if (contents.length <= 0 ){
		document.getElementById("span_username").innerHTML = 'Please specify contents' ;
		return false;
	}
	if (isSending == false)
	{
		ajaxRequest(url, null, result_submit);
		isSending = true;
	}
}


// 글쓰기 하였을때 댓글을 불러들이고 댓글 name 으로 이동한다.
function result_submit(str) {
	if (str == "-1")
	{
		alert("The comment you have entered is too long. Please write a shorter comment and try again.");
	}else{
		comments_view();
	}
	isSending = false;
}

// 댓글내용 ajax 
function comments_view(res) {
	var myform = document.reply_form ;
	comment_id = myform.photo_idx.value;
	ajaxRequest("/video/requestComments.html?page="+comment_page+"&idx="+comment_id,null,callback_comments);
}


// ajax 받아온값을 Div 에 찍는다.
function callback_comments(str){
	document.getElementById("vc").innerHTML = str ;
	document.location.href = '#comment_list';
}

// 댓글에 대한 댓글 쓰기 세팅
function replyWrite(user_name,re_idx){
	document.getElementById("span_userName").innerHTML = "Replying to comment by <span class='user'>"+user_name+"</span> (<a href='javascript:void(0);' onclick='replyCancle();'>cancel</a>)" ;
	document.reply_form.re_idx.value= re_idx;
	document.reply_form.contents.focus();
	
}

//댓글에 대한 댓글을 원래 상태로 돌린다.
function replyCancle(){
	document.getElementById("span_userName").innerHTML = "";
	document.reply_form.re_idx.value= "";
	document.reply_form.contents.focus();
}

//댓글 페이지 처리한다.
function goToPage(page){
	comment_page = page;
	comments_view();
}

// 댓글에 대한 추천 비추천 처리한다.
function voteIt(memo,num){
		ajaxRequest("/video/voteIt.html?memo="+memo+"&num="+num,null,callback_vote);
}

function callback_vote(str){
	var str_arr = str.split(",");

	if (str_arr.length == 3){
		document.getElementById("vote_up_"+str_arr[2]).innerHTML = (str_arr[0]=="0" ? "":"+" )+ str_arr[0] ;
		document.getElementById("vote_down_"+str_arr[2]).innerHTML = (str_arr[1]=="0" ? "":"-") + str_arr[1] ;
		document.getElementById("hand_up_"+str_arr[2]).innerHTML = "<img src='/image/icon/icon_thumbs_up2.gif' alt='Vote!'>" ;
		document.getElementById("hand_down_"+str_arr[2]).innerHTML = "<img src='/image/icon/icon_thumbs_down2.gif' alt='ooo~'>" ;
	}
}

// reply delete
function reply_delete(num, evt, v){
	if (!confirm("Delete this comment?")) return false;
	var delform = document.getElementById("delform");
	delform_idx = num;
	if (v == 0)
	{
		confirmSubmit();
		return false;
	}
	if (document.documentElement.scrollTop != 0) {
		var scrollHeight = document.documentElement.scrollTop;
	} else if (document.body.scrollTop != 0) {
		var scrollHeight = document.body.scrollTop;
	}

	var offArray = Event.element(evt).cumulativeOffset();
	var offX = offArray[0];
	var offY = offArray[1];


	delform.className = "show";
	delform.style.left = (offX-110)+"px";
	delform.style.top = (offY+17)+"px";

	document.getElementById("tbox").focus();
}

function delKey(e){
	var keyName = e.keyCode? e.keyCode : e.charCode;
	keyName  = parseInt(keyName);
	if (keyName == 13){
		confirmSubmit();
	}
}

function confirmSubmit() {
	var idx = delform_idx;
	var delform = document.getElementById("delform");
	var tbox_value = document.getElementById("tbox").value;
	if ((delform.className == "show" && tbox_value != "") || delform.className == "hidden")
	{
		var target_pass = hex_md5(tbox_value);
		var url = "/video/reply_delete.html?id=" + idx + "&password=" + target_pass;
		ajaxRequest(url, null, comments_view);
		confirmCancel();
	}
}

function confirmCancel() {
	var delform = document.getElementById("delform");
	delform.className = "hidden";
}


// 가려진 comment 보이기
function showCom(idx){
	var obj = document.getElementById('showCom_' + idx) ;
	obj.style.display = '' ;
	var obj = document.getElementById('memoShow_' + idx) ;
	obj.innerHTML = "<a href='javascript:hiddenCom(" + idx + ")'>[below viewing threshold, hide comment] </a>" ;
}
// comment 가리기
function hiddenCom(idx){
	var obj = document.getElementById('showCom_' + idx) ;
	obj.style.display = 'none' ;
	var obj = document.getElementById('memoShow_' + idx) ;
	obj.innerHTML = "<a href='javascript:showCom(" + idx + ");'>[below viewing threshold, show comment]</a>" ;
}