function intval(v){v=parseInt(v);return isNaN(v)?0:v;}function getPos(e){var l=0;var t=0;var w=intval(e.style.width);var h=intval(e.style.height);var wb=e.offsetWidth;var hb=e.offsetHeight;while(e.offsetParent){l+=e.offsetLeft+(e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);t+=e.offsetTop+(e.currentStyle?intval(e.currentStyle.borderTopWidth):0);e=e.offsetParent;}l+=e.offsetLeft+(e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);t+=e.offsetTop+(e.currentStyle?intval(e.currentStyle.borderTopWidth):0);return {x:l,y:t,w:w,h:h,wb:wb,hb:hb};}function getScroll(){var t,l,w,h;if(document.documentElement&&document.documentElement.scrollTop){t=document.documentElement.scrollTop;l=document.documentElement.scrollLeft;w=document.documentElement.scrollWidth;h=document.documentElement.scrollHeight;}else if (document.body){t=document.body.scrollTop;l=document.body.scrollLeft;w=document.body.scrollWidth;h=document.body.scrollHeight;}return {t:t,l:l,w:w,h:h};}function scroller(el,duration){if(typeof el!='object'){el=document.getElementById(el);}if(!el)return;var z=this;z.el=el;z.p=getPos(el);z.s=getScroll();z.clear=function(){window.clearInterval(z.timer);z.timer=null};z.t=(new Date).getTime();z.step=function(){var t=(new Date).getTime();var p=(t-z.t)/duration;if(t>=duration+z.t){z.clear();window.setTimeout(function(){z.scroll(z.p.y,z.p.x)},13);}else{st=((-Math.cos(p*Math.PI)/2)+0.5)*(z.p.y-z.s.t)+z.s.t;sl=((-Math.cos(p*Math.PI)/2)+0.5)*(z.p.x-z.s.l)+z.s.l;z.scroll(st,sl);}};z.scroll=function(t,l){window.scrollTo(l,t)};z.timer=window.setInterval(function(){z.step();},13);}


