﻿/*
 * shengxiao common
 * @author Brice Li
 * @date 2008-7-23
 */

//config

var votesvr = "http://svr.game.xunlei.com:8080/plus/submit.vote";
var voteressvr = "http://svr.game.xunlei.com:8080/plus/result.vote";
var gameName = "ShengxiaoVote";
var subject = "20090108";
var votetitle = "生肖法宝修炼系统？";
var voteoptions= new Array("非常有趣","还好一般","没啥感觉","没有必要");
var cboxs;
var telinfo="<li>帐号注册及雷点充值请咨询：<br />0755-26035666 </li><li>客服电话：<br />021-61276626</li><li>客服传真:<br />021-61259088-873</li><li>迅雷商务:<br />0755-26035888-8264</li>";

//跨域
var webGame;
if (!webGame){webGame = {};}

if(!webGame.acrossDomain){webGame.acrossDomain = {};}
 
var scriptBlock = document.createElement("script");
webGame.acrossDomain.request = function(vPath,func,isDebug)
{
    scriptBlock = document.createElement("script"); 
    if(isDebug)alert(vPath); 
	scriptBlock.src = vPath;
	scriptBlock.type = "text/javascript";
	scriptBlock.language = "javascript";
	
	if(webGame.acrossDomain.getBrowseName().toLocaleLowerCase() == "ie"){
		scriptBlock.onreadystatechange = function(){
			if(isDebug)
				alert(scriptBlock.readyState);
			webGame.acrossDomain.cleanStatus();
			if("loaded" == scriptBlock.readyState || "complete" == scriptBlock.readyState){
				func(votertn);
			}
			webGame.acrossDomain.resetStatus();
		}
	}else{
		scriptBlock.onload = function(){
			webGame.acrossDomain.cleanStatus(); 
			try{ 
				func(votertn);
			 
			}catch(e){ 
			}  
			webGame.acrossDomain.resetStatus();
		}
	}
	document.getElementsByTagName("head")[0].appendChild(scriptBlock);
}

webGame.acrossDomain.getBrowseName = function()
{
	if (window.navigator.userAgent.indexOf("MSIE")>=1)
	{
		 return 'IE';
	}else{
		if (window.navigator.userAgent.indexOf("Firefox")>=1)
		{
			//Firefox
			 return 'fiefox';
			
		}else{
			//other brownse
			 return 'unknow';
		}
	}
} 

 
webGame.acrossDomain.cleanStatus = function(txt){
	if(!txt){txt='Loading ... ...';}
	try{
		window.status=txt;
	}catch(e){
		alert('Debug:Status='+e);
	}
}
webGame.acrossDomain.resetStatus = function()
{
	try{
		window.status="";
	}catch(e){
	}
}

//初始化投票选项
function initVote(ul) {
	if (!ul)return;
	var content="";
	for (var i=0;i<voteoptions.length;i++){
		content+=" <li><input name=\"choose\" type=\"radio\" value="+i+" /> <label for=\"\">"+voteoptions[i]+"</label></li>";
	}
	content+="<li class='btn_p'><button id='submitBtn' value='提交' class='button'>提交</button><button id='resultBtn' value='查看' class='button'>查看</button></li>";
	
	ul.innerHTML=content;
	document.getElementById('submitBtn').onclick = submitVote;
	document.getElementById('resultBtn').onclick = showVote;
	cboxs = document.getElementsByName("choose");
}

function submitVote() {
	var url = votesvr + "?gameName=" + gameName + "&subject=" + subject
			+ "&rtntype=script&option=";
	var isChecked = false;
	for ( var i = 0; i < cboxs.length; i++) {
		if (cboxs[i].checked) {
			url += i;
			isChecked = true;
			break;
		}
	}
	if (isChecked){
		document.getElementById('submitBtn').disabled=true;
		webGame.acrossDomain.request(url, submitVoteAction, false);	
	}else
		alert("请选择");
}
function submitVoteAction(rtn) {
	if (rtn.voteSubmitStatus == 0) {
		alert("谢谢您的参与");
	} else if (rtn.voteSubmitStatus == 2) {
		alert("一天只能投票一次");
	} else {
		alert("投票失败 请重试");
	}
	document.getElementById('submitBtn').disabled=false;
}
function showVote() {
	var url = voteressvr + "?gameName=" + gameName + "&subject=" + subject
			+ "&rtntype=script";
	webGame.acrossDomain.request(url, showVoteAction, false);
}
function showVoteAction(rtn) {
	window.open('http://shengxiao.xunlei.com/voter.html?r1=' + rtn.resultMap['0'] + '&r2='
			+ rtn.resultMap['1'] + '&r3=' + rtn.resultMap['2'] + '&r4='
			+ rtn.resultMap['3'] + '&r5=' + rtn.resultMap['4']);
}

