
$(document).ready(function(){
	$('a[name="odds"]').each(function(){
		$(this).after("<div id='div_"+this.id+"' name='upAndDown' style='display:inline;color:red'>&nbsp;&nbsp;</div>");
	});
	
	$('a[name="odds"]').each(function(){
		odds[this.id] = $(this).text();
		
		if( (($(this).text()+0)*1000) > (($(this).parent().prev().children().text()+0)*1000) ) $(this).css('color','#FF0000');
		else if( (($(this).text()+0)*1000) < (($(this).parent().prev().children().text()+0)*1000) ) $(this).css('color','#0000ff');
		
	});
	
	show_odds_change();
	
	$(document).click(function(e){
		var t;
		if(document.all) t = document.elementFromPoint(event.x,event.y).tagName;
		else t = e.target.tagName;
		if(t=='BODY'||t=='DIV'||t=='TD'||t=='IMG')
			$('#pop').hide();
	});
});

function myDate(timestamp) {
	d = new Date(timestamp);
	var jstimestamp = (d.getMonth()+1)+"-"+(d.getDate());
	return jstimestamp;
}


function show_div(div,gif){
	if($('#'+div).css('display')=='none'){
		$('#'+div).show();
		if(div=='div_tournament'){
			$('#span_company2').removeClass('company2');
			$('#span_company2').addClass('company22');
		}else{
			$('#span_company1').removeClass('company1');
			$('#span_company1').addClass('company11');
		}
	}else{
		$('#'+div).hide();
		if(div=='div_tournament'){
			$('#span_company2').removeClass('company22');
			$('#span_company2').addClass('company2');
		}else{
			$('#span_company1').removeClass('company11');
			$('#span_company1').addClass('company1');
		}
	}
}

function single_search(id){
	$(':input[name="tournament"][checked]').attr('checked',false);
	$(':input[name="tournament"][value="'+id+'"]').attr('checked',true);
	search();
}

function cancle_company_select(){
	$(':input[name="providerChecked"]').removeAttr('checked');
	$(':input[name="provider"]').removeAttr('checked');
	//$(':input[name="providerChecked"]').attr('checked',true);
}

function default_company_select(){
	$(':input[name="provider"][checked],:input[name="providerChecked"][checked]').removeAttr('checked');
	$(':input[name="provider"][value="3000343"]').attr('checked',true);
	$(':input[name="provider"][value="3000068"]').attr('checked',true);
	$(':input[name="provider"][value="3000248"]').attr('checked',true);
	$(':input[name="provider"][value="3000048"]').attr('checked',true);
	$(':input[name="provider"][value="3000021"]').attr('checked',true);
	$(':input[name="provider"][value="3000390"]').attr('checked',true);
	$(':input[name="providerChecked"][value="3000343"]').attr('checked',true);
	$(':input[name="providerChecked"][value="3000068"]').attr('checked',true);
	$(':input[name="providerChecked"][value="3000248"]').attr('checked',true);
	$(':input[name="providerChecked"][value="3000048"]').attr('checked',true);
	$(':input[name="providerChecked"][value="3000021"]').attr('checked',true);
	$(':input[name="providerChecked"][value="3000390"]').attr('checked',true);
}

function show_odds_change(){
	var options = {
		type: 'POST',
		data: odds,
		dataType: 'json',
		url: "index.server.php",
		success:function(s){
			// alert(s);return;
			if(s == ''){
				xx = setTimeout("show_odds_change()",30000);
				return;
			}
			
			$('#oBGSound').attr('src',"/chimes.wav");
			for(var i in s){
				odds0 = s[i].v;
				if((odds0+0)*1000 == 0) continue;
				odds[s[i].k] = s[i].v;
				id0 = s[i].k;
				var aLink = $('#'+id0);
				if((odds0+0)*1000 > (aLink.text()+0)*1000){
					$('#div_'+id0).html('<img src="http://w1.goalhi.com/bet_goalhi/images/up.gif" style="vertical-align:middle;">');// 上升后面的div里内容
					aLink.css('color','#FF0000');// 上升对应的文字颜色
				}
				else if((odds0+0)*1000 < (aLink.text()+0)*1000){
					$('#div_'+id0).html('<img src="http://w1.goalhi.com/bet_goalhi/images/down.gif" style="vertical-align:middle;">');// 下降后面的div里内容
					aLink.css('color','#0000ff');// 下降对应的文字颜色
				}
				aLink.text(odds0);
			}
			xx = setTimeout("show_odds_change()",30000);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			// alert(XMLHttpRequest+textStatus+errorThrown);
			xx = setTimeout("show_odds_change()",30000);
		}
	};
	$.ajax(options);
}

