var star_ov_variables=new Array();
var star_last_value=new Array();
for(var i=0;i<4;i++) {
	star_ov_variables[i] = 0;
	star_last_value[i] = 0 ;  // 每个项目评分初始值,
}

var no_data_js = false;
var usrname = getCookie('usrname');
var sid = getCookie('sessionid');
if (usrname == "") {
	var xl_cookie_name_rating = "star_rating_"+peopleid;
	//usrname = "guest";
	//alert("你的登录时间已过期，请重新登录");
} else {
	var xl_cookie_name_rating = "star_rating_"+peopleid+"_"+usrname;
	var xl_cookie_name_tag = "star_tag_"+peopleid+"_"+usrname;
	var xl_cookie_name_collect = "star_collect_"+peopleid+"_"+usrname;
}

var score_0_total = 0;
var score_0_yx = 0;
var score_0_ml = 0;
var score_0_sl = 0;

var my_total = 0;
var my_yx = 0;
var my_ml = 0;
var my_sl = 0;

var score_count = 0;
var tag_ary = new Array();
var rating_ary = new Array();

function star_set_default () {
	//for(var i=0;i<star_ov_variables.length;i++)
	for(var i=0;i<1;i++)
	star_ov_variables[i]=1;
}

function star_switch_ck(itemType,score) {
	if (star_ov_variables[itemType]==0){
		star_ov_variables[itemType]=1;
		star_switch_ov(itemType,score);
	} else if (star_ov_variables[itemType]==1) {
		star_switch_ov(itemType,score);
		star_ov_variables[itemType]=0;
	}
}

function star_switch_cks(score) {
	try{
		if (usrname != "" && rating_ary[usrname]['score_yx']>0) {	
		} else if (getCookie(xl_cookie_name_rating) == "") {
			document.getElementById('ping_block').style.display='block';
			star_switch_ck(0,score);
		}
	} catch(e) {
		document.getElementById('ping_block').style.display='block';
		star_switch_ck(0,score);
	}
}

function star_switch_ov(itemType,score) {
	if(star_ov_variables[itemType]==1){
		if (itemType == 0) {
			for(var i=1;i<star_ov_variables.length;i++) {
				star_item_ov_setscore(i,score);
			}
		} else {
			star_item_ov_setscore(itemType,score);
		}
	}
}

function r_m_mouseover(a) {
	return ;
}

function star_item_ov_setscore(itemType,score) {
	
	document.getElementById("star_item_"+itemType).innerHTML = score;
	document.getElementById("star_item_"+itemType).value = score;
	star_item_set_star(itemType,score);

	var s_yx = parseInt(document.getElementById("star_item_1").innerHTML);
	var s_ml = parseInt(document.getElementById("star_item_2").innerHTML);
	var s_sl = parseInt(document.getElementById("star_item_3").innerHTML);

	var s_total=get_rating_sum(s_yx,s_ml,s_sl);
	var s_shi=parseInt(Math.floor(s_total+0.01));
	var s_ge=parseInt(Math.floor((s_total-s_shi)*10+0.01));
	var starFull=s_shi;
	var starHalf=0;

	if(s_shi==10||s_shi==0&&s_ge==0){
		document.getElementById("score_shi").innerHTML=s_shi;
		document.getElementById("score_ge").innerHTML="";
		starFull=s_shi;
	}else{
		document.getElementById("score_shi").innerHTML=s_shi;
		document.getElementById("score_ge").innerHTML="."+s_ge;
		if(s_ge>=3&&s_ge<=7) {
			starHalf=1;
		}else if(s_ge==0||s_ge==1||s_ge==2){		
			starHalf=0;
		}else if(s_ge==8||s_ge==9){
			starFull=starFull+1;
		}
	}
	star_set_star(starFull,starHalf,0);
}

function star_set_star(star,halfStar,istotal){
	var image=new Array();
	image[0]="http://images.movie.xunlei.com/movie_v2/images/star_big_1.gif";
	image[1]="http://images.movie.xunlei.com/movie_v2/images/star_big_2.gif";
	image[2]="http://images.movie.xunlei.com/movie_v2/images/star_big_3.gif";
	var starImages=new Array();for(var i=1;i<=10;i++){
		if (istotal) {
			starImages[i-1]=document.getElementById("star_item_t_"+i);
		} else {
			starImages[i-1]=document.getElementById("star_item_0_"+i);
		}
	}
	for(var n=1;n<=star;n++){
		starImages[n-1].src=image[0];
	}
	if(halfStar==1)	starImages[star].src=image[1];
	for(var n=star+halfStar+1;n<=10;n++){
		starImages[n-1].src=image[2];
	}
}


