﻿
/*
● 作者：Sean Lin (http://sean.o4u.com)

● 程式功能：讓 blogger.com 張貼的「意見」可以有「作者回覆」。

● 安裝方法：

	在「<b:includable id='comments' var='post'>」下一行加入:
	
	<b:if cond='data:post.allowComments'>
		<script src='http://www.google.com/jsapi' type='text/javascript'/>
		<script charset='UTF-8' src='http://這個檔案存放的網址/ar.js' type='text/javascript'/>
		<script type='text/javascript'>var PostAuthor=&#39;<data:post.author/>&#39;;</script>
	</b:if>
	
*/

if(document.location.href.indexOf('http://sean.o4u.com/')!=0)
alert('為防止 JavaScript 盜連，請修改上下這兩行。刪掉或修改，並放到自己的網站空間。');


google.setOnLoadCallback(initializeAuthorReply);

function initializeAuthorReply() {
	
	var aTags = document.getElementById("comments").getElementsByTagName("A");
	var addCommentUrl = '';

	//找出「張貼意見」的連結
	if(document.getElementById('comment-editor-src')) {
		addCommentUrl = document.getElementById('comment-editor-src').href.replace(/-iframe/,'');
	}
	else {
		for (i=0; i<aTags.length; i++)
			if( aTags[i].href.indexOf('/comment.g?') > 0)
				addCommentUrl = aTags[i].href;
	}
		
	var reCommentUser = /\s*(.+)\s+提到\.\.\.\s*/;
	var reReply = /^\s?<p>\s?Re:\s*(.+?)\s*&lt;(\d+)&gt;<br\/?>((?:.|\n)+)<\/p>\s?$/i;
	var rePostID = /postID=(\d+)/;
	var commentID = '';
	var commentUser = '';
	var elCommentAuthor, elCommentBody, elCommentFooter, replyURL;
	var CommentFooters = new Object();

	for (i=0; i<aTags.length; i++) {
		if( aTags[i].href.indexOf('/delete-comment.g?') > 0) { //在「刪除意見」連結上找 意見ID
			commentID = rePostID.test(aTags[i].href)?RegExp.$1:'';
			elCommentAuthor = document.getElementById('c'+commentID);
			elCommentBody = nextElement(elCommentAuthor);
			elCommentFooter = nextElement(elCommentBody);
			CommentFooters['c'+commentID] = elCommentFooter;
			
			
			
			commentUser = reCommentUser.test(elCommentAuthor.innerText)?RegExp.$1:''; //抽出留意見人名

			if( commentUser == PostAuthor && reReply.test(elCommentBody.innerHTML)){
					replyID = RegExp.$2;
					replyMsg = RegExp.$3;
					if(CommentFooters['c'+replyID]) {
						CommentFooters['c'+replyID].innerHTML += '<hr noshade size="1" /><div align="left" style="background-color: lightyellow; padding: 4px">●<b style="color: blue">作者回覆</b>：'+replyMsg+'<br/><div align="right" style="font-size:8pt; color:darkgray">'+elCommentFooter.innerText+'</div></div>';
						elCommentAuthor.style.display = 'none';
						elCommentBody.style.display = 'none';
						elCommentFooter.style.display = 'none';
					}
			}
			
			replyURL = '<a href="'+addCommentUrl+'&postBody=Re%3A%20'+encodeURIComponent(commentUser)+'%20%3C'+commentID+'%3E%0D%0A'+'" style="font: 9pt Arial; color:#8a8a8a" title="回覆意見">回</a>';
			afterEndHtml(aTags[i],replyURL);
		}
	}
}

function replyEmbedComment(CN,CID) { 
	var url = 
	url += "&postBody=Re:+"+encodeURIComponent(CN)+"+<"+CID+">";
	location.href = url; 
} 


// IE FF 相容式
function afterEndHtml(el, html){
  if(el.insertAdjacentHTML){
			el.insertAdjacentHTML('AfterEnd', html);
			return el.nextSibling;
	}

	var range = el.ownerDocument.createRange();
	range.setStartAfter(el);

	var frag = range.createContextualFragment(html);
	el.parentNode.insertBefore(frag, el.nextSibling);
	return el.nextSibling;
}

// IE FF 相容
function nextElement(el) {
	return(el.nextElementSibling?el.nextElementSibling:el.nextSibling);
}

// 讓 FF 有 innerText 功能
if(window.navigator.userAgent.toLowerCase().indexOf("msie")<0){ 
	HTMLElement.prototype.__defineGetter__("innerText", 
		function(){
			var anyString = "";
			var childS = this.childNodes;
			for(var i=0; i<childS.length; i++) { 
				if(childS[i].nodeType==1)
					//anyString += childS[i].tagName=="BR" ? "\n" : childS[i].innerText;
					anyString += childS[i].innerText;
				else if(childS[i].nodeType==3)
					anyString += childS[i].nodeValue;
			}
	    return anyString;
		} 
	); 
	HTMLElement.prototype.__defineSetter__("innerText", 
		function(sText){
			this.textContent=sText; 
		} 
	); 
}
