var oldTypes={"个性签名":1,"个人日记":2,"天下杂侃":3,"情感天地":4,"休闲搞笑":5,"原创文学":6,"影音数码":7,"体育博彩":8,"游戏动漫":9,"衣食住行":10,"彩信日志":11,"投　　票":21,"偶像评论":101}

//基础函数
String.prototype.convCR=function(r){
	return (!!r)?this.replace(/<br \/>/g,"\n"):this.replace(/\n/g,"<br />");
};

String.prototype.convSP=function(r){
	return (!!r)?this.replace(/&nbsp;/g," "):this.replace(/ /g,"&nbsp;");
};

String.prototype.toRealStr=function(){
	return this.replace(/&quot;/g,"\"").replace(/(?:&#39;)|(?:&apos;)/g,"\'").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");
};

String.prototype.getRealLength = function(){
	return this.replace(/[^\x00-\xff]/g,"aa").length;
};

String.prototype.URLencode=function(){
	return this.replace(/[\x09\x0A\x0D\x21-\x29\x2B\x2C\x2F\x3A-\x3F\x5B-\x5E\x60\x7B-\x7E]/g,function(a){return "%"+((a.charCodeAt(0)<16)?("0"+a.charCodeAt(0).toString(16)):(a.charCodeAt(0).toString(16)))}).replace(/[\x00-\x20 ]/g,"+");
};

//日志内容
	var blogContent = '';
	var g_JData = new Object();
	
	function enterMyMall(){//装扮我的空间
		try{
			if (frames['toolbarFrame'].checkLogin()) {
				if (parseInt(g_iLoginUin)<10000) {
					alert("您未登陆空间,或者登陆超时,请重新登陆");
					return;
				}
				window.location.href='http://'+g_iLoginUin+'.qzone.qq.com/?openmall';
			}
		}catch(e){}
	}
	
	function showError(st,back){ //错误提示
		if(st) showOpratingMsg(st); else showOpratingMsg("您暂时无法连接到服务器,请您稍候再试...");
		if(back)
			opratingMsgTimeout = setTimeout("hideOpratingMsg();top.guideButton(1)",msgLife+500);
		else
			opratingMsgTimeout = setTimeout("hideOpratingMsg()",msgLife+500);
	}
	
	function addFavorUin(uin){//添加关注
		if(!frames['toolbarFrame'].checkLogin()) return;
		if(!uin) uin=g_iUin;
		showModalDialog("http://imgcache.qq.com/qzone/admin/ubb_fav.html",{"other_uin":uin,"uin":top.g_iLoginUin,"main":"users.qzone.qq.com","property":top.g_Property},"dialogHeight: 360px;  dialogWidth: 350px; resizable=yes;status:no");
	}
	
	function $(id) {return document.getElementById(id);}
	function hideToolBar(){//隐藏工具条
		$("toolbarFrame").style.display="none";
		$("toolbarImg1").style.display="block";
		$("toolbarImg2").style.display="block";
	}
	
	function showToolBar(){//显示工具条
		$("toolbarFrame").style.display="block";
		$("toolbarImg1").style.display="none";
		$("toolbarImg2").style.display="none";
	}
	function copyBlogURL(){
		clipboardData.setData("Text",getBlogURL());
		try{showMsgbox("本文网址已经复制到剪贴板中", 1, 2000);}catch(err){}
	}
	function getBlogURL(){return ("http://qzone.qq.com/blog/"+uin+"-"+currentBlogId);}

	function refreshVerifyImage(){
		top.document.getElementById("imgVerify2").src="http://ptlogin2.qq.com/getimage?aid=8000103&leo="+Math.random();
		top.document._vcodeGetterForm.verifycodeTrans.value='';
	}
	function quoteBlog(){
		if (frames['toolbarFrame'].checkLogin()){
			document.getElementById("quotePopup").style.display = "";
			refreshVerifyImage();
		}
	}
	
	function closeQuote(){
		document.getElementById("quotePopup").style.display = "none";
	
	}
	
	function goQuote(){
		//doQuoteBlog();
		var url = 'http://b.qzone.qq.com/cgi-bin/blognew/blog_get_data?uin=' + g_iUin + '&blogid=' + currentBlogId + '&arch=0&direct=1';
		top.loadJsonData('quoteBlogContent',url,doQuoteBlog,function(){alert('系统繁忙!')},true,'GB2312',"_Callback");
		return false;
	}
	
	function doQuoteBlog(){
		//检查回调数据是否有效
		var nContent = (g_JData['quoteBlogContent'].data && g_JData['quoteBlogContent'].data.content)?g_JData['quoteBlogContent'].data.content:'';
		if(!nContent){showError('读取日志消息失败！');return;}
		var vc = top.document._vcodeGetterForm.verifycodeTrans.value;
		var url = "http://b.qzone.qq.com/cgi-bin/blognew/blog_quote";
		var t = document.getElementById("blogTitle").innerText.trim().toRealStr().URLencode();
		var c = document.getElementById("blogCategroy").innerText.trim().toRealStr().URLencode();
		var o = uin;
		//var m = blogContent.encode();
		var m = nContent.toRealStr().URLencode();
		var t2=(oldTypes[c])?oldTypes[c]:2
		var data = "verifycode="+vc+"&title="+t+"&content="+m+"&type="+t2+"&orguin="+o+"&fromuin="+top.g_iUin+"&hassign=1&category="+c;
		top.loadXMLAsync("quoteBlogResult",url,quoteBlogSucc,showError,true,data);
	}
	
	function quoteBlogSucc(){
		showError(top.g_XDoc["quoteBlogResult"][isIE?"text":"textContent"]);
		var err = top.g_XDoc["quoteBlogResult"].getElementsByTagName("error")[0];
		if (err && err.getAttribute("type")=="login"){
				hideOpratingMsg();
				frames['toolbarFrame'].checkLogin();
		}
		closeQuote();
	}
	
	/*----------------------------  生成验证码 ------------------------------*/
	function changeVerifyCode(id,timeout){
		timeout=(!timeout)?1000:timeout;
		if(event && event.srcElement == document.getElementById(id) && event.type!="error") 
			document.getElementById(id).src = "http://ptlogin2.qq.com/getimage?aid=8000103&sds="+Math.random();
		else 
			setTimeout('document.getElementById("'+id+'").src = "http://ptlogin2.qq.com/getimage?aid=8000103&sds="+Math.random()',timeout);
		document.diary_form.verifycode.value = "";
	}
	
	//添加回复
	function checkSubmitComment(){
		if(top.g_iLoginUin < 10000){
			setTimeout(commentLogin,0);
			return false;
		}
		with(document.diary_form){
			var regstr = /\[img\](.[^\]]*)\[\/img\]/ig;
			var imgCount=0;
		    while ((arr = regstr.exec(content.value)) != null) imgCount++;
		    if (imgCount>1) {alert("只能插入一张图片");content.focus();return false;}
			if(!/\S+/.test(content.value)){
				alert("请输入评论内容");
				content.value="";
				content.focus();
				return false;
			}

			if(verifycode.value==""){
				alert("请输入验证码");
				verifycode.select();
				return false;
			}
			
			if(content.value.length>10000){
				alert("很抱歉，您输入的内容字数过多，请删减后再重新提交");
				content.focus();
				return false;
			}
	
			if(content.value.Text2HTML().length>10000){
				alert("很抱歉，您输入的内容字数过多，请删减后再重新提交");
				content.focus();
				return false;
			}
			var r = /\[ffg,([a-zA-z#0-9]{1,10}),([a-zA-z&#=;0-9]{1,10})\]([^\[]{31,})\[\/ft\]/
			if(r.test(content.value))
				if(!confirm("您设置发光的文字已超过30个，发光字效果将可能失效，确认发表此评论吗？")) return false;
			r = /\[ffg,([a-zA-z#0-9]{1,10}),[a-zA-z&#=;0-9]{1,10}\]/g;
			if(content.value.match(r) && content.value.match(r).length>1)
				if(!confirm("发光字回复中效果只能使用一次，更多的发光字将无法显示，确认发表此评论吗？")) return false;
			var r = /\[ffg,([a-zA-z#0-9]{1,10}),([a-zA-z&#=;0-9]{1,10})\](.*\[f.*)\[\/ft\]/
			if(r.test(content.value))
				if(!confirm("若发光字中使用了其它特效，发光字效果将可能失效，确认发表此评论吗？")) return false;
	
			var _c = content.value.encode();
	
			top.cacheForAnonComment={
				blogid:currentBlogId,
				content:_c,
				sf:commentSucc,
				ef:showError
			}//为了匿名评论做准备
	
				var url = "http://b.qzone.qq.com/cgi-bin/blognew/blog_add_comment";
				var data = "blogid="+currentBlogId+
				"&uin="+uin+
				"&verifycode="+verifycode.value+
				"&content="+_c+
				"&cb_autograph="+ (hassign.checked?1:0) +
				"&property="+top.g_Property;
		}
		showOpratingMsg("正在发表评论.请稍候...");
		top.loadXMLAsync("commentResult",url,commentSucc,showError,true,data);
		return false;
	}

	/*
	function commentSucc(){
		var x = top.g_XDoc["commentResult"];
		if(x.getElementsByTagName("succ").length>0){
			    showOpratingMsg(x.getElementsByTagName("succ")[0].text);
			    document.diary_form.content.value="";
				location = "http://user.qzone.qq.com/" + g_iUin + "/blog/" + currentBlogId;
		}else{
			var err = x.getElementsByTagName("error")[0];
			if(err.getAttribute("type")=="login"){
				hideOpratingMsg();
				commentLogin();
			}else{
				showError(err.text);
				showVerifyCode();
				//changeVerifyCode("verifycodeImg_comment");
			}
		}
	}
	*/
	
	function commentLogin(){
		var _c=document.diary_form.content.value.encode();
	
		top.cacheForAnonComment={
			blogid:currentBlogId,
			content:_c,
			sf:commentSucc,
			ef:showError
		}//为了匿名评论做准备
		top.frames["toolbarFrame"].startMsg("qzoneLogin","http://imgcache.qq.com/qzone/client/blog/loginsucc.htm",null,null,null,true);
	}
	
	
	function changeSkin(itemno,type){// 更换皮肤背景
		var miniExt = 1?"jpg":"gif"; //1位标识小屋主背景的扩展名 (0-gif/1-jpg)
		var fullTopExt = 1?"jpg":"gif"; //2位标识滚屏主背景的扩展名 (0-gif/1-jpg)
		var fullBGExt = 1?"jpg":"gif"; //4位标识在全屏模式下页面底部的延伸背景(0-gif/1-jpg)
		var miniable = 0; //8位标识是否支持小屋(0-不支持/1支持)
		var fullable = 1; //16位标识是否支持滚屏(0-不支持/1-支持)
		var isFixBG = 0; //32位标识滚屏模式下背景是否跟随页面滚动 (0-不跟随/1-跟随)
		var isFullmode = 1; //64位标识使用小窝模式或者全屏模式 (0-小窝/1-全屏)
		var bgImg1,bgImg2;
		window._bgImg = {};
		window._bgImg["top"] = new Image();
		window._bgImg["bottom"] = new Image();
	
		if (!miniable && fullable) isFullmode = true;
		
		var itemdir = itemno%16;
		$("tall").style.backgroundImage = "none";
		document.body.style.backgroundImage = "none";
		$("mbody").style.backgroundAttachment = (isFixBG)?'fixed':'scroll';
	
		with(isFixBG?$("mbody"):$("tall")){
			style.backgroundRepeat = 'no-repeat'; 
			if(itemno == 1 && !isFullmode) //默认背景
				bgImg1 = 'url(qzone/images/1/bg.gif)';
			else//自定义背景
				bgImg1 = 'qzone/space_item/orig/' + itemdir + '/' + itemno + (isFullmode?"_top":"") +'.' + (isFullmode?fullTopExt:miniExt);
	
			window._bgImg["top"].onload = function(){
				window._bgImg["top"].onload = null;
				style.backgroundImage = 'url(' + bgImg1 + ')';
				window._setBG = false;
			}
		}
	
		with(isFixBG?document.body:$("mbody")){
			bgImg2 = "qzone/" + ((isFullmode)?("space_item/orig/" + itemdir + "/" + itemno  + "_bg." + fullBGExt):("images/"+top.g_StyleID+"/bodybg.gif"));
			style.backgroundImage = 'url(' + bgImg2 + ')';
	
			window._bgImg["bottom"].onload = function(){
				window._bgImg["bottom"].onload = null;
				style.backgroundImage = 'url(' + bgImg2 + ')';
			}
		}
		
		window._bgImg["top"].src = bgImg1;
		window._bgImg["bottom"].src = bgImg2;
		//$("mbody").style.display = "none";
	}
	
	function space_changeTitle(itemno,flag){
		var menuTop = $("menu_top");
		var itemdir = itemno%16;
		menuTop.style.backgroundImage = (itemno>100?"url(qzone/space_item/orig/" + itemdir + "/" + itemno + (flag?".jpg)":".gif)"):"url(qzone_v4/" + top.g_StyleID + "/bg_title_topb.gif)");
	}
	
	
	function custom_menu_swf(idx,frameName,url){
			switch(parseInt(idx))//SDS_begin
			{
				case 1:location = 'http://user.qzone.qq.com/'+g_iUin;break;
				case 2:location = 'http://user.qzone.qq.com/'+g_iUin+'/blog';break;
				case 3:location = 'http://user.qzone.qq.com/'+g_iUin+'/music';break;
				case 4:location = 'http://user.qzone.qq.com/'+g_iUin+'/msgboard';break;
				case 5:location = 'http://user.qzone.qq.com/'+g_iUin+'/photo';break;
				case 6:location = 'http://user.qzone.qq.com/'+g_iUin+'/home';break;
				case 7:location = 'http://user.qzone.qq.com/'+g_iUin+'/admin';break;
				case 8:location = 'http://user.qzone.qq.com/'+g_iUin+'/interact';break;
				default:break;
			}
			top.sdsFlashSW=true;//SDS_end
	}
	
	function blogCountCallBack(){
		var x = g_JData['blogCommentCount'];
		if(!x)return;
		if(x.error){/*alert(x.error.msg);*/return;}
		var cCount = x.data.itemlist[0].reply;//data.item[0].comment;
		$("cc1").style.display = $("cc2").style.display = $("ccuv").style.display = "";
	 	$("blogCC1").innerHTML = $("blogCC2").innerHTML = $("blogCC3").innerHTML = cCount;
	 	$("blogUV").innerHTML = x.data.itemlist[0].read;//data.item[0].read;
		cCount = parseInt(cCount);
		if(isNaN(cCount)){cCount = 0;}
		if(cCount > 15){
			$('viewMore').style.display = $('viewMore1').style.display = '';
		}
	}
	
	function dealCount(){
		var url = 'http://nb.qzone.qq.com/cgi-bin/blognew/blog_get_countlist?type=0&uin=' + uin + '&blogids=' + currentBlogId;
		top.loadJsonData('blogCommentCount',url,blogCountCallBack,function(){alert('系统繁忙!');},true,'GB2312',"_Callback");
	}
	
	window.cancelSendPV = true;
	function sendPV(action){
		if(typeof(pgvMain)=="function"){//统计读日志pv
			pvCurDomain="blog.qq.com";
			pvCurUrl="/"+action;
			pvRepeatCount=1;
			window.setTimeout(pgvMain,500);
			window.setTimeout(sendStarPV,1000);
			window.setTimeout(sendReadBlogPV,1500);
		}
	}
	
	function sendStarPV(){
		if(typeof(pgvMain)=="function"){//统计读日志pv
			pvCurDomain="star.qzone.qq.com";
			pvCurUrl="/" + g_iUin + "/";
			pvRepeatCount=1;
			pgvMain();
		}
	}
	
	function sendReadBlogPV(){
		if(typeof(pgvMain)=="function"){//统计读日志pv
			pvCurDomain="star.qzone.qq.com";
			pvCurUrl="/readblog";
			pvRepeatCount=1;
			pgvMain();
		}
	}
	
	function QZonePGVDataCallBack(){}
	function QZonePGVDataCallBack1(){}
	function BlogPGVDataCallBack(){}
	
	function loginCallback(){
		top.frames["toolbarFrame"].location.reload();
		showVerifyCode();
		//changeVerifyCode("verifycodeImg_comment");
	}
	
	//设置日志字体大小
	function setFontSize(iTag){
		//判断当前字体
		BIG_FONT_SIZE = '16px';
		SMALL_FONT_SIZE = '';
		var blnSave = false;
		var eBtn = document.getElementById('fontButton');
		if(iTag == undefined){
			iTag = loadUserData('FontSize','pSize');
			if(!iTag){
				iTag = 'l';
			}
		}
		var oDiv = document.getElementById('bContent');
		
		if(iTag == 'l'){
			eBtn.title = '切换小字体';
			eBtn.innerHTML = '小字体';
			//eBtn.src = '/qzone/images/client/nv_show2.gif';
			eBtn.setAttribute('pSize','s');
			eBtn.onclick = function(){
				setFontSize('s');
			}
			oDiv.style.fontSize = BIG_FONT_SIZE;
			blnSave = true;
			oDiv.style.lineHeight = '25px';
		}else{
			eBtn.title = '切换大字体';
			eBtn.innerHTML = '大字体';
			//eBtn.src = '/qzone/images/client/nv_show.gif';
			eBtn.setAttribute('pSize','l');
			eBtn.onclick = function(){
				setFontSize('l');
			}
			oDiv.style.fontSize = SMALL_FONT_SIZE;
			oDiv.style.lineHeight = '';
			blnSave = true;
		}
		if(blnSave){
			saveUserData('FontSize','pSize',iTag);
		}
	}
	
	//引用新的JS脚本
	function includeJS(src,loadFunction){
		var s = document.createElement("script");
		if (loadFunction){ 	
			if (Browser.isIE) {
				s.onreadystatechange = function(){
					if (s.readyState == "complete" || s.readyState == "loaded") {
						s.onreadystatechange = null;
						loadFunction();
					}
				};
			}else{
				s.onload = loadFunction;
			}
		}
		s.src = src;
		document.getElementsByTagName("head")[0].appendChild(s);
		return s;
	}

	var layerIdx = 0;
	
	function commentCallback(){ //填充评论
		var data = window.Blog_Static_Comment;
		var strTemplet = $('commentDIV').innerHTML;
		var arrComment = [];
		$('commentDIV').innerHTML = '';
		layerIdx = 0;
		for(var i=0;i<data.comment.length-1;i++){
			//匹配模版并处理
			arrComment.push(strTemplet.replace(/\$\{([\w]+)\}/g,function(k0,k1){return getReplace(k1)}));
			layerIdx++;
		}
		$('commentDIV').innerHTML = arrComment.join('');
		$('commentDIV').style.display = '';
		if(layerIdx){
			$('divCmtNav').style.display = '';
		}
	}
	
	function getReplace(key){
		data = window.Blog_Static_Comment.comment[layerIdx];
		var retValue;
		switch(key){
			case 'layer':
				retValue = (layerIdx+1);
				break;
			case 'nick':
				if(data.commentuin != 0){
					retValue = '<a class="username" href="http://user.qzone.qq.com/' + data.commentuin + '" target=_blank>' + data.commentnick.trim().replace(/&#32;/g,"&nbsp;") + '</a>';
				}else{
					retValue = '<a class="username">' + data.commentnick + '</a>';
				}
				break;
			case 'pubdate':
				retValue = getFullDateTime(new Date(data.pubdate.replace(/-/g,"/")));
				break;
			case 'content':
				retValue = data.commentcontent;
				var tmp = retValue.match(/\n/g);
				if (tmp && tmp.length > 5){
					retValue = retValue.replace(/\n/g,' ');
				}
				retValue = ubbReplace(retValue,"face quote imageHide");
				break;
			case 'autograph':
				retValue = ubbReplace(data.commentautograph,'face anchor image email sign glow_limit font');
				break;
			default:
				retValue = '';
			
		}
		return retValue;
		
		function getFullDateTime(d){
			return d.getFullYear()+"年"+(d.getMonth()>8?"":"0")+(d.getMonth()+1)+"月"+(d.getDate()>9?"":"0")+d.getDate()+"日 "+
				((d.getHours()<10)?"0"+d.getHours():d.getHours())+"时"+ ((d.getMinutes()<10)?"0"+d.getMinutes():d.getMinutes())+"分";
		}
	}

	function showVerifyCode(){
		if(top.g_iLoginUin < 10000){
			$('verifySpan').display = 'none';
		}else{
			changeVerifyCode("verifycodeImg_comment");
			$('verifySpan').style.display = '';
		}
	}
	
	function sendRefer(){
		var sRefer = document.referrer;
		if(/^http:\/\/(www.)?(google|baidu).(com|cn)\//i.test(sRefer)){
			window.imgRefer = new Image();
			window.imgRefer.src = 'http://post.blog.qq.com/post/add_refer.php?qq=' + uin + '&id=' + currentBlogId + '&refer=' + escape(sRefer);
		}
	}

//验证码相关
var verifyCounter=[];

function VerifyImage(eleID,x,y){
	var c=verifyCounter.length;
	this.timeout=null;
	this.ipt = null;
	if(!eleID)
		this.eleID=eleID="verifyIMG_"+c;
	this.od=document.createElement("div");
	with(this.od){
		style.position="absolute";
		style.backgroundColor="#505050";
		style.border="solid 1px #545454";
		style.padding="3px";
		id=eleID+"_anchor";
	}
	this.templete=('<img id="'+eleID+'" onclick="getVerify(\''+eleID+'\','+c+')" style="cursor:pointer;margin-bottom:5px" src="http://ptlogin2.qq.com/getimage?aid=8000103&sds={{_random_}}" alt="获取中..." title="点击更换" width="130" height="53" /><br /><span onclick="getVerify(\''+eleID+'\','+c+')" style="color:white;cursor:pointer">看不清楚?换一个</span>');
	this.seed=Math.random();
	this.appended=false;

	verifyCounter[c]=this;
}

VerifyImage.prototype.showMe=function(x,y){
	with(this.od){
	//	offsetTop = (y-85);
		//offsetLeft = (x+1);
		style.top=(y-85)+"px";
		style.left=(x+1)+"px";
	}
	if(!this.appended){
		document.getElementById('mbody').appendChild(this.od);
	}
	this.od.innerHTML=this.templete.replace(/\{\{_random_\}\}/g,this.seed);
	this.od.style.display="";
}

VerifyImage.prototype.killMe=function(){
	this.od.style.display="none";
}

VerifyImage.prototype.change=function(){
	var o=$(this.eleID);
	this.seed=Math.random();
	o.src="http://ptlogin2.qq.com/getimage?aid=8000103&sds="+this.seed;
}

//获取验证码
function getVerify(s,n){
	clearTimeout(verifyCounter[n].timeout);
	verifyCounter[n].change();
	//var i=$("verifycode");
	if(verifyCounter[n].ipt){
		verifyCounter[n].ipt.value="";
		verifyCounter[n].ipt.focus();
	}
}

//input focus获取验证码
function focusGetVerify(o){
	var pos=getPosition(o);
	var e=(verifyCounter.length>0)?(verifyCounter[0]):(new VerifyImage());
	e.ipt = o;
	e.showMe(pos.left,pos.top);
	o.value="";
}

//干掉验证码
function hiddenVerify(isImme){
	if(!verifyCounter[0])
		return;
	if(!isImme)
		verifyCounter[0].timeout=setTimeout(function(){verifyCounter[0].killMe()},500);
	else
		verifyCounter[0].killMe();
}

//获取element的位置
function getPosition(obj) {
	var top=0;
	var left=0;
	var width=obj.offsetWidth;
	var height=obj.offsetHeight;
	while (obj.offsetParent) {
			top += obj.offsetTop;
			left += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	return {"top":top,"left":left,"width":width,"height":height};
}