var showType = lastShowType = 'm';
var out1 = out2 = false;
var turnNo = 0;

$(document).ready(function(){	
	
	$('a[id^="an_"]').mouseover(function(){
		$(this).addClass('scred');
	});
	$('a[id^="an_"]').mouseout(function(){
		if(this.id.substring(3)==showType)
			return;
		else
			$(this).removeClass('scred');
	});
	
	var t = read_cookie('gs_schedule_d');
	if(t!='')
		currentDate = t;
	t = read_cookie('gs_schedule_type');
	if(t!='')
		showType = t;
	t = read_cookie('gs_schedule_turnNo');
	if(t!='')
		turnNo = t;
	
	switch_type(showType);
});

function fold(id,e){
	if($('tr[name="stats_'+id+'"]:first').css('display')=='none'){
		e.removeClass('jia').addClass('jian');
		$('tr[name="stats_'+id+'"]').show();
	}else{
		e.removeClass('jian').addClass('jia');
		$('tr[name="stats_'+id+'"]').hide();
	}
}

function set_team(){
	var id = new Array();
	var i = 0;
	$(':input[name="team"]:checked').each(function(){
		id[i++] = this.value;
	});
	
	if(id.length==0 || $(':input[name="team"][checked=false]').length==0)
		team_id = null;
	else
		team_id = id.join(',');
}

function search_team(f){
	set_team();
	
	if(f){
		$('#tip_team').text($(':input[name="team"]:checked').length+'支');
		$('a[id^="a_team_"]').removeClass('scredon').addClass('scredover');
	}
	
	show_data(currentDate,null,null);
	
	// var id = set_team();
	
	// if(id==null)
		// window.location.href = '?league_id='+league_id;
	// else
		// window.location.href = '?league_id='+league_id+'&team_id='+id;
}



function switch_type(v){
	lastShowType = showType;
	showType = v;
	
	$('span[id^="previous_next_"]').hide();
	$('#previous_next_'+v).show();
	$('a[id^="an_"]').removeClass('scred');
	$('#an_'+v).addClass('scred');

	if(showType=='r')
		$('#tip_before_d').text('当前选择轮次：');
	else
		$('#tip_before_d').text('当前选择日期：');
	
	show_data(currentDate,null,null);
}

function show_data(d, t ,t2){
	if(t!=null){
		if(t=='c'){//今天
			d = new Date();
		}else{//向前 向后
			d = new Date(d.substring(0,4) , parseInt(d.substring(4,6), 10)-1, d.substring(6,8));
			var d0 = d;
			
			d = Date.parse(d)/1000;
			if(t2=='m'){
				if(t=='n'){
					d += 3600*24*31;
					for(var i=0;i<4;i++)
						if((new Date(d*1000)).getMonth()-d0.getMonth() != 1)
							d -= 3600*24;
				}else if(t=='p'){
					d -= 3600*24*31;
					for(var i=0;i<4;i++)
						if((new Date(d*1000)).getMonth()-d0.getMonth() != -1)
							d -= 3600*24;
				}
			}else if(t2=='w'){
				if(t=='n')
					d += 3600*24*7;
				else if(t=='p')
					d -= 3600*24*7;
			}else if(t2=='d'){
				if(t=='n')
					d += 3600*24;
				else if(t=='p')
					d -= 3600*24;
			}else if(t2=='r'){
				if(t=='n')
					turnNo++;
				else if(t=='p')
					turnNo--;
				if(turnNo<0)
					turnNo=0;
			}
			d = new Date(d*1000);
		}
		
		var s = d.getMonth()+1;
		if(s<10)
			s = '0'+s;
		if(d.getDate()<10)
			s += '0'+d.getDate();
		else
			s += ''+d.getDate();
		s = d.getFullYear().toString()+s;
		
		currentDate = s;
	}else{//方式切换
		currentDate = d;
	}
	
	set_tip();
	
	var data;
	if(showType=='r' && lastShowType!='r'){//非轮次切换到轮次
		data = 'd='+currentDate+'&type='+showType+'&league_id='+league_id;
	}else if(showType!='r' && lastShowType=='r'){//轮次切换到非轮次
		data = 'd='+currentDate+'&turnNo='+turnNo+'&type='+showType+'&league_id='+league_id;
	}else if(showType=='r')//无切换, 当前轮次
		data = 'd='+currentDate+'&turnNo='+turnNo+'&type='+showType+'&league_id='+league_id;
	else//无切换, 当前非轮次
		data = 'd='+currentDate+'&type='+showType+'&league_id='+league_id;
	
	var id = team_id;
	if(id!=null)
		data += '&team_id='+id;
	
	var options = {
		type:'POST',
		url:'schedule.server.php',
		data:data,
		// async: false,
		cache: false,
		success:function(s){
			$('#datepicker').hide();
			//切换到轮次, 记住当前轮次
			if(showType=='r' && lastShowType!='r')
				turnNo = s.substring(s.indexOf('第')+1,s.indexOf('轮'));
			
			if(showType=='r' && lastShowType!='r'){//非轮次切换到轮次
				$('#tip_d').text('第'+turnNo+'轮');
			}else if(showType!='r' && lastShowType=='r'){//轮次切换到非轮次
				currentDate = read_cookie('gs_schedule_d');
	
				set_tip();
			}
			//设上次状态 为 当前状态
			lastShowType = showType;
			
			$('#big_div').html(s);
			high_light();
			
			if(s.substring(77,110).indexOf('noimg.gif')!=-1){
				$('#tip_des').html('&lt;当前选择日期下没有比赛 请点击<a href="javascript:switch_type(\'m\')">按月查看</a>比赛&gt;');
			}else{
				$('#tip_des').html('');
			}
		},
		error:function(){
			// alert('出意外error');
		}
	};
	$.ajax(options);
}

