// [8,367,22] published at 2009-03-26 11:29:53

var Data = null;
function create_js(id, src) {
	var js = document.getElementById(id);
	if(js != null && js.tagName.toLowerCase() == "script") {
		document.getElementsByTagName("head").item(0).removeChild(js);
	}
	js = document.createElement('script');
	js.type = "text/javascript";
	js.id = id;
	js.src = src;
	document.getElementsByTagName("head").item(0).appendChild(js);
}
function remove_js(id) {
	var js = document.getElementById(id);       
	if (js != null && js.tagName.toLowerCase() == "script") {
		js.src = "http://";
		//document.getElementsByTagName("head").item(0).removeChild(js);
	}
}
function ceil($value) {
	return Math.ceil($value);
}

function Vote(sDid,sUrl,sName,sBrand,sPic,sPrice,objName,sType,sTxt) {
	this.sDId = sDid;
	this.sUrl = sUrl;
	this.sBrand = sBrand;
	this.sName =sName;
	this.sPic = sPic;
	this.sPrice = sPrice;
	this.objName = objName;
	this.sType = sType;

	this.sHeight = 40;
	this.sParam = null;
	this.sItem = {"61":{"251":"kz", "252":"yb","253":"bxh"}, "62":{"254":"xy","255":"mxq"}};

	//因为其他有几个地方在使用n'人'的情行，只有新版女首不加单位，所以加个空字作为例外
	if (sTxt=="空" || sTxt=="") {
		this.sTxt = "";
	}
	else if ( !(sTxt) ) {
		//以前用的时候没有定义这个参数，使用'人'作为单位
		this.sTxt = "人";
	}
	else {
		this.sTxt = sTxt;
	}
}
Vote.prototype = {
	$: function(id)	{
		return document.getElementById(id);
	},
	loadScore: function() {
		var d = new Date().getTime();
		create_js("fpro_vote_score_"+this.objName+"_1", "http://mark.sina.com.cn/v2/GetResult.php?p_mark=hzp_yg&i_mark=fpro_" + this.sDId + "&prefix=fpro1&call_back="+this.objName+".loadScoreCallBack&t=" + d);

		create_js("fpro_vote_score_"+this.objName+"_2", "http://mark.sina.com.cn/v2/GetResult.php?p_mark=hzp_myg&i_mark=fpro_" + this.sDId + "&prefix=fpro2&call_back="+this.objName+".loadScoreCallBack&t=" + d);
	},
	loadScoreCallBack: function() {

		if (typeof(fpro1_Data) != "undefined" && typeof(fpro2_Data) != "undefined")	{

			Data = fpro2_Data;
			for(i in fpro1_Data) {
				Data[i] = fpro1_Data[i];
			}
			var obj = this.sItem;
			for(i in obj) {
				var total = 0;
				var s = Array();
				for(var j in Data[i]) {
					var t = ceil(Data[i][j]);
					total += t;
					s[j] = t;
				}

				for(var j in obj[i]) {
					var txt = this.$(obj[i][j] + "_" + this.objName);
					var img = this.$(obj[i][j] + "img_" + this.objName);

					txt.innerHTML = s[j] + this.sTxt;

					var col_height = 0;

					if (total>0) {
						col_height = ceil(this.sHeight * (s[j] / total));
					}

					img.style.height = col_height + "px";
				}

				if (i == "61") {
					//对使用过的评价计算好评率
					var txt_per = this.$("per_gd" + "_" + this.objName);
					var percent = 100;	//当没有投票时使用多少?
					if (total > 0){
						percent = ceil(fpro1_Data["61"]["251"] / total * 100);	//只有狂赞才是好评吗?
						txt_per.innerHTML = percent + "%";
					}
					else {
						txt_per.innerHTML = "<span style='font-size:14px;'>暂无</span>";
					}
				}

			}
		}
	},
	changeScore: function() {
		var s = this.sParam;
		var num = ceil(Data[s[0]][s[1]]);
		num++;
		Data[s[0]][s[1]] = num;
	},
	vote: function(item) {
		var obj = this.sItem;
		var s = false;
		for(i in obj) {
			for(var j in obj[i]) {
				if(obj[i][j] == item) {
					s = Array(i, j)
					break;
				}
			}
		}

		var pid = s[0] == "61" ? "yg" : "myg";

		var src = "http://mark.sina.com.cn/v2/DoData.php?p_mark=hzp_" + pid + "&i_mark=fpro_" + this.sDId + "&name="+ this.sName +"&question[]=" + s[0] + "&option_" + s[0] + "=" + s[1] + "&type=get&ext2="+this.sUrl+"&ext3="+this.sPic+"&ext4="+this.sPrice+"&ext5="+this.sBrand+"&ext1="+this.sType;

		success = null;
		create_js("fpro_vote", src + "&call_back="+this.objName+".voteCallBack");
		this.sParam = s;

		//加载PV统计页
		var d = new Date().getTime();
		if ( document.getElementById('productVotePV') ) {
			document.getElementById('productVotePV').src = "http://eladies.sina.com.cn/misc/2008/1015/2118.html?"+d;
		}

	},
	voteCallBack: function() {
		if(success) {
			error_msg = "感谢您的投票！";
			this.changeScore();
		}
		alert(error_msg);
		remove_js("fpro_vote");

		this.loadScore();		
		//this.loadScoreCallBack();
	},
	set_survey: function(action){
		action_arr = {"kz":"kz", "yb":"yb", "bxh":"bxh", "xy":"xy", "mxq":"mxq"};
		this.vote(action_arr[action]);
	}
}