//////////////////////////更新最新数据//////////////////////////////////////////////////////////
var many = 0;//many=0,针对多场比赛的页面数据变化和更新，many=1，针对单场比赛的百家赔率变化和更新
var _type = "";  //不同的controller
var msShowOdds = 20000;
//var oPopupTimoutInterval = null;
var __popWindow = 0;//是否弹窗提示指数实时更新
var __popSound  = 0;//是否声音提示指数实时更新
var flash_Sound = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="goal"><param name="movie" value="images/sound2b.swf"><param name="quality" value="high"><param name="wmode" value="transparent"><embed src="images/sound2b.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="0" height="0"></embed></object>';

function Start(type){
	get_OpenWinMsg();
	get_SoundSwitch();
	if(type==1) {
		_type = "main";
		many = 0;
	}
	else if(type==5){
		_type = "beidan";
		many = 0;
	}
	else if(type==4){
		_type = "halffullodds";
		many = 0;
	}
	else if(type==3) {
		_type = "jinqiu";
		many = 0;
	}else{
		_type = "europeodds";
		many = 1;
		var type = $("type").innerHTML;
		if(type=='')  return;
		$("kind").value = type;
	}
	//如果页面是历史期数据，不用查询最及时数据
	var realqihao = $("realqihao").innerHTML;
	var qihao=$("current_qihao").innerHTML;
	if(Number(qihao)<Number(realqihao)){
		return;
	}
	setInterval(_fresh,40000);
}

function _fresh()
{
	var url="index.php?controller="+_type+"&action=ajax";
	if(many=='0')
	{
		var qihao=$("current_qihao").innerHTML;
		var pars="qihao="+qihao;
		var update='';
		var myAjax = new Ajax.Updater(
		update,
		url,
		{
			method: 'get',
			parameters: pars,
			evalScripts : true,
			onComplete:_ShowData
		});
	}
	else{
		var mid = $("matchid").innerHTML;
		var pars="mid="+mid;
		var update='';
		var myAjax = new Ajax.Updater(
		update,
		url,
		{
			method: 'GET',
			parameters: pars,
			evalScripts : true,
			onComplete:ShowEuroodds
		});
	}
}
//////////////////多场比赛的即时数据更新/////////////////////
function _ShowData(response)
{
	var stringText =response.responseText;
	if(stringText!=''){
		var _tem= stringText.split('@');
		var _len = _tem.length-1;
		for(var i=0;i<_len;i++){
			var temp = _tem[i];
			var __len = temp.length;
			var __tem = temp.split('%%');
			var mid = __tem[0];
			var oh = __tem[1];
			var od = __tem[2];
			var oa = __tem[3];
			var winpro = __tem[4];
			var drawnpro = __tem[5];
			var lostpro = __tem[6];
			var returnrate = __tem[7];
			var hometeam = __tem[8];
			var guestteam = __tem[9];
			var time = __tem[10];
			if(__popSound){
				$("ShowSound").innerHTML = flash_Sound;
			}
			if(__popWindow){
				PopupMsg();
				var pre_oh = $("div_"+mid+"_oh").innerHTML;
				var pre_od = $("div_"+mid+"_od").innerHTML;
				var pre_oa = $("div_"+mid+"_oa").innerHTML;
				var pre_time = $("div_"+mid+"_time").innerHTML;
				$("hometeam").innerHTML = hometeam;
				$("guestteam").innerHTML = guestteam;
				$("pre_time").innerHTML = pre_time;
				$("pre_oh").innerHTML = pre_oh;
				$("pre_od").innerHTML = pre_od;
				$("pre_oa").innerHTML = pre_oa;
				$("window_time").innerHTML = time;
				$("window_oh").innerHTML = '<font color='+compare(oh,pre_oh)+'>'+number_format(oh)+'</font>';
				$("window_od").innerHTML = '<font color='+compare(od,pre_od)+'>'+number_format(od)+'</font>';
				$("window_oa").innerHTML = '<font color='+compare(oa,pre_oa)+'>'+number_format(oa)+'</font>';
			}			
			$("div_"+mid+"_oh").innerHTML = number_format(oh);
			$("div_"+mid+"_od").innerHTML = number_format(od);
			$("div_"+mid+"_oa").innerHTML = number_format(oa);
			$("div_"+mid+"_winpro").innerHTML = number_format(winpro)+"%";
			$("div_"+mid+"_drawnpro").innerHTML = number_format(drawnpro)+"%";
			$("div_"+mid+"_lostpro").innerHTML = number_format(lostpro)+"%";
			$("div_"+mid+"_returnrate").innerHTML = number_format(returnrate)+"%";
			$("div_"+mid+"_eh").innerHTML = number_format(($("win_"+mid).value)*oh);
			$("div_"+mid+"_ed").innerHTML = number_format(($("drawnpro_"+mid).value)*od);
			$("div_"+mid+"_ea").innerHTML = number_format(($("lostpro_"+mid).value)*oa);

		}
	}
}

