﻿var showCount = 0;
var allCount = 0;
var matchItem;
var isAllMatch;
var matchsId;
var matchsArr;
var change_flag = false;
var timeout2 ;
var now_date;

function BulidRow(index)
{
	var tournamentName = matchItem["tName"];		
	//var tournamentId =  matchItem["tId"];		
	var providers = matchItem["providers"];
	var live_id = matchItem["id"];
	//var eventId = matchItem["eventid"];
	var starttime = matchItem["starttime"];
	//var oddsID=matchItem["oddsID"];
	var companyID=matchItem["compayID"];
//	var homeId = matchItem["homeId"];
//	var awayId = matchItem["awayId"];
	var half = matchItem["half"];
	var createDt = matchItem["createdt"];
	var redioName=matchItem["redioName"];
	var redioUrl=matchItem["redioUrl"];
	var flag = false;	
	

	var tr_class = '';
	
	
	if(index % 2 == 0)
	{
		tr_class = "tdBg0";			
	}
	else
	{
		tr_class = "tdBg1";			
	}
	
	var rowspan = providers.length;
	for(var l = 0; l < providers.length ; l ++ )
	{				
		if(providers[l].odds.length > 1)
		{
			rowspan +=  (providers[l].odds.length-1);
		}						
	}			
	
	var liveFlag;
	if(half == '1')
	{
		liveFlag = '(' + text[18] + ')';
	}
	else
	{
		liveFlag = '(' + text[17] + ')';
	}
	
	var oTBodyRow = document.createElement("tbody");
	oTBodyRow.id = "tr_" + (index + 1);
	
	oTr = document.createElement("tr");
	oTr.className = tr_class;
	oTBodyRow.appendChild(oTr);
	
	oTd = document.createElement("td");
	oTd.rowSpan = rowspan;
	oTd.innerHTML = "<input type='checkbox' checked='checked' onclick='hide(\"tr_" + ( index + 1 ) + "\")'>";
	oTr.appendChild(oTd);
	
	oTd = document.createElement("td");
	oTd.className = "tdMatch";
	oTd.rowSpan = rowspan;	
	oTd.style.backgroundColor = matchItem["tColor"];
	
	oTd.innerHTML = tournamentName.replace("‘","'");
	
	var br = document.createElement("br");
	oTd.appendChild(br);
	var span = document.createElement("span");			
	span.innerHTML = AmountTimeDiff(starttime,6);		
	oTd.appendChild(span);
	oTr.appendChild(oTd);
	
	oTd = document.createElement("td");
	oTd.className = "tdTeam";
	oTd.rowSpan = rowspan;		
	a = document.createElement("a");
	//a.href = "#";
	a.innerHTML = liveFlag + matchItem["homeName"].replace("‘","'");			
	oTd.appendChild(a);					
	br = document.createElement("br");
	oTd.appendChild(br);
	a = document.createElement("a");
	//a.href = "#";
	a.innerHTML = matchItem["awayName"].replace("‘","'");			
	oTd.appendChild(a);	
	br = document.createElement("br");
	oTd.appendChild(br);
	span = document.createElement("span");
	span.innerHTML = "<a href=\"javascript:ShowAnalyse_" + lang + "(" + live_id + ")\">析</a>";
	oTd.appendChild(span);
	span = document.createElement("span");
	span.innerHTML = "<a href=\"javascript:history("+live_id+","+companyID+",1)\">亞</a>";
	oTd.appendChild(span);
	span = document.createElement("span");
	span.innerHTML = "<a href=\"javascript:history("+live_id+","+companyID+",2)\">歐</a>";
	oTd.appendChild(span);
	span = document.createElement("span");
	span.innerHTML = "<a href=\"javascript:history("+live_id+","+companyID+",3)\">大</a>";
	oTd.appendChild(span);
	if(redioName!="")
	{
	   span = document.createElement("span");
	   span.innerHTML = "<a href=\""+redioUrl+"\" target='_blank'><img src=\"images/live1.gif\" alt=\""+redioName+"\"></a>";
	   oTd.appendChild(span);
	}
	oTr.appendChild(oTd);
	
	//var tmp2 ='';		
	provider_flag = false;
	var hasData = false;
	
	for ( var l = 0; l < providers.length ; l++ )
	{
		var providerId = providers[l]["id"];
		var odds = providers[l]["odds"];
		
		var flag = false;
					
		for ( var m = 0; m < providersId.length ; m++ )
		{						
			if ( providerId == providersId[m])
			{
				flag = true;
				break;
			}
		}
		
		/*
		modify by gj  09-03-25
		start
		*/
		var providername = providers[l]["name"];
		var providerhref = '#';
              
		if(!flag)
		{
			continue;
		}
		hasData = true;
		
		if( provider_flag )
		{
			//tmp2 += '<tr class=' + tr_class + '>';
			oTr = document.createElement("tr");
			oTr.className = tr_class;
			oTBodyRow.appendChild(oTr);
		}
		provider_flag = true;
		
		
		var oddsflag = false;		
		
		if (odds.length == 0)
		{	

			oTd = document.createElement("td");
			oTd.className = "tdInc";
			a = document.createElement("a");
			//modify by gj
			//a.href = "javascript:provider(" + providerId + ",pageType)";
			a.href = providerhref;
			//end
			a.innerHTML = providername;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.className = "tdA1";						
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.className = "tdA2";						
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.className = "tdA1";						
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.className = "tdB1";						
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.className = "tdB1";						
			oTr.appendChild(oTd);
			
			oTr = document.createElement("tr");
			oTr.className = "tdB1";								
			oTBodyRow.appendChild(oTr);
			
			oTd = document.createElement("td");
			oTd.className = "tdC1";						
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.className = "tdC1";						
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.className = "tdC1";						
			oTr.appendChild(oTd);
			
		}
		else
		{
			//tmp2 += '<td class="tdInc" rowspan="' + odds.length + '"><a href="javascript:provider(' + providerId + ',pageType)">' + providername + '</a></td>';
			
			oTd = document.createElement("td");
			oTd.rowSpan = odds.length;
			oTd.className = "tdInc";
			a = document.createElement("a");
			//modify by gj
			//a.href = "javascript:provider(" + providerId + ",pageType)";
			//a.href = providerhref;
			//end
			a.innerHTML = providername;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
		}
		
		for(var m = 0; m < odds.length ; m ++ )
		{
			var ah_home_odds = odds[m].AH.home == "" ? "" : odds[m].AH.home;
			var ah_away_odds = odds[m].AH.away == "" ? "" : odds[m].AH.away;
			var handicap;
			if( odds[m].AH.handicap == "")
			{
				handicap = "" ;
			}
			else if( odds[m].AH.handicap.indexOf('-') >= 0)
			{
				
				handicap = sign[1] + CetPkStr1(parseInt(4 * parseFloat(odds[m].AH.handicap.replace('-',''))));
			}
			else
			{
				if ( odds[m].AH.handicap == "0" )
				{
					handicap = CetPkStr1(parseInt(4 * parseFloat(odds[m].AH.handicap)));
				}
				else
				{
					handicap = sign[0] + CetPkStr1(parseInt(4 * parseFloat(odds[m].AH.handicap)));
				}
			}
			
			 
			var ah_home_oId =  odds[m].AH.home_oid;
			var ah_away_oId =  odds[m].AH.away_oid;
			var ah_handicap_oId =  odds[m].AH.handicap_oid;
			
			var over_odds = odds[m].OU.over == "" ? "" : odds[m].OU.over;
			var under_odds = odds[m].OU.under == "" ? "" : odds[m].OU.under;
			//var score_base = odds[m].OU.base == "" ? "" : CetPkStr2(parseInt(4 * parseFloat(odds[m].OU.base)));
			var score_base = odds[m].OU.base == "" ? "" : odds[m].OU.base;
			var over_oId =  odds[m].OU.over_oid;
			var under_oId =  odds[m].OU.under_oid;
			var base_oid =  odds[m].OU.base_oid;
			
			var hda_home_odds = odds[m].HDA.home == ""? "" : odds[m].HDA.home;
			var draw_odds = odds[m].HDA.draw == ""? "" : odds[m].HDA.draw;
			var hda_away_odds = odds[m].HDA.away == ""? "" : odds[m].HDA.away;
			var hda_home_oId =  odds[m].HDA.home_oid;
			var hda_away_oId =  odds[m].HDA.away_oid;
			var draw_oId =  odds[m].HDA.draw_oid;
			
			var oid1='';
			var oid2='';
			var oid3='';
			
			if(ah_home_oId != "")
			{
				oid1=ah_home_oId;						
			}
			else if(ah_away_oId != "")
			{
				oid1=ah_away_oId;
			}
			else if(ah_handicap_oId!="")
			{
			   oid1=ah_handicap_oId;
			}
			else
			{
				oid1=0;
			}
			
								
			if(hda_home_oId != "")
			{
				oid2=hda_home_oId;
			}
			else if(draw_oId != "")
			{
				oid2=draw_oId;
			}
			else if(hda_away_oId != "")
			{
				oid2=hda_away_oId;
			}
			else
			{
				oid2=0;
			}
			
			if(over_oId != "")
			{
				oid3=over_oId;
			}
			else if(under_oId!="")
			{
				oid3=under_oId;
			}
			else if(base_oid!="")
			{
				oid3=base_oid;
			}
			else
			{
				oid3=0;
			}	

			
			if( oddsflag == true )
			{
				//tmp2 += '<tr class=' + tr_class + '>';
				oTr = document.createElement("tr");
				oTr.className = tr_class;
				oTBodyRow.appendChild(oTr);
			}	
			
			var oddsflag = true ;

			oTd = document.createElement("td");
			oTd.id = ah_home_oId;
			oTd.className = "tdA1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",1)";
			a.innerHTML = ah_home_odds;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");	
			oTd.id = ah_handicap_oId;				
			oTd.className = "tdA2";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",1)";
			a.innerHTML = handicap;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.id = ah_away_oId;
			oTd.className = "tdA1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",1)";
			a.innerHTML = ah_away_odds;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.id = hda_home_oId;
			oTd.className = "tdB1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",2)";
			a.innerHTML = hda_home_odds;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.id = draw_oId;
			oTd.className = "tdB1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",2)";
			a.innerHTML = draw_odds;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.id = hda_away_oId;
			oTd.className = "tdB1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",2)";
			a.innerHTML = hda_away_odds;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.id = over_oId;
			oTd.className = "tdA1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",3)";
			a.innerHTML = over_odds;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.id = base_oid;
			oTd.className = "tdC1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",3)";
			a.innerHTML = score_base;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);
			
			oTd = document.createElement("td");
			oTd.id = under_oId;
			oTd.className = "tdC1";
			a = document.createElement("a");
			a.href = "javascript:history("+live_id+","+odds[m].companyid+",3)";
			a.innerHTML = under_odds;			
			oTd.appendChild(a);					
			oTr.appendChild(oTd);	
			
			
		}			
	}	
	
	
	if( !hasData )
	{			
		oTd = document.createElement("td");
		oTd.className = "tdInc";								
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdA1";						
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdA2";						
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdA1";						
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdB1";						
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdB1";						
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdB1";								
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdC1";						
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdC1";						
		oTr.appendChild(oTd);
		
		oTd = document.createElement("td");
		oTd.className = "tdC2";						
		oTr.appendChild(oTd);
	}
	
	var table = document.getElementById("odds");
	table.appendChild(oTBodyRow);	

	var i = index + 1;
	if (i < matchsArr.length)
	{
		matchItem = matchsArr[i];
		if ( i % 10 == 0 )
		{
			timeout2 = setTimeout("BulidRow(" + i + ")", 10);
		}
		else
		{
			BulidRow(i);
		}
	}
	else
	{
		clearTimeout(timeout2);
		now_date = AmountTimeDiff(matchsArr[0]["starttime"],9);
		change_flag = true;
	}
}

