/*
 * 回复相关 js函数 文件
 * 2009-09-07 22:12:32
 * author:wangxb
 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  */

function onReply(logId, formId){
	var _objReply = new replyInput(logId, formId);
	if(_objReply.state == "activated")	
		return false;
	if(_objReply.state == 'sleep'){
		_objReply.activate();
	}
}

function delReply(logId, formId, lreplyId, thisId){
	if(confirm("您好，确认删除信息吗？")){
		var _objReply = new replyInput(logId, formId);
		objBt = $(thisId);
		_objReply.del(lreplyId, objBt);
	}
}

function showMore(logId, formId){
	var _objReply = new replyInput(logId, formId);
	_objReply.showMore();
}

function toggleReply(thisId){
	var _thisObj = $(thisId);

	if(_thisObj.up(2).next(0).getStyle('display')=='none'){
		_thisObj.up(2).next(0).show();
		var _str = _thisObj.innerHTML.replace("回复", "收起回复");
		_thisObj.update(_str);
	}else{
		_thisObj.up(2).next(0).hide();
		var _str = _thisObj.innerHTML.replace("收起回复", "回复");
		_thisObj.update(_str);
	}
}

function replyUser(logId, formId, logreplyid, replyName, thisId){
	var _objReply = new replyInput(logId, formId);
	_objReply.setReReply(logreplyid, replyName);
}

/*
 * {{{class: 回复表单管理对象}}}
 *
 * @param string inputId 输入框id
 *
 */