////////////与弹出窗口相关//////////////////////////
function PopupMsg(){
	getMsg();
	window.setTimeout(closeDiv, msShowOdds);
}
var divHeight,divWidth,docHeight,docWidth,objTimer,Msgi = 0;

function getMsg()
{
	try{
		divTop = parseInt($("Msg").style.top,10);
		divLeft = parseInt($("Msg").style.left,10);
		divHeight = parseInt($("Msg").offsetHeight,10);
		divWidth = parseInt($("Msg").offsetWidth,10);
		docWidth = parseInt(document.documentElement.clientWidth,10);
		docHeight = parseInt(document.documentElement.clientHeight,10);
		$("Msg").style.display="";
		$("Msg").style.top = (parseInt(document.documentElement.scrollTop ,10) + docHeight -parseInt($("Msg").offsetHeight,10))+'px';
		$("Msg").style.left = (parseInt(document.documentElement.scrollLeft,10) + docWidth - parseInt($("Msg").offsetWidth,10))+'px';

		objTimer = window.setInterval("moveDiv()",16);
	}
	catch(e){}
}

function resizeDiv()
{
	Msgi+=1;
	if(Msgi>1288) closeDiv();

	try{
		divHeight = parseInt($("Msg").offsetHeight,10);
		divWidth = parseInt($("Msg").offsetWidth,10);
		docWidth = document.documentElement.clientWidth;
		docHeight = document.documentElement.clientHeight;
		$("Msg").style.top = (docHeight - divHeight + parseInt(document.documentElement.scrollTop,10))+'px';
		$("Msg").style.left = (docWidth - divWidth + parseInt(document.documentElement.scrollLeft,10))+'px';
	}
	catch(e){}
}

function moveDiv()
{
	try
	{
		if(parseInt($("Msg").style.top,10) <= (docHeight - divHeight + parseInt(document.documentElement.scrollTop,10)))
		{
			window.clearInterval(objTimer);
		}else{
			$("Msg").style.top = (divTop - 1)+'px';
			divTop -= 1;
		}
	}
	catch(e){}
}

function closeDiv()
{
	$('Msg').style.display='none';
	if(objTimer) window.clearInterval(objTimer);
}
window.onresize = resizeDiv;
//js中数据格式化函数（数据包含小数点）
function number_format(s){
	var s=String(s);
	if(s.indexOf('.')==-1){  
		//如果此数字没有小数部分把它别称带小数的数字       
	    s=s+'.0000';              
	}              
	var temp = s.split('.');
	var temp_1 = temp[0];
	var temp_2 = temp[1];
	var temp_3 = temp_2.substring(0,2);
	return temp_1+'.'+temp_3;
}
////////cookies相关////////
function setCookie(name, value, lifetime) {
	var now = new Date();
	var expiry = new Date(now.getTime() + lifetime*30*24*60*60*1000);
	if ((value != null) && (value != ""))
	document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
	return getCookie(name) != null;
}

function getCookie(name) {
	var start, an_end;
	if (document.cookie) {
		start = document.cookie.indexOf(name+"=");
		if (start < 0) return null;
		start = document.cookie.indexOf("=", start) + 1;
		an_end = document.cookie.indexOf(";", start);
		if (an_end < 0) an_end = document.cookie.length;
		return unescape(document.cookie.substring(start, an_end));
	}
	else return null;
}

function deleteCookie(name) {
	var now = new Date();
	var expired = new Date(now.getTime() - 30*2*24*60*60*1000);
	document.cookie=name + "=null; expires=" + expired.toGMTString();
}
/////////////功能选项相关/////////////////////
function save_SoundSwitch() {
	deleteCookie("Sound");
	if($('Sound').checked){
		setCookie("Sound",1,24);
		__popSound = 1;
	}
	else{
		setCookie("Sound",0,24);
		__popSound = 0;
	}
}
function save_OpenWinMsg() {
	deleteCookie("Window");
	if($('Window').checked){
		setCookie("Window",1,24);
		__popWindow = 1;
	}
	else{
		setCookie("Window",0,24);
		__popWindow = 0;
	}
}
function get_OpenWinMsg() {
	$("Window").checked = true;
	__popWindow = 1;

}

function get_SoundSwitch() {
	$("Sound").checked = true;
	__popSound  = 1;

}
//////比较大小，返回不同的颜色//////////////////////////////
function compare(a,b){
	if(a > b)       return 'red';
	else if(a == b)   return 'black';
	else            return 'green';
}
