﻿//********************************************************************************
//** 插件功能:		文章投票插件
//** 插件应用:		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.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\"><a href=\"javascript:void((function(s,d,e){try{}catch(e){}var f='http:\/\/service.weibo.com\/share\/share.php?',u=d.location.href,p=['url=',e(u),'&title=',e(d.title),'&appkey=2283369843&ralateUid=1494759712'].join('');function a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=620,height=450,left=',(s.width-620)\/2,',top=',(s.height-450)\/2].join('')))u.href=[f,p].join('');};if(\/Firefox\/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}})(screen,document,encodeURIComponent));\" 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(&#39;http:\/\/sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=&#39;+encodeURIComponent(location.href));" style="background:url(http:\/\/qzonestyle.gtimg.cn/qzone_v6\/proj_connect_open\/sprite\/app_connect_open_intro.png) no-repeat; background-position: -316px -34px; padding:5px 0 0 19px;height:19px;text-align:right; line-height:normal; color:#000000;text-decoration:none;font-size:12px;font-weight:normal"><span style="FONT-SIZE: 12px; CURSOR: pointer">转帖到QQ空间</span></a>');

document.writeln("<a href=\"javascript:void(0)\" onclick=\"posttoWb();\" close=\"tmblog\" style=\"background:url(http:\/\/v.t.qq.com\/share\/images\/ico.png) no-repeat;padding:5px 0 0 19px;height:19px;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=\"background:url(http:\/\/img1.kaixin001.com.cn\/i\/favicon.ico\) no-repeat; padding:5px 0 0 19px;height:19px;text-align:right; line-height:normal; color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><span style=\"FONT-SIZE: 12px; CURSOR: pointer\">转帖到开心网<\/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=\"background:url(http:\/\/a.xnimg.cn\/favicon.ico\) no-repeat; padding:5px 0 0 19px;height:19px;text-align:right; line-height:normal; color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><span style=\"FONT-SIZE: 12px; CURSOR: pointer\">转帖到人人网<\/span><\/a>");

document.writeln(" &nbsp; <\/div>");

//document.writeln("<center><a href='http://go3.myad.cn/?CV1Mzn6XX/s7GX1UyUgSNNwDSeFqx7Q6atcvCcN5ufo=' target=_blank><img src='http://img4.myad.cn/2010/sina/728-90.gif' width='728'  border=0 height='90'></a><div style='position:relative left:-1px; top:-1px;'><img src='http://i.myad.cn/?CV1Mzn6XX/s7GX1UyUgSNNwDSeFqx7Q6atcvCcN5ufo=' height=1 width=1></div><center\/>");

 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://service.weibo.com/share/share.php?appkey=2283369843&',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);
}

function posttoWb(){
		var _t = encodeURI(document.title);
		var _url = encodeURI(window.location);
		var _source = 1000002;
		var _site = encodeURI('http://www.williamlong.info');
		var _pic = '';
		var _u = 'http://v.t.qq.com/share/share.php?appkey=26d63160d45846cc9f9608d19ff0a315&title='+_t+'&url='+_url+'&source='+_source+'&site='+_site+'&pic='+_pic;
		window.open( _u,'转播到腾讯微博', 'width=700, height=480, top=320, left=180, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' );
}



