$(function(){//啟用 jQ

var i = 0, got = -1, len = document.getElementsByTagName('script').length;//讀取網頁找 script 數量
while ( i <= len && got == -1){
	var js_url = document.getElementsByTagName('script')[i].src,//判斷哪一個 script 是 comments-ajax.js
			got = js_url.indexOf('comments-ajax.js'); i++ ;	//找到 comments-ajax.js 文件路徑
}
var	ajax_php_url = js_url.replace('-ajax.js','-ajax.php'),//將 -ajax.js 替換為 -ajax.php, 找到 comments-ajax.php 路徑
		wp_url = js_url.substr(0,js_url.indexOf('/wp-content/')),//找到 WP 安裝路徑
		txt1 = ' style="display: none;background: url(',//--------------- 以下是過程所用的 html 字段, 儘量不去動它.
		txt2 = ') no-repeat left;',
		txt3 = '<div id="commentload"'+ txt1 +  txt2 + '">嘟嘟嘟嘟 connecting...</div>',
		txt4 = '<div id="commenterror"'+ txt1 +  txt2 + 'margin: 0 auto;">#</div>',
		txt5 = '\n<ol class="commentlist" id="new_comm_',
		txt6 = '\n<ul class="children" id="new_comm_',
		txt7 = '" style="display: none;">',
		txt8 = '\n<span id="success_',
		txt9 = '" class="success_" style="background: url(' + txt2 + '">sent successfully<a rel="nofollow" href="',
		txta = '#edit" onclick=\'return editComment.moveForm("',
		txtb, num = 0, edit_id, edit, $tmp_comm, $tmp_sucs, $new_comm, $new_sucs,
		cancel_text = $('#cancel-comment-reply-link').html(),
		comm_array = new Array(); comm_array.push(''), num ++;
				$('#submit').attr("disabled",false); //確定提交按鈕功能沒取消
				$('#respond').before( txt3 + txt4 );	//添加提交和錯誤提示, 在#comment 或 #submit 後添加, 視模板設計而定
				
$('#commentform').submit(function(){//id='commentform' submit時的動作
				$('#submit').attr("disabled",true).fadeTo('slow', 0.2);//防範再次按提交按鈕
	if ( edit ) {//如果是再編輯的
				tmp_txt = '<input type="text" name="edit_id" id="edit_id" value="' + edit_id + '" style="display: none;" />';//增加一欄 edit_id
				$('#comment').after(tmp_txt);//#comment 後面添加被取代的 comment_ID, 通知 php 來刪除
	}

	$.ajax({//啟用 Ajax
				url: ajax_php_url,//comments-ajax.php 位址
				data: $('#commentform').serialize(),//發送的數據 id='commentform'
				type: 'POST',//請求類型為 POST

		beforeSend: function(){//提交時的動作
				$('#commenterror').hide();//隱藏:錯誤提示
				$('#commentload').slideDown();//拉下顯示:正在提交
				},

		error: function(request){//錯誤時的動作
				$('#commentload').slideUp();//推上隱藏:正在提交
				$('#commenterror').show('slow').html(request.responseText);//顯示:錯誤提示
				setTimeout(function(){$('#submit').attr('disabled',false).fadeTo('slow', 1);$('#commenterror').slideUp();}, 3000);//恢復: 提交按鈕
				},

		success: function(data){//成功時的動作
				$('textarea').each(function(){this.value=''});//清空: textarea 《使用 $('#comment').val(''); 也可以, 但有些模板不動作》
				$('#commentload').hide();//隱藏:正在提交
		var t = addComment, cancel = t.I('cancel-comment-reply-link'),//評論框 & 取消回覆鏈接定義
				temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId),//評論框的臨時節點定義
				post = t.I('comment_post_ID').value, parent = t.I('comment_parent').value,//傳回父層值
		 		num_text = num.toString();//數字轉文字, 給編號

	if ( ! edit ){//如果不是再編輯的	--------------- 評論數變化
	if ($('#comments').length){//如果已有 id='comments'
		tmp_txt = t.I('comments').innerHTML,//取 id='comments' 內容
		n = parseInt(tmp_txt.match(/\d+/)),//在字串中找數字
		tmp_txt = tmp_txt.replace( n, n + 1 );//替換評論數字串
		$('#comments').text(tmp_txt)}//顯示:新評論數
	else {tmp_txt = '';//沒有時, 產生新 id='comments' 回复后不显现在有几条。1017,09
		$('#respond').before(tmp_txt);//將新 id 加入
	}}

	if ( parent == '0'){new_htm = txt5 + num_text + txt7 + '</ol>' }//如果是底層, 加:ol	-------------- 顯示新評論
	else {new_htm = txt6 + num_text + txt7 + '</ul>';//子層加:ul
		is_div = document.getElementsByTagName('ol')[0].innerHTML.indexOf('div-');//找尋 div- 字頭的 id
		if ( is_div == -1 ){txtb = ''} else {txtb = 'div-'};//如果找到, comment 的 id 也要加 div- 字頭, 因 WP 默認的的有字頭, 但一般模板設計沒字頭.
			 }
				new_htm = new_htm + txt8 + num_text + txt9;//加:提交成功和再編輯

new_htm = new_htm.concat(txta,txtb,'comment-',parent,'", "',parent,'", "respond", "',post,'",'+ num_text,')\'></a></span>\n'); //加:編輯鏈接
				$('#respond').before(new_htm);//在 #respond 前加入 new_htm
				$new_sucs = $('#success_' + num_text ); $new_sucs.hide();//不显示提交成功和编辑。1017,09
		var $new_comm = $('#new_comm_' + num_text);//定義新評論的 div
				$new_comm.append(data).fadeIn(3000);//將新評論內容傳入$new_comm, 以淡入效果顯示新評論, (3000)表示3秒
				countdown();//(倒計時函式在最下面)
				num++ ;//編號累進, 目的是不讓 id 重覆

		edit_content = $('#new_comm_' + num_text+' p').html();//新評論 id	-------------- 存取內容, 以便再編輯
		comm_array.push(edit_content);//評論內容存入數據矩陣
		edit='';//清除編輯標記
		$('*').remove('#edit_id');//刪除: #edit

		cancel.style.display = 'none';//隱藏:取消回覆	-------------- 評論框回底層
		cancel.onclick = null;//清空:回覆鏈接
		$('#cancel-comment-reply-link').text(cancel_text);//取消編輯改為取消回覆
		t.I('comment_parent').value = '0';//回底層
if ( temp && respond ){//如果有節點和回覆框
		temp.parentNode.insertBefore(respond, temp);//temp 節點前加評論框
		temp.parentNode.removeChild(temp)}//刪除 temp 節點	------------------- end --
				}
			});//結束Ajax
  return false;//終止submit動作
	});

addComment = {//回覆時的動作, 以下參考 wp-includes\js\comment-reply.dev.js
	moveForm : function(commId, parentId, respondId, postId) {
		var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');

		if ( edit ){//如果正在編輯別的評論, 又按這個回覆
			$tmp_comm.show();//顯示:別的評論
			$tmp_sucs.show();//顯示:別的提交成功
			$('textarea').each(function(){this.value=''});//清空: textarea
			edit=''}//清除編輯標記

		$('#commenterror').hide();//隱藏:錯誤提示
		$('#cancel-comment-reply-link').text(cancel_text);//取消編輯改為取消回覆

		t.respondId = respondId;
		postId = postId || false;

		if ( ! t.I('wp-temp-form-div') ) {
			div = document.createElement('div');
			div.id = 'wp-temp-form-div';
			div.style.display = 'none';
			respond.parentNode.insertBefore(div, respond)
		}

		if ( post && postId && comm )
			comm.parentNode.insertBefore(respond, comm.nextSibling);
			post.value = postId;
			parent.value = parentId;
			cancel.style.display = '';

		cancel.onclick = function() {//取消回覆時的動作
			var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);

			$('#commenterror').hide();//隱藏:錯誤提示

			this.style.display = 'none';
			this.onclick = null;
			t.I('comment_parent').value = '0';
		if ( temp && respond ){
			temp.parentNode.insertBefore(respond, temp);
			temp.parentNode.removeChild(temp)}
			return false;
		};
		try { t.I('comment').focus(); }
		catch(e) {}
		return false;
	},
	I : function(e) {
		return document.getElementById(e)
	}
};		//結束addComment