function BulidTable()
{  
	isAllMatch = false;		
	var matchsId_cookie = getCookie("@matchsID");
	var compayID=defaultProvidersId.split(',')[0];
	//var matchsId_cookie_tmp = getCookie("@matchsID");
	//setCookie("@matchsID1",matchsId_cookie_tmp);	
	setCookie("@flag","1");	
	var matchsId_show = "All";	
	if(matchsId_cookie != null && matchsId_cookie != "" && matchsId_cookie != "null")
	{
		if(matchsId_cookie == "All")
		{
			isAllMatch = true;
		}	
		else
		{
			matchsId = matchsId_cookie.split(",");	
			matchStr = matchsId_cookie;
		}		
	}
	else
	{	
		if ( typeof( matchinfos ) != 'undefined')
		{
			var dayOfWeek = matchinfos.day;
			
			isAllMatch = true;
		}
	}
	var table = '<table cellpadding="2" cellspacing="0" border="1" bordercolor="#DDDDDD" id="odds"><tr class="thead"><th></th><th>' + text[0] + '</th><th>' 
	+ text[1] + '</th><th>' + text[2] + '</th><th  colspan=3>' + text[3] + '</th><th  colspan=3>' + text[4] + '</th><th  colspan=3>' + text[5] + '</th></tr>';
	//table += '<tr class="tdgong"><th colspan="13">公告:球皇新指数正在测试中，如有错误敬请见谅!</th></tr>';	
	table += '<tr  class="thead2"><th colspan="4"></th><th>' + text[8] + '</th><th>' + text[11] + '</th><th>' + text[9] + '</th><th>' + text[8] + '</th><th>' + text[10] + '</th><th>' + text[9] + '</th><th>' + text[12] + '</th><th title="' + text[19] + '">' + text[14] + '</th><th>' + text[13] + '</th></tr>' ;
	
	table += '<tr class="tdWidth">'
			+ '<td width="3%"></td>'	<!-- 显示 -->
			+ '<td width="9%"></td>'	<!-- 赛事 -->
			+ '<td width="20%"></td>'	<!-- 对阵 -->
			+ '<td width="8%"></td>'	<!-- 公司 -->
			+ '<td width="6%"></td>'
			+ '<td width="10%"></td>'
			+ '<td width="6%"></td>'
			+ '<td width="6%"></td>'	<!-- 欧洲 -->
			+ '<td width="6%"></td>'
			+ '<td width="6%"></td>'
			+ '<td width="6%"></td>'	<!-- 大小 -->
			+ '<td width="6%"></td>'
			+ '<td width="6%"></td></tr>';
			
	
				
	table += "</table>";

	var div = document.getElementById("oddsinfo");
	div.innerHTML = table;
	//alert('a');
	//setTimeout('',100);	
	//alert(datas.length);
	if ( datas.length == 0 )
	{
		var table = document.getElementById("odds");   
		var oTBody = document.createElement("tbody"); 
		table.appendChild(oTBody);
		
    	var oTr = document.createElement("tr");		
		oTBody.appendChild(oTr);
		
		var oTd = document.createElement("td");
		oTd.colSpan = 13;   
		oTd.className = "tdDate";
		oTd.innerText = text[6]; 
		oTr.appendChild(oTd);
	}
	else
	{
		var datetime = matchinfos.dt;
		matchsArr = new Array();
		var matchsTmpArr = new Array();		
		
		for ( var i = 0; i < datas.length; i++ )
		{
			var matchs = datas[i].matchs;
			var providerId = datas[i].pId;
			
			var providerName = "";
			
			for ( var l = 0; l < providerinfos.length; l ++ )
			{
				if ( providerId == providerinfos[l].id)
				{
					providerName = providerinfos[l].name;
					break;
				}
			}
			for ( var j = 0; j < matchs.length; j++ )
			{			
				if ( matchs[j].odds.length == 0)
				{
					continue;
				}
				
				
				var matchId = matchs[j].id;
				//var oddsID = matchs[j].odds.oddsid;
				//alert(matchId);
				var matchinfo = null;
				for ( var l = 0; l < matchinfos.matchs.length; l ++ )
				{						
					if ( matchId == matchinfos.matchs[l].id )
					{
						matchinfo = matchinfos.matchs[l];
						break;
					}
				}
				if ( matchinfo == null )
				{
					continue;
				}				
				
				
				var index = -1;
				
				var f = false;
				for ( var k = 0; k < matchsTmpArr.length; k++ )
				{
					if ( matchsTmpArr[k] == matchId )
					{		
						f = true;
						break;	
					}
				}
				
				if ( !f )
				{
					matchsTmpArr[matchsTmpArr.length] = matchId;					
				}
				
				if ( matchsId_show != "All" &&  ("," + matchsId_show  + ",").indexOf("," + matchId + "," ) == -1 )
				{
					continue;
				}
				
				for ( var k = 0; k < matchsArr.length; k++ )
				{
					if ( matchsArr[k]["id"] == matchId )
					{
						index = k;
						break;
					}
				}	
				if ( index == -1 )
				{				
					//var matchinfo = null;
//					for ( var l = 0; l < matchinfos.matchs.length; l ++ )
//					{						
//						if ( matchId == matchinfos.matchs[l].id )
//						{
//							matchinfo = matchinfos.matchs[l];
//							break;
//						}
//					}	
//					
//					if ( matchinfo == null )
//					{
//						continue;
//					}
					
					if ( !isAllMatch && ("," + matchStr + ",").indexOf("," + matchinfo.tId + "," ) == -1 )
					{						
						continue;
					}
					
					var matchIndex = getIndex(matchinfo.starttime,matchsArr);
					//var matchIndex = matchsArr.length;	
					matchsArr[matchIndex] = new Array();
					matchsArr[matchIndex]["id"] = matchId;
					//matchsArr[matchIndex]["oddsID"] = oddsID;
					matchsArr[matchIndex]["compayID"] = compayID;
					//matchsArr[matchIndex]["eventid"] = eventId;
					matchsArr[matchIndex]["tName"] = matchinfo.tName;		
					//matchsArr[matchIndex]["tId"] =  matchinfo.tId;
					matchsArr[matchIndex]["starttime"] = matchinfo.starttime;
                    matchsArr[matchIndex]["redioName"] = matchinfo.redioName;
				   matchsArr[matchIndex]["redioUrl"] = matchinfo.redioUrl;
					matchsArr[matchIndex]["homeName"] = matchinfo.homename;
					matchsArr[matchIndex]["awayName"] = matchinfo.awayname;
					matchsArr[matchIndex]["half"] = matchinfo.half;
					matchsArr[matchIndex]["tColor"] = matchinfo.tColor;
					if (typeof( matchinfo.createdt ) != 'undefined' && matchinfo.createdt != null)
					{
						matchsArr[matchIndex]["createdt"] = matchinfo.createdt;
					}
					
					matchsArr[matchIndex]["providers"] = new Array();	
					
					index = matchIndex;			
					
				}
				
				var providersArr = matchsArr[index]["providers"];
				
				var providerIndex = providersArr.length;
				providersArr[providerIndex] = new Array();		
				providersArr[providerIndex]["id"] = providerId;
				providersArr[providerIndex]["name"] = providerName;
				providersArr[providerIndex]["odds"] = matchs[j].odds;
				
			}
		}		
		
		//dataOrder(matchsArr);			
		if (matchsArr.length > 0)
		{
			//var table = document.getElementById("odds"); 
//			var oTBody = document.createElement("tbody"); 
//			table.appendChild(oTBody);
//			
//			var oTr = document.createElement("tr");		
//			oTBody.appendChild(oTr);
//			
//			var oTd = document.createElement("td");
//			oTd.colSpan = "13";
//			oTd.className = "tdDate" ;
//			oTd.id = "td_date";
//			oTd.appendChild(document.createTextNode(datetime));
//			oTr.appendChild(oTd);
			allCount = matchsTmpArr.length;
			showCount = matchsArr.length;	
			matchItem = matchsArr[0];			
			timeout2 = setTimeout("BulidRow(0)", 100);
		}
		else
		{
			allCount = matchsTmpArr.length;
			showCount = matchsArr.length;
			
			var table = document.getElementById("odds");   
			var oTBody = document.createElement("tbody"); 
			table.appendChild(oTBody);
			
			var oTr = document.createElement("tr");		
			oTBody.appendChild(oTr);
			
			var oTd = document.createElement("td");
			oTd.colSpan = 13;   
			oTd.className = "tdDate";
			oTd.innerText = text[6]; 
			oTr.appendChild(oTd);
		}
      if(matchsArr.length>0)
		  setInterval("hideMatch();",5000);
	}
	
	
}
function hideMatch()
{ 
  for(var i=0;i<matchsArr.length;i++)
  {
    if(!ifShow(matchsArr[i]["starttime"]))
    {
      var line = document.getElementById("tr_" + ( i + 1 ));
	  if(line.style.display!="none")
         {
	   line.style.display = "none";
	   hide_count ++;
	
	   var hider =  document.getElementById('hider');
	  hider.innerText = hide_count;
         }
    }
  }
}
function getIndex(statetime,matchsArr)
{
	
	for ( var i = 0; i < matchsArr.length ; i ++ )
	{
		var date_tmp = matchsArr[i]["starttime"];

		if ( Date.parse(AmountTimeDiff(statetime,9)) < Date.parse(AmountTimeDiff(date_tmp,9)))
		{
			for (var j = matchsArr.length; j > i ; j-- )
			{
				matchsArr[j] = matchsArr[j-1];
			}
			
			return i;
		}
	}
	
	return matchsArr.length;
}