function star_item_set_star(itemType,score){
	score = parseInt(score);
	if (star_last_value[itemType] == score)
	{
		return;
	}
	var imageItem=new Array();
	imageItem[0]="http://images.movie.xunlei.com/movie_v2/images/star_small_1.gif";
	imageItem[1]="http://images.movie.xunlei.com/movie_v2/images/star_small_2.gif";
	imageItem[2]="http://images.movie.xunlei.com/movie_v2/images/star_small_3.gif";
	imageItem[3]="http://images.movie.xunlei.com/movie_v2/images/star_small_4.gif";

	var starItemImages=new Array();
	for(var j=1;j<=10;j++){
		starItemImages[j-1]=document.getElementById("star_item_"+itemType+"_"+j);
	}
	for(var n=1;n<=score;n++){
		if(n%2==0)
			starItemImages[n-1].src=imageItem[2];
		else
			starItemImages[n-1].src=imageItem[0];
	}
	for(var n=score+1;n<=10;n++){
		if(n%2==0)
			starItemImages[n-1].src=imageItem[1];
		else
			starItemImages[n-1].src=imageItem[3];
	}
	star_last_value[itemType] = score;
}



function rating_post(type) {

	var s_yx = parseInt(document.getElementById("star_item_1").innerHTML);
	var s_ml = parseInt(document.getElementById("star_item_2").innerHTML);
	var s_sl = parseInt(document.getElementById("star_item_3").innerHTML);
	if (type == 1){
		var id = movieid;
	} else if (type == 2){
		var id = peopleid;
	}
	
	var rating = s_yx+"|"+s_ml+"|"+s_sl;
	setCookie(xl_cookie_name_rating, rating, 1);

	set_rating_record(type, id, usrname, rating);
	set_tracker_record(type, id, 'rating');

	var s_total=get_rating_sum(s_yx,s_ml,s_sl);
	var my_shi=parseInt(Math.floor(s_total+0.01));
	var my_ge=parseInt(Math.floor((s_total-my_shi)*10+0.01));

	document.getElementById('ping_block').style.display='none';	

	score_0_total = get_average(score_0_total, s_total);
	score_0_yx    = get_average(score_0_yx, s_yx);
	score_0_ml    = get_average(score_0_ml, s_ml);
	score_0_sl    = get_average(score_0_sl, s_sl);

	//if (no_data_js){
		score_count++;
	//}
	//alert(score_0_total+' '+score_0_yx+' '+score_0_ml+' '+score_0_sl);
	star_set_total_rating(score_0_total, score_0_yx, score_0_ml, score_0_sl);
	star_set_my_rating(my_shi, my_ge);
	star_set_count_show();
}

function star_set_my_rating(s_shi, s_ge) {
	if (s_shi >=10)	{
		//document.getElementById("my_score").innerHTML='<span class="wo_score">我的评分<a href="javascript://void(0)" onclick="document.getElementById(\'ping_block\').style.display=\'block\'"><cite id="my_shi" class="wo_shi2">'+s_shi+'</cite></a></span>';
		document.getElementById("my_score").innerHTML='<span class="wo_score">我的评分<cite id="my_shi" class="wo_shi2">'+s_shi+'</cite></span>';
	} else {
		//document.getElementById("my_score").innerHTML='<span class="wo_score">我的评分<a href="javascript://void(0)" onclick="document.getElementById(\'ping_block\').style.display=\'block\'"><cite id="my_shi" class="wo_shi">'+s_shi+'</cite><cite id="my_ge" class="wo_ge">.'+s_ge+'</cite></a></span>';
		document.getElementById("my_score").innerHTML='<span class="wo_score">我的评分<cite id="my_shi" class="wo_shi">'+s_shi+'</cite><cite id="my_ge" class="wo_ge">.'+s_ge+'</cite></span>';
	}
}

function star_set_count_show() {
	if (score_count>0) {
		document.getElementById("rating_count_show").innerHTML = "(已有"+score_count+"人评分)";
	} else {
		document.getElementById("rating_count_show").innerHTML = "(暂无评分)";
	}
}

