var this_URL=location.protocol+"//"+location.host
var uc_regUrl="inc_Fun/uc_register.utf8.asp"	//UC注册网址
var signUrl="Default.aspx?class=SignImage&temp="+Math.random()	//验证码网址
var	uc_regErr=["用户名不合法","包含不允许注册的词语","用户名已经存在","Email 格式有误","Email 不允许注册","该 Email 已经被注册","请重新登录，并选中 [以后自动登录]","校验用户信息出错，请重新登录"]	//UC注册返回的错误信息

var cookiepre="club_isso"
var cookiedomain=""
var cookiepath=""


var none=function(){}

//js cookie copy from discuzX
function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	if(cookieValue == '' || seconds < 0) {
		cookieValue = '';
		seconds = -2592000;
	}
	expires.setTime(expires.getTime() + seconds * 1000);
	domain = !domain ? cookiedomain : domain;
	path = !path ? cookiepath : path;
	document.cookie = escape(cookiepre + cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function getcookie(name, nounescape) {
	name = escape(cookiepre + name);
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	if(cookie_start == -1) {
		return '';
	} else {
		var v = document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length));
		return !nounescape ? unescape(v) : v;
	}
}

function msgalert(msg,rpcr){
	//msg 为jquery
	//rpcr是否在行末加上\n
	//逗号换行
	var txt=""
	msg.each(function(){
		//alert($(this).text())
		txt+=$(this).text().replace(/，/g,"\n\n")
		if (!rpcr) txt+="\n"
	})
	return txt;
}
function doit(obj,qry,url,s_callback,f_callback,e_callback,txt,ifshowok,ifshowerr,obj_t1){
	//s_callback:成功后执行的回调函数
	//f_callback:失败后执行的回调函数
	//e_callback:全部完成后的回调函数
	//txt:显示文字最初部分,如果不输入则取obj文字
	//obj_t1 本函数中无用处，传回去用
	//ifshowok:成功后是否显示文字
	//ifshowerr:失败后是否显示文字
	$.post(url,qry,function(data){
		var xml=$(data)
		if (txt==undefined||txt=="") txt=obj.text()
		if (xml.find("success").text()=="True")
		{
			if (ifshowok==undefined)ifshowok=true
			ifshowerr=false
			txt+="成功\n\n"
			s_callback(obj,xml,obj_t1)
		}
		else{
			if (ifshowerr==undefined)ifshowerr=true
			ifshowok=false
			txt+="失败，原因：\n\n"
			f_callback(obj,xml,obj_t1)
		}
		txt+=msgalert(xml.find("Msg"),true)
		if (ifshowok||ifshowerr)alert(txt)
		e_callback(obj,xml,obj_t1)
	})
}
//计算字符串长度  
function strlen(str) {  
	var len = 0;  
	for (var i = 0; i < str.length; i++) { 
		if (str.charCodeAt(i) > 255 || str.charCodeAt(i) < 0) len += 2; else len ++;  
	}  
	return len;  
}

//时间对象的格式化;

