﻿//********************************************************************************
//** 插件功能:		文章投票插件
//** 插件应用:		Z-Blog(修改getID函数后可用在其它系统上)
//** 插件制作:		软件兔 (http://blog.wyshen.com/ SoftBunny+Soft@Gmail.com)
//** 备　　注:	
//**
//** 变更记录:	
//** 2007-05-18	基线版本
//********************************************************************************
var xmlhttp
var ID=getID();
var Path=getPath();
var Score=0;
var UserScore=0;
var Rnumber=0;

sndReqShowRating();

function getID(){
	var str = document.location.pathname;
	return str.substring(str.lastIndexOf("/")+1,str.lastIndexOf("."));
}

function getPath(){
	var strtmp;
	var scripts=document.getElementsByTagName("script");
	for (i=scripts.length-1; i>0; i--)
		if (scripts[i].src != "")
			if(scripts[i].src.toLowerCase().indexOf("rating.js")>0)
				strtmp = scripts[i].src.toLowerCase().replace("rating.js","");
	if (strtmp!="")
		if (strtmp.substr(0,4).toLowerCase() != "http")
			strtmp=document.location.protocol + "//" + document.location.host + strtmp;
	return strtmp;
}

function myXMLHttpRequest() {
	var xmlhttplocal;
   
	var msxmlhttp = new Array(
	'Msxml2.XMLHTTP.5.0',
	'Msxml2.XMLHTTP.4.0',
	'Msxml2.XMLHTTP.3.0',
	'Msxml2.XMLHTTP',
	'Microsoft.XMLHTTP');
	for (var i = 0; i < msxmlhttp.length; i++) {
		try {
			xmlhttplocal = new ActiveXObject(msxmlhttp[i]);
		} catch (e) {
			xmlhttplocal = null;
		}
	}
	
	if(!xmlhttplocal && typeof XMLHttpRequest != "undefined")
		xmlhttplocal = new XMLHttpRequest();
	if (!xmlhttplocal)
		alert("Could not create connection object.");
	return xmlhttplocal;
}

function sndReqShowRating()
{
	xmlhttp=myXMLHttpRequest();
    xmlhttp.open('get', Path + 'Score' + ID + '.txt?rnd=' + Math.random());
    xmlhttp.onreadystatechange = handleShowRating;
    xmlhttp.send(null);	
}

function handleShowRating() {
	if(xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200){
			var Rdata = xmlhttp.responseText.split(",");
			Score = new Number(Rdata[0]);
			Rnumber =  Rdata[1];
		}
		else if (xmlhttp.status == 404)
		{
			Score = 0;
			Rnumber = 0;
		}
		else
		{
			Score = 0;
			Rnumber = 0;
		}
		var strtmp="";
		document.getElementById("RatingPre").innerHTML = "<div id=\"Rating\"><div id=\"Star\"><div id=\"Star-Cur\"><\/div><a href=\"JavaScript:Rating(" + ID + ",1)\" id=\"Star1\" title=\"这么差的文章，怎么写出来的！\"><\/a><a href=\"JavaScript:Rating(" + ID + ",2)\" id=\"Star2\" title=\"马马虎虎，写得不怎么样\"><\/a><a href=\"JavaScript:Rating(" + ID + ",3)\" id=\"Star3\" title=\"一般情况吧\"><\/a><a href=\"JavaScript:Rating(" + ID + ",4)\" id=\"Star4\" title=\"写得不错，再努力呀！\"><\/a><a href=\"JavaScript:Rating(" + ID + ",5)\" id=\"Star5\" title=\"精彩，太值得读了！\"><\/a><\/div><div id=\"Score\"><\/div><\/div>";
		if (Score==0)
		{
			document.getElementById("Star-Cur").style.width = "0px";
			document.getElementById("Score").innerHTML = "<div id=\"Rating-no\">当前尚无评分记录</div>";
		}
		else
		{
			document.getElementById("Star-Cur").style.width = Score * 30 + "px";
			document.getElementById("Score").innerHTML = "<div id=\"Rating-show\">平均得分" + Score.toFixed(2) + "分（共" + Rnumber + "次打分）</div>";
		}
	}
}

function Rating(intID,intScore)
{
	document.getElementById("Score").innerHTML = "<div id=\"Rating-now\">正在投票，请稍候……</div>";
	UserScore=intScore;
	xmlhttp=myXMLHttpRequest();
    xmlhttp.open('get', Path + 'Rating.asp?ID=' + intID + '&S=' + intScore);
    xmlhttp.onreadystatechange = handleRating;
    xmlhttp.send(null);	
}