editComment = {//編輯時的動作
	moveForm : function(commId, parentId, respondId, postId, num_text) {
		var t = addComment, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');

		if ( edit ){//如果正在編輯別的評論, 又按這個編輯
			$tmp_comm.show();//顯示:別的評論
			$tmp_sucs.show();//顯示:別的提交成功
			$('textarea').each(function(){this.value=''});//清空: textarea
			edit=''}//清除編輯標記

		t.I('comment').value = comm_array[num_text];//從數據矩陣提取評論內容

		new_comm = 'new_comm_'+ num_text;//給新 id
    new_htm = t.I(new_comm).innerHTML;//提取評論的div
    tmp_txt = new_htm.substring(new_htm.indexOf('div id')) ;//在 div id 中找 comment-
		edit_id = tmp_txt.match(/\d+/);//找到被編輯的 id

		t.I('comment_parent').value = '0';//先回底層, 以免位置出錯
		$('#commenterror').hide();//隱藏:錯誤提示

		$new_sucs = $('#success_' + num_text ); $new_sucs.hide();//隱藏:提交成功
		$new_comm = $('#new_comm_' + num_text ); $new_comm.hide();//隱藏:新評論
		$('#cancel-comment-reply-link').text('取消編輯');//取消回覆改為取消編輯
		edit = num_text;//設為正在編輯
		$tmp_comm = $new_comm; $tmp_sucs = $new_sucs;//暫存正在編輯內容

		t.respondId = respondId;
		postId = postId || false;
		if ( ! t.I('wp-temp-form-div') ) {
			div = document.createElement('div');
			div.id = 'wp-temp-form-div';
			div.style.display = 'none';
			respond.parentNode.insertBefore(div, respond)
		}

		if ( ! comm ){
			temp = t.I('wp-temp-form-div'),
			t.I('comment_parent').value = '0';
			temp.parentNode.insertBefore(respond, temp);
			temp.parentNode.removeChild(temp)}

		if ( post && postId && comm )
			comm.parentNode.insertBefore(respond, comm.nextSibling);
			post.value = postId;
			parent.value = parentId;
			cancel.style.display = '';

		cancel.onclick = function() {//取消編輯時的動作
			var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);

		$('#commenterror').hide();//隱藏:錯誤提示
		$tmp_comm.show();//顯示:評論
		$tmp_sucs.show();//顯示:提交成功
		$('textarea').each(function(){this.value=''});//清空: textarea
		$('#cancel-comment-reply-link').text(cancel_text);//取消編輯改為取消回覆
		edit ='';//清除編輯標記

			this.style.display = 'none';
			this.onclick = null;
			t.I('comment_parent').value = '0';
		if ( temp && respond ){
			temp.parentNode.insertBefore(respond, temp);
			temp.parentNode.removeChild(temp)}
			return false;
		};
		try { t.I('comment').focus(); }
		catch(e) {}
		return false;
	}
};		//結束editComment

var wait = 15, submit_val = $('#submit').val();//時間設15秒, 暫存:按鈕上的字
function countdown(){//倒計時函式
	if ( wait == 0 ){//如果時間到
		$('#submit').val(submit_val).attr('disabled',false).fadeTo('slow', 1);//恢復:提交按鈕
		wait = 15;//重置時間
	} else {
		$('#submit').val(wait); wait--; setTimeout(countdown,1000);//顯示:秒數, 秒數遞減, 1秒延遲
  }
}
})