function star_set_total_rating(s_t, s_yx, s_ml, s_sl) {
	document.getElementById("s_yx").innerHTML=s_yx;
	document.getElementById("s_ml").innerHTML=s_ml;
	document.getElementById("s_sl").innerHTML=s_sl;
	
	if (is_int(s_t)){
		var s_shi=s_t;
		var s_ge=0;
	} else {
		var tmp_t = s_t.toString();
		var tmp=tmp_t.split(".");  
		var s_shi=parseInt(tmp[0]);
		var s_ge=parseInt(tmp[1]);
	}

	var starFull=s_shi;
	var starHalf=0;

	if(s_shi==10||s_shi==0&&s_ge==0){
	//if(s_shi==10||s_ge==0){
		if (s_shi == 10) {
			document.getElementById("total_shi").innerHTML=s_shi;
			document.getElementById("total_ge").innerHTML="";
		} else {
			document.getElementById("total_shi").innerHTML=s_shi;
			document.getElementById("total_ge").innerHTML=".0";
		}
		
		starFull=s_shi;
	}else{
		if (s_shi == 10) {
			document.getElementById("total_shi").innerHTML=s_shi;
			document.getElementById("total_ge").innerHTML="";
		} else {
			document.getElementById("total_shi").innerHTML=s_shi;
			document.getElementById("total_ge").innerHTML="."+s_ge;
		}
		if(s_ge>=3&&s_ge<=7) {
			starHalf=1;
		}else if(s_ge==0||s_ge==1||s_ge==2){		
			starHalf=0;
		}else if(s_ge==8||s_ge==9){
			starFull=starFull+1;
		}
	}
	star_set_star(starFull,starHalf,1);

}


function rating_init() {
	if (typeof(rating_count)!='undefined') {
		score_count = rating_count;
	}

	var xl_cookie = getCookie(xl_cookie_name_rating);
	if (xl_cookie == "") {
		try{
			if (usrname != "") {
				if (rating_ary[usrname]['score_yx']>0){
					my_total = rating_ary[usrname]['score_total'];
					my_yx = rating_ary[usrname]['score_yx'];
					my_ml = rating_ary[usrname]['score_ml'];
					my_sl = rating_ary[usrname]['score_sl'];
					
				}
			} else {
			}
		} catch(e) {
		}
	} else {
		var patrn=/^[0-9]{1}|[0-9]{1}|[0-9]{1}|$/;   
		if (patrn.exec(xl_cookie)) {
			var tmp=xl_cookie.split("|");  			
			my_yx = tmp[0];
			my_ml = tmp[1];
			my_sl = tmp[2];
			my_total=get_rating_sum(my_yx,my_ml,my_sl);
		}

		// 如果cookie中有而js没有
		if (usrname != "") {
			try{
				if (rating_ary[usrname]['score_total']>=0){
					no_data_js = false;
				}
			} catch(e) {
				no_data_js = true;
			}
		} else {
			no_data_js = true;
		}
	}
	try{
		if (rating_ary[0]['score_yx']>0){
			score_0_total = rating_ary[0]['score_total'];
			score_0_yx = rating_ary[0]['score_yx'];
			score_0_ml = rating_ary[0]['score_ml'];
			score_0_sl = rating_ary[0]['score_sl'];

			if (no_data_js)	{
				score_0_total = get_average(score_0_total, my_total);
				score_0_yx    = get_average(score_0_yx, my_yx);
				score_0_ml    = get_average(score_0_ml, my_ml);
				score_0_sl    = get_average(score_0_sl, my_sl);
				score_count++;
			}
			star_set_total_rating(score_0_total, score_0_yx, score_0_ml, score_0_sl);
		}
	} catch(e) {
		if (no_data_js)	{
			score_0_total = get_average(score_0_total, my_total);
			score_0_yx    = get_average(score_0_yx, my_yx);
			score_0_ml    = get_average(score_0_ml, my_ml);
			score_0_sl    = get_average(score_0_sl, my_sl);
			score_count++;
		}
		star_set_total_rating(score_0_total, score_0_yx, score_0_ml, score_0_sl);
	}

	if (my_total>0){
		star_switch_ck(1, my_yx);
		star_switch_ck(2, my_ml);
		star_switch_ck(3, my_sl);
		star_set_default();
		
		if (is_int(my_total)){
			var my_shi=my_total;
			var my_ge=0;
		} else {
			var tmp_total = my_total.toString();
			var tmp=tmp_total.split(".");  
			var my_shi=parseInt(tmp[0]);
			var my_ge=parseInt(tmp[1]);
		}
		star_set_my_rating(my_shi, my_ge);
	}
	
	star_set_count_show();
	
}

function collect_init() {
	check_collect_cookie();
}

function get_rating_sum(yx,ml,sl) {
	yx = parseFloat(yx);
	ml = parseFloat(ml);
	sl = parseFloat(sl);
	return (Math.round((yx*0.4+(ml+sl)*0.3)*10)/10);
}

function is_int(num) {
	var patrn=/^[0-9]{1,}$/;   
	if (patrn.exec(num)) {
		return true;
	}
	return false;
}

