/**= = = = = = = = = = = = = = = = = = = = = -回复控件= = = = = = = = = = = = = = = = = = = = = ------*/
var myInfo=Entry.getUserInfo();
if (!$.HitUp) {		//兼容新 旧库
	var Urls = {
		home: function(uid, flag){
			uid = uid || userId;
			return (flag ? "http://resimage." + XN.doMain : "") + XN.PathParse(uid,"userfile") + "";
		}
	};
	
	$.HitUp = (function(postUrl, method, para, type) {
		var ajax = new FWK.Util.Ajax(postUrl, method, para, "async");

		ajax.$success = function(){this.backData = eval("("+this.responseText+")");  if(!this.backData["code"]||this.backData["code"]=="0"){ this.$onSuccessed();} else {this.$onFailed();}};
		ajax.$fail = function(){this.backData = eval("("+this.responseText+")"); this.$onFailed()};
		ajax.asyncAsk = function(obj){
			this.setParamObj(obj);
			this.open();			
		};
//		ajax.debug = true;
		ajax.extra = function(obj) {
			for (var i in obj) {
				ajax[i] = obj[i];
			}
			
			return this;
		};
		return ajax;
	});
	
};
var replyBox = {
	timeInterval:null,
	setMode : function(mode,resType,resId) {
		this.mode = mode=="subPage" ? "subPage" : null;
		this.resType = resType || "00";
		if (resId&&mode=="subPage") {
			this.updateDomId(resId);
//			var dom = $("rpl_res_");
//			var fireDom = $("rpl_fire_");
//			dom.id = "rpl_res_"+resId;
//			fireDom.id = "rpl_fire_"+resId;
			this.getMore(resId);
			this.showReply(resId);			
		}
//		replyBox.resId=resId;

	},
	setResId : function(resId) {
		if (this.mode) {
			this.updateDomId(resId);
			
			this.getMore(resId);
		}
		this.resId = resId;
	},
	updateDomId : function(resId) {
		oldId = this.resId||"";
		var dom = $("rpl_res_"+oldId);
		var fireDom = $("rpl_fire_"+oldId);
		
		dom.id = "rpl_res_"+resId;
		fireDom.id = "rpl_fire_"+resId;
		
		dom.parentNode.style.paddingBottom = "38px";		//将内页的下边撑开足够空间，供回复框扩展使用
	},
	resType : "00",
	ajax : new $.HitUp("/newuser/freply.do","Post",null,"text/json").extra({
		$onSuccessed : function() {
			id = replyBox.resId;
			var dom = $("rpl_res_"+id);
			replyBox.showCount(this.backData["recordCount"]||0);//评论个数
			if (this.backData["code"]=="0") {
				dom.innerHTML = this.backData["list"];
			
				if (replyBox.h) {
					var h = dom.offsetHeight; //计算刚回复的评论偏移位置
					var diffH = h - replyBox.h;
					var top = document.documentElement.scrollTop||document.body.scrollTop + diffH;
					window.scrollTo(0, top);
				}	
			
				replyBox.rplCancel();
				
			} else {
				this.$onFailed();
			}
		},
		$onFailed : function() {
			FWK.alert(this.backData["msg"]||"未知错误");
		}
	}),
	replyCtrl : FWK.Util.createElement("div", {"className":"addComBox"}, [
		["div", {className:"commentWho", innerHTML:[
			'<a href="http://www.woniu.com/'+(myInfo?myInfo.userId:userId)+'.home" class="imgKK">',
				'<img src="'+(Urls.home(myInfo?myInfo.userId:myId)+'/face/face_S.jpg')+'" />',
			'</a>',
		].join("")}],
		["div", {className:"addComBoxR"}, [
			["textarea",{
				className: "textWhite addtext",
				ref: "ipt",
				onkeyup: function(){
					replyBox.rplExpand(this);
				},
				onpaste: function(){
					replyBox.rplExpand(this);
				},
				onblur: function(){
						setTimeout(function(){
							replyBox.rplBlur();
						},200)
				},
				onchange: function(){
					replyBox.rplExpand(this);
				},
				onkeydown: function(e){
					var evt = e || event;
					if (evt.ctrlKey && evt.keyCode==13) {
						replyBox.rplOk();
						return false;
					}
				}
			}],
			["a",{href: "javascript:;",	className: "face", ref: "face",	onclick:function(){replyBox.showFaceCtrl(this);	}, innerHTML:"&nbsp;"}],
			["a",{href:"javascript:;",className:"btn",ref:"ok",onclick:function(){replyBox.rplOk();}, innerHTML:"<span></span><b>评论</b>"}],
			["span",{className:"addComBoxHint", innerHTML:"Crtl+Enter提交"}]
		]]
	]),
	getFaceCtrl : function() {
		if (!replyBox.faceCtrl) {
			var imgs = [];
			for (var i=1;i<=97;i++) {
				imgs.push('<img src="http://resimage.woniu.com/comres/homespace/manage/style/images/facePrew/'+i+'.gif" border="0" onclick="replyBox.attachFace('+i+');" />');
			}
		
			replyBox.faceCtrl = FWK.Util.createElement("div",{className:"faceKK_KK", style:{display:"none"}},[
				["div",{className:"facingKK", innerHTML : imgs.join("")}]
			]);
			
			document.body.appendChild(replyBox.faceCtrl);
			FWK.addEvent(document.body,"click",replyBox.hideFaceCtrl);
		}
		
		return replyBox.faceCtrl;
	},
	attachFace : function(i) {
		var ipt = replyBox.tmpIpt||replyBox.replyCtrl["refs"]["ipt"];
		ipt.focus();
		ipt.value += "[em:$]".replace("$",i);
		
		setTimeout(function(){
			replyBox.rplExpand();
		},100);		
		
		replyBox.hideFaceCtrl();
//		var range = ipt.createTextRange();
//		range.pasteHTML("[em:$]".replace("$",i));
	},
	showFaceCtrl:function(dom,ipt) {
		var evt = window.event || arguments.callee.caller.arguments[0];
		window.event?evt.cancelBubble = true:(evt.stopPropagation&&evt.stopPropagation());
		replyBox.replyCtrl["refs"]["ipt"].focus();
		var pos = null;
		try {
			pos = FWK.getDomCoord(dom)
		}catch(ex) {
			pos = $(dom, "getCoord");
		}
		
		var faceCtrl = replyBox.getFaceCtrl();
		faceCtrl.style.display = "block";
		
		faceCtrl.style.top = this.mode ? pos.top - faceCtrl.offsetHeight + "px": pos.top + "px";
		faceCtrl.style.left = pos.left + "px";		
		
		if (ipt) {
			replyBox.tmpIpt = ipt;
		}
	},
	hideFaceCtrl : function() {
		var faceCtrl = replyBox.getFaceCtrl();
		faceCtrl.style.display= "none";
		
		replyBox.tmpIpt = null;
	},
	rplOk:function(){
		if (remindLogin()) return;
		var evt = window.event || arguments.callee.caller.arguments[0];
		window.event?evt.cancelBubble = true:(evt.stopPropagation&&evt.stopPropagation());
		var val = replyBox.replyCtrl["refs"]["ipt"].value.trim();
		
		if (!val) {
			 replyBox.rplSetFocus();
			replyBox.fade(replyBox.replyCtrl["refs"]["ipt"],9);
		}
		else {
			replyBox.addReply(val);
		}
	},
	fade: function(node,level){

		level||(level=9);
		var step = function(){
			var hex = level.toString(16);
			node.style.backgroundColor = '#FF'+ hex + hex + hex + hex;
			if (level < 15) {
				level += 1;
				setTimeout(step, 50);
			}
		};
		setTimeout(step, 50);
	},
	rplCancel:function(id){
		if (this.mode) {
			replyBox.replyCtrl["refs"]["ipt"].value = "";
			replyBox.rplExpand();
			replyBox.replyCtrl["refs"]["ipt"].blur();
			try{parent.expandRight();}catch(e){}
			return;
		}
		
		id = id || replyBox.resId;
		var dom = $("rpl_res_"+id);
		var fireDom = $("rpl_fire_"+id);
		if (dom && fireDom) {
			fireDom.style.display = "";
			fireDom.parentNode.style.display = "none";
			
			var currentParent = replyBox.replyCtrl.parentNode;
			if (currentParent) {
				currentParent.removeChild(replyBox.replyCtrl);
			}
			
			replyBox.h = 0;
		}
	},
	rplExpand : function(dom) {
		dom = dom || replyBox["replyCtrl"]["refs"]["ipt"];
		setTimeout(function(){
//			alert(dom.value)
			if (dom.value.length > 150) {
				dom.value = dom.value.substr(0, 150);
			}
			var h = dom.scrollHeight;
			var c = dom.offsetHeight;
	//		debugger;
	//console.log([h,c])
			if (Math.abs(h - c) > 20) {
				h > c ? WNSNS.expandDom(dom, h, c) : WNSNS.collapseDom(dom, c, h);
			}
		},200)
		
	},
	showReply : function(id, toWhom) {
		if (replyBox.resId&&id!=replyBox.resId) {	//上一个，要关闭
			replyBox.rplCancel(replyBox.resId);
		}
		var dom = $("rpl_res_"+id);
		var fireDom = $("rpl_fire_"+id);
		if (dom) {
			replyBox.h = dom.offsetHeight; //document.documentElement.scrollTop;
			fireDom.style.display = "none";
			fireDom.parentNode.style.display = "";
//			http://my.woniu.com/newuser/freply.do?m=query&resType=00&resId=24
			var ipt = replyBox.replyCtrl["refs"]["ipt"];
			toWhom ? ipt.value = ("回复 "+toWhom + "：") : ipt.value = "";
			ipt.style.height = "";
			replyBox.resId = id;
//			var D=replyBox.replyCtrl.parentNode;
//			var tempD=document.createElement("div");
			
			fireDom.parentNode.appendChild(replyBox.replyCtrl);
//			fireDom.parentNode.insertBefore(replyBox.replyCtrl,tempD);
//			fireDom.parentNode.removeChild(tempD);

			if (this.mode && ipt.value.trim() == "") return;
			
			if (FWK.isIE) {
				var range = ipt.createTextRange();
				range.moveStart('character',ipt.value.length);   
				range.collapse(true); 
//				setTimeout(function(){
					range.select();
//				},20);
				
			} //-->mk
//			setTimeout(function(){
//				ipt.focus();
//			},10);
		}
		
	},
	rplBlur : function(id) {
		if(replyBox.replyCtrl["refs"]["ipt"].value.trim() == "") {
//			debugger;
			var evt = window.event || arguments.callee.caller.arguments[0];
			var target = (evt&&evt.target)||document.activeElement;
			if( replyBox.replyCtrl["refs"]["ipt"]==document.activeElement){
				return
			}
			(evt&&evt.target)&&(target=target.parentNode.childNodes[1]);
//			alert(target === replyBox.replyCtrl["refs"]["face"]);
			if (target != replyBox.replyCtrl["refs"]["face"]&&target != replyBox.replyCtrl["refs"]["ok"]) {
				replyBox.rplCancel(id);
			}
		}		
	},
	rplSetFocus : function() {
		replyBox.replyCtrl["refs"]["ipt"].focus();
	},
	addReply : function(val){
		if(replyBox.timeInterval){
			if(Math.floor(replyBox.timeInterval.dateDiff("second"))<10){
				FWK.alert("你说话的速度太快啦");
				return;
			}
		}
		var postVars = {
			m: "add",
			resType: this.resType,
			resId: replyBox.resId,
			content: val
		};
		var tp_fn=replyBox.ajax.$onSuccessed;
		replyBox.ajax.$onSuccessed=function(){
			tp_fn.call(replyBox.ajax);
			replyBox.timeInterval=new Date();
		};
		replyBox.ajax.asyncAsk(postVars);
	},
	getMore : function(rid) {
		replyBox.resId = rid;
		
		var postVars = {
			m:"query",
			resType:this.resType,
			resId:replyBox.resId
		};
		replyBox.ajax.asyncAsk(postVars);
	},
	del : function(id,rid){
		replyBox.resId = rid;
		
		var postVars = {
			m:"delete",
			commId:id,
			resId:replyBox.resId,
			resType:this.resType
		};
		
		replyBox.ajax.asyncAsk(postVars);
	},
	showCount:function(){
		
	}
};


