var N = 8;
var pid, key, url, expara, num;
var moodObject, cookieKey, moodKey;
var moodV = new Array();
var up,down,other1,other2,other3,other4,other5,other6,other7,other8,other9,other10;
var articalId = document.getElementsByName("nsid")[0].value;
var ccId = document.getElementsByName("rootid")[0].value;
var articleTitle = encodeURI(unescape(document.getElementsByName("title")[0].value));
var url = document.getElementsByName("puburl")[0].value;
var httpIndex = url.indexOf("http");
var articleUrl = url.substring(httpIndex);
var urlSuffix = "ccId="+ccId+"&ctArticalId="+articalId+"&ctArticalTitle="+articleTitle+"&ctArticalUrl="+articleUrl;


//Cookie日期匹配
function fixCookieDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) date.setTime(date.getTime() - skew);
}
//Cookie设置
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) + ((expires)?"; expires="+expires.toGMTString():"") + ((path)?"; path="+path:"") + ((domain)?"; domain="+domain:"") + ((secure)?"; secure":"");
}
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name) {
	var arg = name + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	var flag = '';
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i,j) == arg)
			flag = getCookieVal(j);
		i = document.cookie.indexOf(" ",i) + 1;
		if (i == 0) break;
	}
	return flag;
}
function requestMoodCounter() {
	var elem = document.getElementsByTagName("span");
	for(i=0; i<elem.length; i++) {
		var att = elem[i].getAttribute("name");
		if (att == "moodcounter") {
			moodObject = elem[i];

			// 判断浏览器
			if ( navigator.userAgent.toLowerCase().indexOf('msie') >= 0 ) {
				document.createStyleSheet(cssUrl);
			} else {
				var style = document.createElement('link');
				style.type = 'text/css';
				style.rel = 'stylesheet';
				style.href = cssUrl;
				document.body.insertBefore(style, null);
			}
			moodObject.innerHTML = counterPage;
			moodObject.style.display = "";
			break;
		}
	}
}
//页面项选择触发
function moodClick(moodradio) {
	var moodValue = "";
	for (var i=0; i<moodradio.length; i++) {
		if (moodradio[i].checked == true) {
			moodValue = moodradio[i].value;
			break;
		}
	}
	if (moodValue == "") return false;
	var str = "http://218.1.65.172:8091/addCommentVoteAction.action?flag=" + moodValue + "&" +urlSuffix + "&callback=?";
		$.ajax({
             url:str,
             dataType:"jsonp",
             jsonp:"callback",
			 cache:'false',
			 timeout:'5000',
             success:function(data){
             	
             	 up = data[0].up;
             	 down = data[0].down;
             	 other1 = data[0].other1;
             	 other2 = data[0].other2;
             	 other3 = data[0].other3;
             	 other4 = data[0].other4;
             	 other5 = data[0].other5;
             	 other6 = data[0].other6;
             	 other7 = data[0].other7;
             	 other8 = data[0].other8;
             	 other9 = data[0].other9;
             	 other10 = data[0].other10;
             
             	 Dispaly();
             }
        });
	return false;
}

 function Dispaly() //接受服务端返回的数据，对其进行显示
{
	
	var moodTotal = 0;
	var maxIndex = -1, tmp = 0;

	if (up > tmp) {
		tmp =up;
		maxIndex = 0;
	}	

	if (down-tmp>0) {

		tmp =down;
		maxIndex = 1;
	}
	if (other1-tmp>0) {
		tmp =other1;
		maxIndex = 2;
	}
	if (other2 -tmp>0) {
		tmp =other2;
		maxIndex = 3;
	}
	if (other3-tmp>0) {
		tmp =other3;
		maxIndex = 4;
	}
	if (other4-tmp>0) {
		tmp =other4;
		maxIndex = 5;
	}
	if (other5 -tmp>0) {
		tmp =other5;
		maxIndex = 6;
	}
	if (other6-tmp>0) {
		tmp =other6;
		maxIndex = 7;
	}
	if (other7-tmp>0) {
		tmp =other7;
		maxIndex = 8;
	}
	moodTotal = parseInt(up) + parseInt(down) + parseInt(other1) + parseInt(other2) + parseInt(other3) + parseInt(other4) + parseInt(other5) + parseInt(other6) + parseInt(other7) ;
    //替换结果值
	var realResult = resultPage;
	
	var reg1 = new RegExp("_MOOD" + 0 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(up*maxColumnHeight/moodTotal));
	var reg2 = new RegExp("_MOOD" + 0 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 0)?redPicUrl:greenPicUrl);
	var reg3 = new RegExp("_MOOD" + 0 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(up));
        
   	reg1 = new RegExp("_MOOD" + 1 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(down*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 1 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 1)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 1 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(down));
        
    reg1 = new RegExp("_MOOD" + 2 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(other1*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 2 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 2)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 2 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(other1));
        
    reg1 = new RegExp("_MOOD" + 3 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(other2*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 3 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 3)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 3 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(other2));
        
    reg1 = new RegExp("_MOOD" + 4 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(other3*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 4 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 4)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 4 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(other3));

    reg1 = new RegExp("_MOOD" + 5 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(other4*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 5 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 5)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 5 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(other4));

    reg1 = new RegExp("_MOOD" + 6 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(other5*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 6 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 6)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 6 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(other5));

    reg1 = new RegExp("_MOOD" + 7 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(other6*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 7 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 7)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 7 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(other6));
        
    reg1 = new RegExp("_MOOD" + 8 + "_", "g");
	realResult = realResult.replace(reg1, (moodTotal == 0)?0:parseInt(other7*maxColumnHeight/moodTotal));
	reg2 = new RegExp("_MOOD" + 8 + "PIC_", "g");
	realResult = realResult.replace(reg2, (maxIndex == 8)?redPicUrl:greenPicUrl);
	reg3 = new RegExp("_MOOD" + 8 + "RATE_", "g");
		//realResult = realResult.replace(reg3, (moodTotal == 0)?0:parseInt(moodV[i]*100/moodTotal));
        realResult = realResult.replace(reg3, parseInt(other7));

	var reg = new RegExp("_MOODTOTAL_", "g");
	realResult = realResult.replace(reg, moodTotal);

	moodObject.innerHTML = realResult;
	moodObject.style.display = "";

}

requestMoodCounter();