Date.prototype.format = function(format){
 /*
  * eg:format="YYYY-MM-dd hh:mm:ss";
	var today=new Date()
	var posttime=today.format("yyyy-MM-dd hh:mm:ss");
  */
 var o = {
  "M+" :  this.getMonth()+1,  //month
  "d+" :  this.getDate(),     //day
  "h+" :  this.getHours(),    //hour
  "m+" :  this.getMinutes(),  //minute
  "s+" :  this.getSeconds(), //second
  "q+" :  Math.floor((this.getMonth()+3)/3),  //quarter
  "S"  :  this.getMilliseconds() //millisecond
   }
 
   if(/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
   }
 
   for(var k in o) {
    if(new RegExp("("+ k +")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    }
   }
 return format;
}

var win = new Object();

win.box=$("<div/>")
win.box2=$("<div/>")

win.express=function(title){
	return $("<div class=\"msg_2\"><div class=\"msg_3\"><h3>"+title+"</h3><div class=\"msg_4\"><textarea name=\"msgtxt\" class=\"msg_txt\"></textarea><ol><li>以标题方式提交，字数不能超过1000字，格式无效</li><li>发帖成功后页面仅模拟插入结果，刷新后方可见完整结果</li><li>IE8,FireFox8.0,Chrome浏览器测试成功，其他未测试</li></ol><div class=\"clear\"><xsl:comment></xsl:comment></div></div></div><input type=\"submit\" class=\"msg_submit\" value=\"发帖\"/><input type=\"button\" class=\"msg_close\" value=\"关闭\"/></div>")
}
win.epTopicBox=function(title){
//快速发帖，回帖的窗口内的内容
	var msg2=$("<div/>").addClass("msg_2")
	var msg3=$("<div/>").addClass("msg_3").appendTo(msg2)
	var tt=$("<h3/>").text(title).appendTo(msg3)
	var msg4=$("<div/>").addClass("msg_4").appendTo(msg3)
	var msgtxt=$("<textarea/>").attr("name","msgtxt").addClass("msg_txt").appendTo(msg4)
	var ol=$("<ol><li>以标题方式提交，字数不能超过1000字，格式无效</li><li>发帖成功后页面仅模拟插入结果，刷新后方可见完整结果</li><li>IE8,FireFox8.0,Chrome浏览器测试成功，其他未测试</li></ol>").appendTo(msg4)
	var clear=$("<div/>").addClass("issoclear").appendTo(msg4)
	var submit=$("<input type=\"button\"/>").addClass("msg_submit").val("发帖").appendTo(msg2)
	var close=$("<input type=\"button\"/>").addClass("msg_close").val("关闭").appendTo(msg2)
	return {msg2:msg2,msg4:msg4,msgtxt:msgtxt,submit:submit,close:close}
}

win.pos=function(p,bv,ov,b){
	//p:位置，1/2/3,bv:窗口的宽或高,ov:目标的宽或长,b:边框宽度
	if (p==0)
	{
		return 0;
	}
	else if (p==1)
	{
		return (bv-ov-2*b)/2;
	}
	else{
		return (bv-ov-2*b)
	}
}

win.float={
	zindex:1000,
	open:function(w,h,t,o,x,y,s,p,bw,para,foc,f,cf,ic){
		/*
		w:width,h:height,t:title,o:obj,jquery对象，显示的内容;f:执行的函数;para:参数，通过函数f回传,foc:聚焦的地方,cf:关闭窗口前执行的函数,ic:图标的CSS
		x:0/1/2 左中右。y:0/1/2 上中下
		p:插入到哪一层
		bw:边框宽度
		s:true:显示阴影
		*/
		var st=$(document).scrollTop()
		win.float.zindex+=2
		var left=win.pos(x,$(window).width(),w,bw)
		var	top=win.pos(y,$(window).height(),h,bw)+st

		var box=$("<div/>").addClass("openbox").width(w).height(h).css({left:left,top:top,"z-index":win.float.zindex})
		
		var title=$("<div/>").addClass("title").appendTo(box)
		if (ic!=undefined && ic!="")
		{
			var icon=$("<div/>").addClass(ic).appendTo(title)
		}
		var close=$("<div/>").addClass("close").appendTo(title)
		var h1=$("<h1/>").text(t).appendTo(title)
		o.appendTo(box)
		box.appendTo(p)
		if (s)
		{
			var box2=$("<div/>").addClass("openbox2").width(w).height(h).css({left:left+5,top:top+5,opacity:0.6,"z-index":win.float.zindex-1})
			box2.appendTo(p)
		}
		if (!$.isEmptyObject(foc))
		{
			foc.focus()
		}
		var obj={box:box,box2:box2,o:o}
		$(".close",box).click(function(){cf(obj,para);win.float.close(obj)})
		box.mousedown(function(){
			var z=box.css("z-index")
			if (z<win.float.zindex)
			{
				win.float.zindex+=2;
				box.css("z-index",win.float.zindex)
				box2.css("z-index",win.float.zindex-1)
			}
		})		
		title.mousedown(function(event){
			box.data("close",false)
			var ppshow=true
			var pp=$("<div/>").height($("body").height()).width("100%").css({position:"absolute",left:0,top:0,background:"#666",opacity:0.3,"z-index":100,cursor:"move"})
			var divX=box.position().left
			var divY=box.position().top
			var x=event.clientX;
			var y=event.clientY;
			$(document).mousemove(function(event){
				if (ppshow)
				{
					pp.appendTo($("body"))
					ppshow=false
				}
				var xOffset1=event.clientX-x;
				var yOffset1=event.clientY-y;
				box.css({left:xOffset1+divX,top:yOffset1+divY});
				box2.css({left:xOffset1+divX+5,top:yOffset1+divY+5})
			})
			title.mouseup(function(){
				$(document).unbind("mousemove")
				pp.remove();
				box.data("close",true)
			})
		})
		f(obj,para)
	},
	close:function(obj){
		if ((!obj.box.data("close"))&&(obj.box.data("close")!=undefined))
		{
			obj.o.empty().append($("<p>您已移动窗口，自动关闭被取消<br/><br/>请单击右上角的关闭按钮手动关闭！</p>").css("padding-top",20));
			return false;
		}
		obj.box.empty().remove();
		obj.box2.remove();
	}
}
win.menu={
	open:function(w,h,o,x,y,s,p,m,c){
		/*
		w:width,h:height,o:显示的内容(jquery),x:left,y:top,s:是否阴影,p:要插入的地方(jquery),m:是否在屏幕边缘校正偏移
		c:给menu以及阴影层附加一个CLASS，这个CLASS一般用来控制宽度等信息
		*/
		win.p=p
		win.p.css("position","relative")
		win.box.empty();
		win.box2.empty();
		win.box.addClass("openmenu").css({left:x,top:y})
		if (c!=undefined) win.box.addClass(c)
		o.appendTo(win.box)
		if (w > 0) win.box.width(w)
		if (h > 0) win.box.height(h)
		win.box.appendTo(win.p)
		if (s)
		{
			win.box2.addClass("openmenu2").width(win.box.width()).height(win.box.height()).css({left:x+2,top:y+2,opacity:0.6})
			if (c!=undefined) win.box2.addClass(c)
			win.box2.appendTo(win.p)
		}
		if (m)
		{
			var t1,t2
			var of=win.box.offset();
			var st=$(document).scrollTop()
			//left:
			t1=0
			if (of.left<0)
			{
				t2=t1-of.left+parseInt(win.box.css("left"))
				win.box.css({left:t2})
				win.box2.css({left:t2+2})
			}
			//right:
			var t1=$(window).width()-win.box.width()-22			
			if (of.left > t1)
			{
				t2=t1-of.left+parseInt(win.box.css("left"))
				win.box.css({left:t2})
				win.box2.css({left:t2+2})
			}
			//top:
			if (of.top<st)
			{
				t1=st
				t2=t1-of.top+parseInt(win.box.css("top"))
				win.box.css({top:t2})
				win.box2.css({top:t2+2})
			}
			//bottom
			t1=$(window).height()+st-win.box.height()-4			
			if (of.top > t1)
			{
				t2=t1-of.top+parseInt(win.box.css("top"))
				win.box.css({top:t2})
				win.box2.css({top:t2+2})
			}
		}
	},
	close:function(){
		win.box.empty().remove();
		win.box2.remove();
		win.p.css("position","static")
	}
}


var topic={
	//需要全局变量style_type和exp_type
	//style_type:当前系统的风格，isso/usual/issoold
	signname:"",
	signnum:"",
	block:function(obj,stype){
		if (stype == undefined) stype=style_type
		if (stype=='易索风格' || stype=='isso')
		{
			return obj.closest("div")
		}
		else if (stype=='usual')
		{
			return obj.closest("tr")
		}
	},
	url:function(obj,stype){
		if (stype == undefined) stype=style_type
		if (stype=='易索风格' || stype=='isso')
		{
			return $("a:first",topic.block(obj,stype)).attr("href")
		}
		else if (stype=='usual')
		{
			return $(".fb_title a",topic.block(obj,stype)).attr("href")
		}
	},
	forum_id:function(obj,stype){
		return topic.block(obj,stype).attr("forum_id")
	},
	forum_name:function(obj,stype){
		return topic.block(obj,stype).attr("forum_name")
	},
	id:function(url){
		var x=/(.*=)(\d+)(.*)/
		return url.replace(x,'$2')
	},
	pink:function(url){
		var x=/pink=true|pink=false/i
		return x.exec(url)
	},
	lock:function(obj){
		obj.removeClass().addClass("forum_3 HeadType1")
	},
	sign:function(){
		var url="/Default.aspx?Class=NewTopic&Forum=1&xml&tmp="+Math.random()
		$.get(url,function(data){
			var xml=$(data)
			topic.signname=$("SignName",xml).text()
			topic.signnum=$("SignNum",xml).text()
			if (topic.signname !="" && topic.signnum != "")
			{
				try
				{
					var str="qry={"+topic.signname+":\""+topic.signnum+"\"}"	//解决生成不合javascript要求的变量名时的问题
					eval(str);
				}
				catch (ex)
				{
					topic.sign()
				}
			}
		})
	},
	nextdel:function(obj){
		var x=obj.next()
		obj.remove()
		return x						
	},
	nextlock:function(obj){
		var x=obj.next()
		topic.lock(obj)
		return x
	},
	user:function(obj,stype){
		if (stype == undefined) stype=style_type
		if (stype=='易索风格' || stype=='isso')
		{
			return $(".user_1 a,.user_2 a",topic.block(obj,stype))
		}
		else if (stype=='usual')
		{
			return $(".fb_author a",topic.block(obj,stype))
		}
	},
	userid:function(obj,stype){
		try{
			return topic.id(topic.user(obj,stype).attr("href"))
		}
		catch(err){
			return -1
		}
	},
	username:function(obj,stype){
		return topic.user(obj,stype).text();
	},
	title:function(obj,stype){
		if (stype == undefined) stype=style_type
		if (stype=='易索风格' || stype=='isso')
		{
			return $("a:first",topic.block(obj,stype)).text()
		}
		else if (stype=='usual')
		{
			return $(".fb_title a",topic.block(obj,stype)).text()
		}
	},
	errCheck:function (text){
		text=text.replace(/\s/gi,"")
		var chkstr=/(.+)\1{10}/gi
		chkstr.lastIndex=0
		return chkstr.test(text)
	},
	userurl:function(uid){
		return "/default.aspx?class=UserInfo&User="+uid
	},
	userlink:function(uid,uname){
		return "<a href=\""+topic.userurl(uid)+"\" target=\"_blank\">"+uname+"</a>"
	},
	icon:function(obj,stype){
		if (stype == undefined) stype=style_type
		if (stype=='易索风格' || stype=='isso')
		{
			return topic.block(obj,stype)
		}
		else if (stype=="usual")
		{
			return $(".icon",topic.block(obj,stype))
		}
	}
}
var sayhello=function()
{
	var today = new Date();
	var h=today.getHours();
	if (h<3){return "夜深了，您该睡觉了"}
	else if (h<5){return "您这是还没睡还是刚起床"}
	else if (h<7){return "您起的好早啊"}
	else if (h<9){return "早上好"}
	else if (h<12){return "上午好"}
	else if (h<13){return "中午好"}
	else if (h<19){return "下午好"}
	else {return "晚上好"}
}

var fmjsstr=function(x){
	var y=x.replace(/\"/g,"\\\"")
	return y
}

var tips={
	bMove:function(obj,ybegin,step,yend,ynow){
		//ybegin:起始，step:间隔，yend:结束处,ynow:当前
		var y=ynow
		if (step>0?(y>=yend):(y<=yend)) 
			y=ybegin
		else
			y=y+step
		if (step > 0)
			obj.css("background-position","0 -"+y+"px")
		else
			obj.css("background-position","0 "+(0-y)+"px")
		obj.data("c",setTimeout(function(){tips.bMove(obj,ybegin,step,yend,y)},150))
	},
	background_position:function(obj,type){
		//type:x or y
		var p=obj.css("background-position")
		if (typeof (p) === "undefined") {
			return obj.css("background-position-"+type)
		}
		else{
			if (type=="x")
				return p.split(" ")[0]
			else
				return p.split(" ")[1]
		}
	},
	button:function(obj,ybegin,step,yend,istopic){
		var y,o,c,t
		obj.hover(function(){
			o=$(this)
			y = tips.background_position(o,"y")
			var n=(!istopic)?topic.username(o,"isso"):author
			if (vname!=''){
				if (n!=vname)
				{
					t=vname+",请支持我吧，点击一下来点亮我的帖子"
				}
				else{
					t="不能自己点亮自己"
				}
			}
			else{
				t=n+"请你点亮帖子来支持他，不过你得先登录"
			}
			o.attr("title",t)
			tips.bMove(o,ybegin,step,yend,ybegin)
		},function(){
			o.css("background-position","0 "+y)
			clearTimeout(o.data("c"))
		})

		obj.click(function(){
			if (vname=='') return
			o=$(this)
			var n=(!istopic)?topic.username(o,"isso"):author
			if (n==vname) return
			var id=(!istopic)?topic.id(topic.url(o,"isso")):Topic
			var pink=(!istopic)?topic.pink(topic.url(o,"isso")):IfPink
			var url="Default.aspx?class=Tips&Topic="+id+"&Pink="+pink+"&xml"

			if (!IfTipsCheck)
			{
				doit(o,{},url,tips.t_ok,none,none,"点亮["+n+"]的帖子",false,true,"")
			}
			else{
				if (confirm("您确定要点亮["+n+"]的这个帖子吗?"))
					doit(o,{},url,tips.t_ok,none,none,"点亮["+n+"]的帖子",true,true,"")
				else
					return
			}
		})
	},
	t_ok:function(obj,xml,t1){
		var n=$(".num",obj)
		n.text(parseInt(n.text())+1)
	}
}

var copy={
	button:function(obj,ybegin,step,yend,istopic){
		var y,o,c,t
		obj.hover(function(){
			o=$(this)
			y = tips.background_position(o,"y")
			var n=(!istopic)?topic.username(o,"isso"):author
			if (vname!=''){
				if (n!=vname)
				{
					t=vname+",觉得我的帖子好就点击此处来复印我的帖子吧"
				}
				else{
					t="不能自己复制自己"
				}
			}
			else{
				t=n+"请你在复印他的帖子之前先登录"
			}
			o.attr("title",t)
			tips.bMove(o,ybegin,step,yend,ybegin)
		},function(){
			o.css("background-position","0 "+y)
			clearTimeout(o.data("c"))
		})
		obj.click(function(){
			if (vname=='') return
			o=$(this)
			var n=(!istopic)?topic.username(o,"isso"):author
			if (n==vname) return
			var id=(!istopic)?topic.id(topic.url(o,"isso")):Topic
			var pink=(!istopic)?topic.pink(topic.url(o,"isso")):IfPink
			var url="Default.aspx?class=ReplyTopic&Topic="+id+"&Pink="+pink+"&isCopy=1&xml"
			var qry={Test:"http://www.isso.com.cn"}
			var txt=(!istopic)?$("a:first",topic.block(o,"isso")).text():$(".table_4 h1").text()
			if (!IfCopyCheck)
			{
				doit(o,qry,url,copy.t_ok,none,none,"复印["+n+"]的帖子",true,true,txt)
			}
			else{
				if (confirm("您确定要复印["+n+"]的这个帖子吗?"))
					doit(o,qry,url,copy.t_ok,none,none,"复印["+n+"]的帖子",true,true,txt)
				else
					return
			}
		})
	},
	t_ok:function(obj,xml,t1){
		var n=$(".num",obj)
		n.text(parseInt(n.text())+1)
		var o=$("div:first",$(".TopicTree"))
		var url=($("a:first",$("Msg",xml)).attr("href"))
		var tp=topicInsert(o,url,t1) //PostTopic.js
		if (!tp.hasClass("forum_copy"))tp.addClass("forum_copy")
	}
}