//*留言部分*//
var comt;
var my;
$.fn.comment = function(options) {
	options = options || {};
	chn = options.chn;
	eid = options.eid;
	cui = options.uid;
	oid = options.oid || 0;
	p = options.p?options.p:1;
	$("<style>.line_solid {border-bottom:1px solid #ccc;}</style>").appendTo("head");
	my = $(this);	
	my.css({"width":"100%","float":"left"});
	my.html("<ul><img src='/images/load.gif' alt='数据加载中,请稍候......' /></ul>");
	my.cmtform({psi:'prepend'});
	$("#showface").css({"cursor":"pointer"}).bind("click",$.showface);
	if(Boolean(comt)!=false) comt.abort();
	var s = new Date().getTime();
	comt = $.getJSON("/comment/json?s="+s,{chn:chn,eid:eid,p:p},function(data){
		my.children("ul").empty();
		$.each(data.data,function(i,cmtArr){
			tcmtHtml=fcmtHtml=scmtHtml="";
			$.each(cmtArr,function(j,cmt) {
					cmt.replyid = i;
					if(j==0){
						cmt.level=1;
						fcmtHtml = $.cmtflihtml(cmt);
					}else{
						cmt.level=2;
						if(cmt.rcontent){cmt.level=3;}
						scmtHtml += $.cmtflihtml(cmt);
					}
			});
			tcmtHtml = fcmtHtml+scmtHtml;
			my.children("ul").append($.cmtfli({html:tcmtHtml}));
		});
		a = Number(data.a);
		if(a>1){
			tagdiv = $("<div />").css({"margin":"10px 0","padding":"10px","text-align":"center"});
			for(i=1;i<=a;i++){
				tagspan = $("<span />").css({"border":"1px solid #AEDEF9","color":"#0066CB","padding":"5px 10px","text-decoration":"none","margin":"0 5px 0 0","font-weight":"bold","cursor":"pointer"}).attr("id",i).bind("click",$.cmtgopage).html(i);
				if(i==p) { tagspan.css({"background":"#DDF5FB","color":"#000000"});}
				tagdiv.append(tagspan);
			}
			tagdiv.appendTo(my);
		}
		if(data.data){
			$(".cmt_reply").bind("click",$.cmtreply);
			$(".cmt_delete").bind("click",{t:'del'},$.cmtdeal);
		}
		cmt_hash = window.location.hash.replace(/#/,'');
		if(cmt_hash){scroller(cmt_hash,500);}
	});
}
$.cmtgopage = function(e) { $(my).comment({chn:chn,eid:eid,uid:cui,p:$(this).attr('id')});}
$.cmtfli = function(options){
	return '<li  class="line_solid">'+options.html+'</li>';
}

$.cmtflihtml = function(options){
	options = options || {};
	cmt=options;
	cmthtml = "";
	if(cmt.level==1){
		cmthtml += "<div style='line-height:20px;text-align:left;clear:both;margin-top:5px;'>";		
	}else{
		cmthtml += "<div style='border-top:1px dashed #CCCCCC;margin-left:50px;padding-top:3px;'>";
		if(cmt.level==3){
			cmthtml += '<p style="border:1px solid #DFDFDF;color:#999999;margin:5px 0;padding:3px;width:98%;">@ '+cmt.rcontent+'</p>';
		}		
	}
	cmtdel = "";
	if(cui === cmt.uid){
		cmtdel = ' <span class="a2"><a href="#" id="s'+options.id+'" class="cmt_delete">删除</a></span>';
	}
	if(cui === cmt.oid){
		cmtdel = ' <span class="a2"><a href="#" id="o'+options.id+'" class="cmt_delete">删除</a></span>';

	}
	
	cmthtml += "<a id='com_"+cmt.id+"' name='com_"+cmt.id+"' /><em style='float:left;'><a href='/space/indexspace.php?userid="+cmt.uid+"'><img alt='"+cmt.uname+"' src='"+cmt.uphoto+"' style='border:1px solid #CCCCCC;height:40px;width:40px;'/></a></em><span class='cmt_uname'><a href='/space/indexspace.php?userid="+cmt.uid+"'>"+cmt.uname+"</a>：</span><span class='cmt_con'>"+cmt.content+"</span><p style='text-align:right;clear:both;margin-top:5px;'><span class='msgtag_time gray'>"+cmt.date+"</span> <span class='a1'><a href='#' id='r"+options.rid+"' class='cmt_reply'>回复</a></span>"+cmtdel+"</p>";
	cmthtml += "</div>";
	return cmthtml;
}
$.cmtreply = function(event){
	rid = $(this).attr("id");
	rid = rid.substring(1,rid.length);
	$(this).parent().parent().cmtform({rid:rid});
	return false;
}
$.fn.cmtform = function(options){
	options = options || {};
	if(options.psi != 'prepend'){
		var ml = "50px";
	}
	var othform = $("div [class='cmt_form']");
	if(othform.length){
		othform.hide();
	}
	var cmtform = $(this).children("div [class='cmt_form']");
	if(cmtform.length){
		cmtform.toggle();
	}else{
		if(options.psi != 'prepend'){
			var cname = 'cmt_form';
		}
		cmtForm = '<div style="margin-left:'+ml+';" class="'+cname+'"><img src="/space/images/face/%231.gif" class="cmt_showface" style="cursor:pointer;float:left;" /><br /><textarea name="content" type="textarea" class="texTextarea" onblur="this.className=\'texTextarea\'" onfocus="this.className=\'texTextarea1\'"/><p style="border-bottom:1px solid #CCCCCC;margin-top:5px;padding-bottom:2px;text-align:right;width:100%;"><input type="submit" class="cmt_add msg" id="rid_'+(options.rid || 0)+'" value="发布评论"/></p></div>';
		if(options.psi == 'prepend'){
			$(this).prepend(cmtForm);
		}else{
			$(this).append(cmtForm);
		}
		$(".cmt_showface").bind("click",{},$.showface);
		$(".cmt_add").bind("click",{t:"add",eid:eid,chn:chn},$.cmtdeal);
	}
}
$.showface = function(event) {
	t = event.data.t?$(event.data.t):$(this).parent().children("textarea");
	facebox = $("#facebox");
	offset = $(this).offset();
	left = offset.left;
	mtop = offset.top+20;
	if(facebox.length) {
		foffset = facebox.offset();
		ftop = foffset.top;
		if(facebox.css('display')=='none'){
			facebox.css({"left":left,"top":mtop});
			facebox.show()
		}else{
			if(ftop!=top){
				facebox.css({"left":left,"top":mtop});
			}else{
				facebox.toggle();
			}
		}
	} else {
		innerHTML = "<table cellpadding='2' cellspacing='2'><tr>";
		for(i=1; i<22; i++) {
			innerHTML += '<td><img src="/space/images/face/%23'+i+'.gif" id="f'+i+'" style="cursor:pointer; position:relative;" /></td>';
			if (i%7==0){
				innerHTML += "</tr><tr>";
			}
		}
		innerHTML += "</tr></table>";
		innerHTML += "<div style='text-align:right;background-color:#CBE1EC;padding:1px;line-height:20px;'><span style='text-decoration:none;color:#333;cursor:pointer'>关闭</span></div>";
	
		$("<div />").css({"position":"absolute","margin":"0px 0px","background-color":"#fff","border":"2px solid #CBE1EC","z-Index":20000,"left":left,"top":mtop}).attr("id","facebox").html(innerHTML).appendTo("body");
		$("#facebox span").bind("click",function(){ $(this).parent().parent().hide();});
	}
	$("#facebox img").bind("click",{t:t[0]},$.insertface).css({"cursor":"pointer"});	
	return false;
}
$.insertface = function (event){
	target = event.data.t;
	pos = $.getselection(target);
	con = $(target).val();
	id = $(this).attr('id');
	i = id.substring(1,id.length);
	con = con.substring(0,pos)+'[:#'+i+':]'+con.substring(pos,con.length);
	$(target).val(con);
	$("#facebox").hide();
	$(target).focus();
}
$.getselection = function (e) {
	if(e.selectionStart != undefined && e.selectionEnd != undefined)
	{
		return e.selectionEnd; 
	}
	else
	{
		e.focus();
		var rngSel = document.selection.createRange();
		var rngTxt = e.createTextRange();
		var flag = rngSel.getBookmark();
		rngTxt.collapse();
		rngTxt.moveToBookmark(flag);
		rngTxt.moveStart('character',-e.value.length);
		str = rngTxt.text.replace(/\r\n/g,'');
		return(str.length);
	}
}
$.cmtdeal = function(e){
	t = e.data.t;
	rid = $(this).attr("id");
	rid = rid.substring(4,rid.length);
	pho = e.data.pho?e.data.pho:false;	
	own = $(this);
	switch(t){
		case "add":
			chn = e.data.chn;
			eid = e.data.eid;
			p = $(this).parent().parent();
			height = p.height();
			width = p.width();
			offset = p.offset();
			mtop = offset.top;
			left = offset.left;
			p.append($("<div style='z-index:1338;height:"+height+"px;width:"+width+"px;top:0;left:0;top:"+mtop+"px;left:"+left+"px;position:absolute;text-align:left;'><img src='/images/load.gif' style='padding-top:"+(height/2)+"px;padding-left:"+(width/2)+"px;'></div>").css({opacity:0.2,"background-color":"black"}));
			ppp = own.parent().parent().parent().parent();
			rcontent = "";
			if(ppp.css("margin-left")=="50px"){
				a = ppp.children("span[class='cmt_uname']");
				n = a.text();
				s = ppp.children("span[class='cmt_con']");
				c = s.html(); 
				rcontent = n+c;
			}
			
			strContent = (pho==true)?p.children("input").val():p.children("textarea").val();
			if(!!strContent==false){
				Boxy.alert("内容不能为空",function(){p.children("div").hide();},{title:"提示"});
				break;
			}
			$.post("/comment/add",{chn:chn,eid:eid,content:strContent,rid:rid,rcontent:rcontent},function(msg){
				if(pho==false){
					p.children("div").remove();
					p.children("textarea").val("");
				}
				if(pho==true){
					p.children("input").val("");
				}
				if(msg.offline){
					mask("/loginbox.php",'','登录六人行');
					return false;
				}
				if(msg.error){
					Boxy.alert(msg.error,function(){t.focus();},{title:"提示"});
					return false;
				}
				if(msg.success){
					if(pho==true){
						bAlert("发布评论成功！");
						getReplyText(eid);
						return false;
					}				
					if(msg.info.level==1){
						nreply = $.cmtfli({html:$.cmtflihtml(msg.info)});
						my.children("ul").prepend(nreply);
					}else{
						nreply = $.cmtflihtml(msg.info);
						own.parent().parent().parent().parent().after(nreply);
						own.parent().parent().hide();
					}
					$(".cmt_reply").bind("click",$.cmtreply);					
					$(".cmt_delete").bind("click",{t:"del"},$.cmtdeal);
					return false;
				}
			},'json');
			break;
		case "del":
			id = own.attr('id');
			l = id.length;
			i = id.substring(1,l);
			w = id.substring(0,1);//s是自己删除o事件拥有者删除
			$.post("/comment/del",{id:i,w:w},function(msg){
				if(msg.offline){
					mask("/loginbox.php",'','登录六人行');
					return false;
				}			
				if(msg.error){
					Boxy.alert(msg.error,function(){},{title:"提示"});
					return false;
				}
				if(msg.success) {
					ot = own.parent().parent().parent();
					if(ot.css("margin-left")=='0px' || ot.css("margin-left")=='auto') {ot=ot.parent()}
					ot.fadeOut();
				}
			},'json');
			break;
		default:
			break;
		
	}
	return false;
}