function get_average(total, add) {
	total = parseFloat(total);
	add = parseFloat(add);
	return (Math.round(((total*score_count+add)/(score_count+1))*10)/10);
}

function add_collect(type) {
	if (!check_is_login()) {
		return;
	}
	if (getCookie(xl_cookie_name_collect) == 1){
		if (person_name!=""){
			alert("已将 "+person_name+" 添加到您的明星收藏！");
		} else {
			alert("已添加到您的明星收藏！");
		}
		return;
	}
	if (type == 1){
		var id = movieid;
	} else if (type == 2){
		var id = peopleid;
	}
	
	set_collect_record(type, id, usrname);
	set_tracker_record(type, id, 'collect');

	document.getElementById("collect_count_show").innerHTML = "已有 <strong>"+ (parseInt(collect_count)+1) +"</strong> 位用户收藏了此明星";	

	setCookie(xl_cookie_name_collect, 1, 24);

	if (person_name!=""){
		alert("已将 "+person_name+" 添加到您的明星收藏！");
	} else {
		alert("已添加到您的明星收藏！");
	}
	
}

function check_collect_cookie() {
	if (getCookie(xl_cookie_name_collect) == 1){
		document.getElementById("collect_count_show").innerHTML = "已有 <strong>"+ (parseInt(collect_count)+1) +"</strong> 位用户收藏了此明星";
	}
}

function tag_post(type) {
	
	if (!check_is_login()) {
		return;
	}

	var content = document.getElementById("add_tags_content").value;
	if (content == ""){
		alert("标签不能为空");
		return;
	}
	if (!is_tag_content(content)){
		alert("标签中不能含有<>`~、!@#$%^&*(){}[]（）《》?+等特殊字符");
		return;
	}
	
	patrn = /[\s]+/g;
	content = content.replace(patrn, " ");
	if (type == 1){
		var id = movieid;
	} else if (type == 2){
		var id = peopleid;
	}

	set_tag_record(type, id, sid, content);
	
	document.getElementById('add_tags').style.display='none';	
	document.getElementById("add_tags_content").value = "";
	
	alert("提交成功！");
}

function is_tag_content(content) {
	var patrn=/[`~!@#\$%\^&\*\(\)\（\）\+\{\}\\、<>\《\》\?\-\[\]\|\.]/;   
	if (patrn.exec(content)) {
		return false;
	}
	return true;
}


function jiucuo_post(type) {
	var content = document.getElementById("jiucuo_content").innerHTML;
	if (content == ""){
		alert("内容不能为空");
		return;
	}
	if (type == 1){
		var id = movieid;
	} else if (type == 2){
		var id = peopleid;
	}

	var content = document.getElementById("jiucuo_content").innerHTML;
	//document.getElementById("jiucuo_iframe_show").innerHTML = "<iframe id='jiucuo_iframe' name='jiucuo_iframe' width='0' height='0' style='display:none;'></iframe>" ;
	var this_form=document.forms['jiucuo_form'];   
	this_form.action='http://rating.movie.xunlei.com/set_jiucuo.php?type='+type+'&id='+id+'&usrname='+usrname;   
	this_form.method='POST';   
	this_form.submit();
	
	document.getElementById('jiucuo').style.display='none';	
	document.getElementById("jiucuo_content").innerHTML = "" ;

	alert("提交成功！");

}


function tag_init() {
	/*
	var tag_ary_out = new Array();
	var xl_cookie = getCookie(xl_cookie_name_tag);
	if (xl_cookie != "") {
		try{			
			var tmp=xl_cookie.split(" ");  
			tag_ary_out = tag_ary.concat(tmp);
		} catch(e) {
			tag_ary_out = tag_ary;
		}
	} else {
		tag_ary_out = tag_ary;
	}
	
	star_set_tag_show(tag_ary_out);*/
	star_set_tag_show(tag_ary);

}

function star_set_tag_show(tag_ary) {
	//alert(tag_ary);
	var tag_show_str = '';

	for (i=0; i<tag_ary.length; i++) {
		if (true) {
			tag_show_str += '<a href="http://movies.xunlei.com/person/tags/'+encodeURI(tag_ary[i])+'">'+tag_ary[i]+'</a>';
		}
	}
	document.getElementById("tag_show").innerHTML = tag_show_str ;
}

function rating_post_cancel() {
	if (confirm("取消评分您的评分将不会被保留,确定取消吗")) {
		document.getElementById('ping_block').style.display='none';
	} else {
		//document.getElementById('ping_block').style.display='none';
	}
}