function eab(t,tournamentId,homeParticipantId,awayParticipantId){
	// var p = (window.location+'').substring(indexOf("/");
	// if(p>0) var responseUrl = (window.location+'').substring(0,p)+'?1=1';
	// else var responseUrl = window.location+'?1=1';
	var responseUrl = 'http://bet.goalhi.com/match.php?';
	if(t == 'a') responseUrl += '&type=asianHandicap';
	else if(t == 'b') responseUrl += '&type=overUnder';
	responseUrl += '&tournament='+tournamentId;
	responseUrl += '&homeParticipant='+homeParticipantId;
	responseUrl += '&awayParticipant='+awayParticipantId;
	// alert(responseUrl);
	// window.location.href = responseUrl+'';
	window.open(responseUrl+'');
}
function show_range_odds(v){
	if(v == 0){ $('a[name="odds"]').css('background-color',''); return; }
	var min = $('#odds_search_min').val()+0;
	var max = $('#odds_search_max').val()+0;
	$('a[name="odds"]').css('background-color','');
	$('a[name="odds"]').each(function(){
		if(($(this).text()+0)>=min && ($(this).text()+0)<=max) $(this).css('background-color','#FFFF99');
	});
}
function check_if_more_than_five(o){
	if( ($(":input[name='provider'][checked]").length + $(":input[name='providerChecked'][checked]").length) > 7){
		alert('最多选择7个公司');
		o.checked = false;
	}
}
function show_provider(d){
	for(var i=1;i<8;i++)
		$("#provider_"+i).hide();
	if(d) $("#provider_"+d).show();
}


function show_pop(e,h){
	$('#popcontent').html('加载中...');
	var x,y;
	if(document.all){
		x = e.x;
		y = e.y;
		$('#pop').css('top',document.documentElement.scrollTop+y+20);
	}else{
		x = e.pageX;
		y = e.pageY;
		$('#pop').css('top',document.body.scrollTop+y+20);
		// $('#pop').css('top',y+20);
	}
	var typeId = h.substring(33,35);
	if(typeId==43){
		$('#pop').css('width',500);
		if(x+520>document.body.clientWidth)
			$('#pop').css('left',x-520);
		else
			$('#pop').css('left',x+20);
	}else{
		$('#pop').css('width',300);
		if(x+320>document.body.clientWidth)
			$('#pop').css('left',x-320);
		else
			$('#pop').css('left',x+20);
	}
	$('#pop').show();
	$('#pop_a_odds').attr('href',h);
	
	var i = 0;
	var p = -1;
	h = h.substring(33);
	while(i<4){
		p = h.indexOf('-',p+2);
		i++;
	}
	var id = h.substring(0,p);
	while(true){
		if(id.indexOf('-')==-1)
			break;
		id = id.replace('-',',');
	}
	
	var change_colors = {
		'up':'red',
		'down':'blue',
		'':'green'
	};
	
	var options = {
		type: 'POST',
		data: 'id='+id,
		dataType: 'json',
		url: "index3.server.php",
		success:function(s){//alert(s);return;
			if(s.length==0){
				$('#popcontent').html('<div style="color:red">此庄家删除了该比赛的数据, 正在重新加载页面...</div>');
				setTimeout('window.location.reload()',2000);
				return false;
			}
			var html = '<table border="0" cellSpacing="1" cellPadding="0" bgColor="#E0B77B" width="100%">';
			if(typeId==43)
				html += '<tr class="title4"><td>主胜</td><td>平</td><td>客胜</td><td>时间</td><td>主胜率</td><td>平率</td><td>客胜率</td><td>返回率</td>';
			else if(typeId==47)
				html += '<tr class="title4"><td>大球</td><td>盘口</td><td>小球</td><td>时间</td>';
			else
				html += '<tr class="title4"><td>主队</td><td>盘口</td><td>客队</td><td>时间</td>';
			html += '</tr>';
			
			if(typeId==43)
				for(var date in s){
					html += '<tr bgColor="#ffffff"><td class="'+change_colors[s[date]['0win']['ud']]+'">';
					html += s[date]['0win']['odds'];
					html += '</td><td class="'+change_colors[s[date]['1draw']['ud']]+'">';
					html += s[date]['1draw']['odds'];
					html += '</td><td class="'+change_colors[s[date]['2lose']['ud']]+'">';
					html += s[date]['2lose']['odds'];
					html += '</td><td>';
					html += date.substring(5);
					html += '</td><td class="green">';
					html += s[date]['winRate'];
					html += '%</td><td class="green">';
					html += s[date]['drawRate'];
					html += '%</td><td class="green">';
					html += s[date]['loseRate'];
					html += '%</td><td>';
					html += s[date]['returnRate'];
					html += '%</td></tr>';
				}
			else
				for(var date in s){
					html += '<tr bgColor="#ffffff"><td class="'+change_colors[s[date]['0win']['ud']]+'">';
					html += s[date]['0win']['odds'];
					html += '</td><td>';
					html += s[date]['outcome']['odds'];
					html += '</td><td class="'+change_colors[s[date]['2lose']['ud']]+'">';
					html += s[date]['2lose']['odds'];
					html += '</td><td>';
					html += date.substring(5);
					html += '</td>';
					html += '</tr>';
				}
			
			html += '</table>';
			$('#popcontent').html(html);
			$('#popcontent').css('height','auto');
		}
	};
	$.ajax(options);
	
	return false;
}