var replyInput = Class.create( {
	initialize : function(logId, formId) {
		this.urlTpl = $H({
				showBlock: new Template('handle/sendReplyAjax.php?logId=#{logId}&logreplyid=#{logreplyid}'),
				showMore: new Template('handle/showMoreReplyAjax.php?logId=#{logId}'),
				del: new Template("handle/delReplyAjax.php?lreplyId=#{lreplyId}")
			});

		this.postCount = 0;

		var _msgBlockTpl = "";
		_msgBlockTpl +=	'<span class="share-n-hide"><a class="x-to-hide" id="a-delreply-#{lreplyId}" onclick="delReply(\'#{logId}\', \'#{formId}\', \'#{lreplyId}\', \'a-delreply-#{lreplyId}\');" href="#nogo"></a></span>';
		_msgBlockTpl +=	'<a class="minfriendpic" href="../uCenter/index.php?userId=#{uid}"><span><img src="../upload/images/user/head/img33/#{headimg}" /></span></a>';
		_msgBlockTpl +=	'<p class="replybody">';
		_msgBlockTpl +=	'<a class="replyername" href="../uCenter/index.php?userId=#{uid}">#{nickname}</a>';
		_msgBlockTpl +=	'<span class="time">  #{time}</span>';
		_msgBlockTpl +=	'<br/>';
		_msgBlockTpl +=	'<span class="replycontent">#{content}</span>';
		_msgBlockTpl += '</p>';

		this.msgBlockTpl = new Template(_msgBlockTpl);

		this.logId = logId;
		
		this.thisObj = $("input-content-"+this.logId);

		if(this.thisObj.className=='input-text'){
			this.state = 'sleep';
		}else{
			this.state = 'activated';
		}

		this.ctNum = this.thisObj.value.length;
		//{{{dom对象
		this.optionDiv = this.thisObj.next(0);
		this.contentNum = this.optionDiv.down(0);
		this.submitBt = this.optionDiv.down(0).next(0);
		this.form = $(formId);
		this.logreplyid = 0;
		
		this.moreDiv = this.form.down(0).next(0);
		//}}}
		
	},

	activate : function() {
		Event.stopObserving(this.submitBt, "click");
		this.state = "activated";
		if(this.thisObj.className=='input-text'){
			this.thisObj.observe("blur",this.sleep.bind(this));
			this.thisObj.className = '';
			this.thisObj.update('');
		}
		
		this.thisObj.setStyle({height:'32px'});
		this.optionDiv.show();
		this.thisObj.observe("keyup", this.changeContentLength.bind(this));
	
		this.submitBt.observe("click", this.reply.bind(this));
	},

	sleep : function() {
		setTimeout(function(){
			Event.stopObserving(this.submitBt, "click");
			Event.stopObserving(this.thisObj, "blur");
			Event.stopObserving(this.thisObj, "keyup");
			this.thisObj.className = 'input-text';
			this.thisObj.setStyle({height:'16px'});
			this.thisObj.update('我要回复');
			this.optionDiv.hide();
		}.bind(this),500);
	},

	reply : function() {
		if(this.replyCheck()){
			this.postCount ++;
			var _url = this.urlTpl.get("showBlock").evaluate({logId: this.logId, logreplyid: this.logreplyid});
			new Ajax.Request(_url, {
						method: "post",
						parameters: Form.serialize(this.form),
						onComplete: function(transport){
							if(this.postCount == 1){
								var response = transport.responseText;
								this.showNew(response.evalJSON());
							}
						}.bind(this)
					});
		}
	},

	changeContentLength : function(e){
		this.ctNum = this.thisObj.value.length;

		if(this.ctNum>0){
			Event.stopObserving(this.thisObj, "blur");
		}else{
			this.thisObj.observe("blur",this.sleep.bind(this));
		}
		if(this.ctNum>139){
			this.ctNum = 140;
			this.thisObj.value = this.thisObj.value.substring(0,139);
		}
		this.contentNum.innerHTML = this.ctNum + "\/140";
	},

	replyCheck : function(){
		_oldContent = this.thisObj.value;
		var patrn=/^\u56de\u590d.+：(.*)/;
		_oldContent = _oldContent.replace(patrn,"$1");
		if(_oldContent.blank()){
			return false;
		}
		return true;
	},

	showNew : function(json){
		var _needJson = {
			logId: json.logid,
			formId: "form-reply-" + json.logid,
			lreplyId: json.id,
			uid: json.uid,
			headimg: json.headimg,
			nickname: json.nickname,
			time: json.time,
			content: json.content
		};

		var _divBlock = new Element("div", {className: 'statuscmtitem'}).update(this.msgBlockTpl.evaluate(_needJson));

		this.thisObj.up(0).insert({"before":_divBlock});
		this.thisObj.value="";
		this.ctNum = 0;
		this.contentNum.innerHTML = "0\/140";
		this.postCount--;
		if($('replyNum-'+json.logid)!=null){
			var _strNum = $('replyNum-'+json.logid).innerHTML;
			_strNum = _strNum.replace("(", '');
			_strNum = _strNum.replace(")", '');
			_strNum ++;
			$('replyNum-'+json.logid).update("("+_strNum+")")
		}
	},

	del : function(lreplyId, objBt){
		objBt.up(1).remove();
		var _url = this.urlTpl.get("del").evaluate({lreplyId: lreplyId});
		new Ajax.Request(_url, {
					method: "get",
					onComplete: function(transport){
						var response = trim(transport.responseText);
						if(response=='fail'){
							window.location.href='../home/login.php';
						}
					}.bind(this)
				});
		if($('replyNum-'+this.logId)!=null){
			var _strNum = $('replyNum-'+this.logId).innerHTML;
			_strNum = _strNum.replace("(", '');
			_strNum = _strNum.replace(")", '');
			_strNum --;
			$('replyNum-'+this.logId).update("("+_strNum+")")
		}
	},

	showMore : function(){
		var _url = this.urlTpl.get("showMore").evaluate({logId: this.logId});
		new Ajax.Request(_url, {
					method: "get",
					onComplete: function(transport){
						var response = transport.responseText;
						this.moreDiv.innerHTML = response;
					}.bind(this)
				});
	},
	
	setReReply : function(logreplyid, replyName){
		
		this.logreplyid = logreplyid;
		var _oldContent = this.thisObj.value;
		var patrn=/^\u56de\u590d.+：(.*)/;
		_oldContent = _oldContent.replace(patrn,"$1");

		if(this.state != "activated"){
			this.activate();
			this.thisObj.select();
			this.thisObj.value = '回复'+replyName+'：';
		}else{
			this.thisObj.value = '回复'+replyName+'：'+_oldContent;
		}
		
		//window.scrollBy(0,this.thisObj.scrollTop);
		
		this.changeContentLength();
	 }
});