function show_team(){
	if($('#div_team').css('display')=='none'){
		$('#a_team').removeClass('scclose').addClass('scopen');
		$('#div_team').show();
	}else{
		$('#a_team').removeClass('scopen').addClass('scclose');
		$('#div_team').hide();
	}
}

function hide_team(){
	$('#a_team').removeClass('scopen').addClass('scclose');
	$('#div_team').hide();
}

function all_team(f){
	if(f){
		$(':input[name="team"]').attr('checked','checked');
	}else{
		$(':input[name="team"]').removeAttr('checked');
		// $('#tip_team').text('全不选');
	}
}

function check_all_team(){
	all_team(true);
	$('#tip_team').text('全选');
	$('a[id^="a_team_"]').removeClass('scredon').addClass('scredover');
	$('#a_team_').removeClass('scredover').addClass('scredon');
	hide_team();
	team_id = null;
	// search_team(false);
	show_data(currentDate,null,null);
}

function check_one_team(aid){
	all_team(false);
	$('#tip_team').text($('#a_team_'+aid).text());
	$(':input[name="team"][value="'+aid+'"]').attr('checked','checked');
	
	$('a[id^="a_team_"]').removeClass('scredon').addClass('scredover');
	$('#a_team_'+aid).removeClass('scredover').addClass('scredon');
	
	hide_team();
	
	search_team(false);
}

function high_light(){
	$('#big_div tr[name="tr_title"]:even').css('background','#f1f1f1');
	// $('#big_div tr:even').css('background','blue');
	
	if(team_id==null)
		return;
	
	var a = team_id.split(',');
	for(var i in a){
		$('span[name="span_team_'+a[i]+'"]').addClass('scon');
	}
}

function a_selecter_team(id){
	if((team_id==null || team_id=='') && id.substr(7)=='')
		return;
	
	if(team_id==id.substr(7))
		return;

	$('#'+id).removeClass('scredon').addClass('scredover');
}

function show_table_table(id){
	$('table[id^="table_table_"]').hide();
	$('#table_table_'+id).show();
	table_table_id = id;
	$('span[id^="span_table_"]').removeClass('lion').addClass('lioff');
	$('#span_table_'+id).removeClass('lioff').addClass('lion');
}

function table_table_out(id){
	if(id==table_table_id)
		return;
	$('#span_table_'+id).removeClass('lion').addClass('lioff');
}

function set_tip(){
	if(showType=='s')
		$('#tip_d').text(currentDate.substr(0,4)+'-'+(parseInt(currentDate.substr(0,4),10)+1)+'赛季');
	else if(showType=='m')
		$('#tip_d').text(currentDate.substr(0,4)+'年'+currentDate.substr(4,2)+'月');
	else if(showType=='d')
		$('#tip_d').text(currentDate.substr(0,4)+'年'+currentDate.substr(4,2)+'月'+currentDate.substr(6,2)+'日');
	else if(showType=='w'){
		var dd = new Date(currentDate.substring(0,4) , parseInt(currentDate.substring(4,6), 10)-1, currentDate.substring(6,8));
		var start = new Date(dd.getTime()-(dd.getDay()-1)*1000*3600*24);
		var end = new Date(start.getTime()+1000*3600*24*7);
		start = start.getFullYear()+'年'+(parseInt(start.getMonth(), 10)+1)+'月'+start.getDate()+'日';
		end = end.getFullYear()+'年'+(parseInt(end.getMonth(), 10)+1)+'月'+end.getDate()+'日';
		$('#tip_d').text(start+' - '+end);
	}else if(showType=='r'){
		$('#tip_d').text('第'+turnNo+'轮');
	}
}