function getResponseParameter(strname) {
	var hrefstr, pos, parastr, para, tempstr;
	hrefstr = window.location.href;
	pos = hrefstr.indexOf("?");
	parastr = hrefstr.substring(pos + 1);
	para = parastr.split("&");
	tempstr = "";
	for (i = 0; i < para.length; i++) {
		tempstr = para[i];
		pos = tempstr.indexOf("=");
		if (tempstr.substring(0, pos) == strname) {
			return tempstr.substring(pos + 1);
		}
	}
	return "";
}

function initVoteRs(){
	if(!document.getElementById("tp_content"))return;
	var c1=getResponseParameter('r1');
	var c2=getResponseParameter('r2');
	var c3=getResponseParameter('r3');
	var c4=getResponseParameter('r4');
	if(c1==''||c1=='undefined'||!c1)c1=0;
	if(c2==''||c2=='undefined'||!c2)c2=0;
	if(c3==''||c3=='undefined'||!c3)c3=0;
	if(c4==''||c4=='undefined'||!c4)c4=0;
	var sum = c1*1+c2*1+c3*1+c4*1;
	
	var p1 = (sum!=0)?(((c1/sum)*100).toFixed(0)):0;
	var p2 = (sum!=0)?(((c2/sum)*100).toFixed(0)):0;
	var p3 = (sum!=0)?(((c3/sum)*100).toFixed(0)):0;
	var p4 = (sum!=0)?(((c4/sum)*100).toFixed(0)):0;
	
	var content=' <h6>'+votetitle+'</h6>';
	content += "<table>";
	content +='<tr><td width="107" align="right">A.</td><td width="130" align="center">'+voteoptions[0]+'<td width="50" align="center">|</td><td>'+c1+'票</td><td><span class="tp_v" style="width:'+p1+'px;"></span><em>'+p1+'%</em></td></tr>';
	content +='<tr><td width="107" align="right">B.</td><td width="130" align="center">'+voteoptions[1]+'<td width="50" align="center">|</td><td>'+c2+'票</td><td><span class="tp_v" style="width:'+p2+'px;"></span><em>'+p2+'%</em></td></tr>';
	content +='<tr><td width="107" align="right">C.</td><td width="130" align="center">'+voteoptions[2]+'<td width="50" align="center">|</td><td>'+c3+'票</td><td><span class="tp_v" style="width:'+p3+'px;"></span><em>'+p3+'%</em></td></tr>';
	content +='<tr><td width="107" align="right">D.</td><td width="130" align="center">'+voteoptions[3]+'<td width="50" align="center">|</td><td>'+c4+'票</td><td><span class="tp_v" style="width:'+p4+'px;"></span><em>'+p4+'%</em></td></tr>';
	content += "</table>";
	content +='<p style="padding-top:20px;"><button type="button" class="button" onclick="window.close();" value="关闭">关闭</button></p>';
	document.getElementById("tp_content").innerHTML=content;
}


//mycode
window.onload = init;
var cboxs;
function init(){
	var leftdiv = document.getElementById("left_box");
	if(leftdiv){
		var uls = leftdiv.getElementsByTagName("ul");
		if(uls && uls[0]){
			//change vote
			leftdiv.getElementsByTagName("p")[0].innerHTML=votetitle;
			initVote(uls[0]);
		}
		if(uls && uls[1]){
			//change info
			uls[1].innerHTML=telinfo;
		}
	}
}

	document.domain = "xunlei.com";

	function setCookie(name,value,hours)
	{
		var expireDate=new Date(new Date().getTime()+hours*3600000);
		document.cookie = name + "=" + escape(value) + "; path=/; domain=xunlei.com ; expires=" + expireDate.toGMTString() ;
	}
	var advid = "";
	var url = window.location.href;
	getPara(url);
	if(advid != "")
	{
		setCookie("shengxiao_statadvid",advid,1);
		setCookie("location",url,1);
	}
	function getPara(url) {
		var para = "";
		var paraindex = url.indexOf("?");
		if(paraindex != -1){
			var advidindex = url.indexOf("advid=");
			if(advidindex != -1){
				advid = url.substring(advidindex + 6);
			}
		}

	}
	function register() {
		var registerUrl = "http://i.xunlei.com/register/register.htm";
		var advid = getCookie("shengxiao_statadvid");
		if( advid !=""){
		     window.open(registerUrl + "?advid=" +advid ,"reg");
	  }else{
	  	   window.open(registerUrl ,"reg");
	  }	
	}
	function download(downloadUrl) {
		var advid = getCookie("shengxiao_statadvid");
		if( advid !=""){
		    window.open(downloadUrl  + "?advid=" +advid  ,"download");
	  }else{
	  	  window.open(downloadUrl ,"download");
	  }	
		
	}
	function getCookie(name)
	{
		var search;
		search = name + "=";
		offset = document.cookie.indexOf(search);
		if (offset != -1) 
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
		else
		{
			return "";
		}
	}

