/*
 * author:addo zhang
 */
(function(){
	function $(id){
		return document.getElementById(id);
	}
	function setStyleDisplay(id,status){
		$(id).style.display=status;
	}
	function reply(comment_id,comment_author,textarea_id,reply_to,cancel,reply_comment){
		var str = '<a href="#comment-'+comment_id+'">@';
		str += comment_author;
		str += ': </a>';
		var replyto =  ' to <a class="reply" href="#comment-'+comment_id+'">' + comment_author +'</a>';
		/*show the target user which current user wants reply to*/
		$(reply_to).innerHTML =	replyto;
		/*support a button for user to cancel replying*/
		$(cancel).innerHTML = '<input type="button" value="Cancel" onclick="Util.cancel(\''+reply_to+'\',\''+cancel+'\')"/>';
		/*save the href of reply*/
		$(reply_comment).value = str;
		/*the comment textarea gets focus*/
		$(textarea_id).focus();
	}
	function cancel(reply_to,cancel){
		/*clear the prompt for replying target user*/
		$(reply_to).innerHTML = '';
		/*delete the cancel button*/
		$(cancel).innerHTML = '';
	}
	function appendComment(reply_to,comment,reply_comment,cancel){
		var replyTo = $(reply_to).innerHTML;
		if(replyTo.length!=0){
			/*reply someone comment*/
			$(reply_comment).value += '\n'+$(comment).value;
		}else {
			/*normal comment*/
			$(reply_comment).value = $(comment).value;
		}
		/*clear the prompt for replying target user*/
		$(reply_to).innerHTML = '';
		/*delete the cancel button*/
		$(cancel).innerHTML = '';
	}
	
	window['Util'] = {};
	window['Util']['$']=$;
	window['Util']['reply']=reply;
	window['Util']['cancel']=cancel;
	window['Util']['appendComment']=appendComment;
	window['Util']['setStyleDisplay']=setStyleDisplay;
})();