function handleRating() {
	if(xmlhttp.readyState == 4)
		if (xmlhttp.status == 200){
			var Rdata = xmlhttp.responseText.toLowerCase();
			if (Rdata == "ok")
			{
				Score = (Score*Rnumber+parseInt(UserScore))/(parseInt(Rnumber)+1);
				Score = Score.toFixed(2);
				Rnumber =  parseInt(Rnumber)+1;
				document.getElementById("Star-Cur").style.width = Score * 30 + "px";
				//document.getElementById("Score").innerHTML = "<div id=\"Rating-show\">平均得分" + Score + "分（共" + Rnumber + "次打分）</div>";
				document.getElementById("Score").innerHTML = "<div id=\"Rating-Tanks\">投票成功,感谢您的投票</div>";
			}
			else if (Rdata=="already")
			{
				document.getElementById("Score").innerHTML = "<div id=\"Rating-already\">您已经投过一票了</div>";
			}
			else if (Rdata=="error")
			{
				document.getElementById("Score").innerHTML = "<div id=\"Rating-err\">遇到未知错误，请向作者反映</div>";
			}
			else
			{
				document.getElementById("Score").innerHTML = "<div id=\"Rating-err\">遇到未知错误，请向作者反映</div>";
			}
		}
}

//document.write ("&nbsp;&nbsp;&nbsp;&nbsp;<img src='/PLUGIN/Rating/email.gif' border='0' align='absbottom' />&nbsp;&nbsp;<a href='http://www.sendmerss.com/profiles.aspx/www.williamlong.info/sydication.asp?cmt=" + ID + "' target='_blank'>将这篇文章的最新评论发送到我的信箱</a><br/>");

/*
document.body.oncopy=function(){
event.returnValue=false;
var t=document.selection.createRange().text;
var s="本文来源于月光博客 http://www.williamlong.info , 原文地址：http://www.williamlong.info/archives/" + ID + ".html";
clipboardData.setData('Text',t+'\r\n'+s+'\r\n');
}
*/

document.writeln("<div id=\"socialbookmark\"><br\/><a href=\"javascript:void(0)\" id=\"fxwb\" style=\"background:url(http:\/\/i2.sinaimg.cn\/IT\/deco\/2009\/0930\/images\/wbicon_cl_002.png) no-repeat;width:105px;padding:10px 0 0 20px;height:20px;text-align:right; line-height:normal;color:#000000;text-decoration:none;font-weight:normal \"><span style=\"font-size:12px;\">转发至新浪微博<\/span><\/a>");

document.writeln("<a href=\"javascript:window.open(\'http:\/\/www.kaixin001.com\/repaste\/share.php?rtitle=\'+encodeURIComponent(document.title.substring(0,76))+\'&rurl=\'+encodeURIComponent(location.href)+\'&rcontent=\',\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\'); void 0\" style=\"color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><SPAN style=\"PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 10px; CURSOR: pointer; PADDING-TOP: 5px\"><IMG alt=转帖到开心网 src=\"http:\/\/img1.kaixin001.com.cn\/i\/favicon.ico\" align=absMiddle border=0>&nbsp;转帖到开心网<\/SPAN><\/a>");

document.writeln("<a href=\"javascript:window.open(\'http://share.renren.com/share/buttonshare.do?title=\'+encodeURIComponent(document.title.substring(0,76))+\'&link=\'+encodeURIComponent(location.href)+\'&content=\',\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\'); void 0\" style=\"color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><SPAN style=\"PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 10px; CURSOR: pointer; PADDING-TOP: 5px\"><IMG alt=转帖到人人网 src=\"http:\/\/s.xnimg.cn\/favicon-rr.ico\" align=absMiddle border=0>&nbsp转帖到人人网<\/SPAN><\/a><\/div>");

 var sendT = {
	getHeader : function(){
		var g_title=document.getElementsByTagName("h2")[1].innerHTML;
		var re = /<[^<>]*?font[^<>]*?>/gi;
		g_title = g_title.replace(re,"");
		return g_title;
	},
					getFirstImgSrc : function(){
						if (document.getElementById("artibody").getElementsByTagName("img")[0] && document.getElementById("artibody").getElementsByTagName("img")[0].width > 100)
						{
							return document.getElementById("artibody").getElementsByTagName("img")[0].src;
						}
						else
						{
							return null;
						}

					}

}

document.getElementById("share").innerHTML = document.getElementById("socialbookmark").innerHTML;

document.getElementById("socialbookmark").innerHTML="";

document.getElementById("fxwb").onclick=function(){
 (function(s,d,e,r,l,p,t,z,c){
	 var f='http://v.t.sina.com.cn/share/share.php?',u=z||d.location,p=['url=',e(u),'&title=',e(sendT.getHeader()),'&source=',e(r),'&sourceUrl=',e(l),'&content=',c||'gb2312','&pic=',e(p||'')].join('');
	 function a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');
	 };
	 if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();
})(screen,document,encodeURIComponent,'月光博客','http://www.williamlong.info/',sendT.getFirstImgSrc(),null,null,null);
}

