var all_list_match = null;

function refreshLivescore(){
		$.ajax({
		url: "/inc/home/_livescore_match.shtml",
		cache: false,
		dataType: "json",
		success: function(data) {
		var consoleText = "";
		all_list_match = Array();
		//alert("success "+data.match);
		if(data.match != null){
			$.each(data.match, 
				function(i,item){
					if(item.status == null)
						return;
					last_match_event = new Array();
					status_result = item.status;
					typeResult = item.risultato;
					id = item.id;
					time = item.time;
					home = item.home;
					homeScore = item.home_score;
					away = item.away;
					awayScore = item.away_score;
					competition_id = item.competition_id;

					if(item.home_goal != null){
						$.each(item.home_goal.event,function(j,item_){
							last_match_event.push({"event_time":item_.time,"event_player":item_.player,"event_team":"home","event_number":item_.event_number});
						});
					}
					
					if(item.away_goal != null){			
						$.each(item.away_goal.event,function(j,item_){
							last_match_event.push({"event_time":item_.time,"event_player":item_.player,"event_team":"away","event_number":item_.event_number});
						});
					}
					
					last_match_event.sort(sortNumber);

					updateMatch(status_result,typeResult,id,time,home,homeScore,away,awayScore,last_match_event,competition_id);
				}
			);
		}
    }});
	setTimeout("resetElement()",15000);
	setTimeout("refreshLivescore()",60000);
}

function updateMatch(status_result,typeResult,id,time,home,homeScore,away,awayScore,last_match_event,competition_id){
	
	if(status_result != 'PreMatch'){
		if(time != '')
			$("#status_header_"+id).html(time+"'");
		$("#match_header_"+id+" ul:eq(0) li:eq(0)").html(home);
		$("#match_header_"+id+" ul:eq(0) li:eq(1)").html(homeScore);
		$("#match_header_"+id+" ul:eq(1) li:eq(0)").html(away);
		$("#match_header_"+id+" ul:eq(1) li:eq(1)").html(awayScore);
		if(status_result == 'HalfTime'){//intervallo
			$("#match_header_"+ id +" ul:eq(0) li:eq(0)").attr('class','club');//rispristino colore alla sq di casa
			$("#match_header_"+ id +" ul:eq(0) li:eq(1)").attr('class','score');//rispristino colore alla sq di casa (score)
			$("#match_header_"+ id +" ul:eq(1) li:eq(0)").attr('class','club');//rispristino colore alla sq ospite
			$("#match_header_"+ id +" ul:eq(1) li:eq(1)").attr('class','score');//rispristino colore alla sq ospite (score)
			$("#match_header_"+ id +" span").attr('class','opta_col_3');
			$("#match_header_"+ id +" span").html("FINE PRIMO TEMPO");
		}
		else if(status_result == 'FullTime'){//partita terminata
			$("#match_header_"+ id +" ul:eq(0) li:eq(0)").attr('class','club');//rispristino colore alla sq di casa
			$("#match_header_"+ id +" ul:eq(0) li:eq(1)").attr('class','score');//rispristino colore alla sq di casa (score)
			$("#match_header_"+ id +" ul:eq(1) li:eq(0)").attr('class','club');//rispristino colore alla sq ospite
			$("#match_header_"+ id +" ul:eq(1) li:eq(1)").attr('class','score');//rispristino colore alla sq ospite (score)
			$("#match_header_"+ id +" span").attr('class','opta_col_1');
			$("#match_header_"+ id +" span").html("TERMINATA");
			$("#status_header_"+ id).html("");
		}
		else{
			if(status_result == 'FirstHalf'){
				$("#match_header_"+ id +" span").attr('class','opta_col_2');
				$("#match_header_"+ id +" span").html("IN CORSO 1&deg;T");
			}
			else if(status_result == 'SecondHalf'){
				$("#match_header_"+ id +" span").attr('class','opta_col_2');
				$("#match_header_"+ id +" span").html("IN CORSO 2&deg;T");			
			}
			
			if(last_match_event.length > 0 ){
				if((time - last_match_event[0]["event_time"]) <= 5){//se sto entro i 5 minuti mostro i marcatori
					if(last_match_event[0]["event_team"] == 'home'){
						$("#match_header_"+ id +" ul:eq(0) li:eq(0)").attr('class','club opta_col_2');//cambio colore alla sq di casa
						$("#match_header_"+ id +" ul:eq(0) li:eq(1)").attr('class','score opta_col_2');//cambio colore alla sq di casa (score)
					}else if(last_match_event[0]["event_team"] == 'away'){
						$("#match_header_"+ id +" ul:eq(1) li:eq(0)").attr('class','club opta_col_2');//cambio colore alla sq ospite
						$("#match_header_"+ id +" ul:eq(1) li:eq(1)").attr('class','score opta_col_2');//cambio colore alla sq ospite (score)
					}
					$("#match_header_"+ id +" span").attr('class','opta_col_2');
					$("#match_header_"+ id +" span").html(last_match_event[0]["event_player"] + "&nbsp;" + last_match_event[0]["event_time"] + "'");
					if($.cookie(last_match_event[0]["event_number"]) == null){
						parentHtml = $("#match_header_"+ id).parent().html();
						href_attr = $("#match_header_"+id+" a:first").attr("href");//link del match
						all_list_match.push({"id":id,"parentHtml":parentHtml,"time":time});//inserisco nell'array gli elementi da ripristinare
						$("#match_header_"+ id).parent().html(parentHtml + "<a href=\""+href_attr+"\"><div class=\"gol_surprise\"><span>"+last_match_event[0]["event_player"]+"&nbsp;"+last_match_event[0]["event_time"]+"'</span></div></a>");//mostro il goal
						$.cookie(last_match_event[0]["event_number"], "true");
					}
				}else{//se sto oltre i 5 minuti tolgo i marcatori
					$("#match_header_"+ id +" ul:eq(0) li:eq(0)").attr('class','club');//rispristino colore alla sq di casa
					$("#match_header_"+ id +" ul:eq(0) li:eq(1)").attr('class','score');//rispristino colore alla sq di casa (score)
					$("#match_header_"+ id +" ul:eq(1) li:eq(0)").attr('class','club');//rispristino colore alla sq ospite
					$("#match_header_"+ id +" ul:eq(1) li:eq(1)").attr('class','score');//rispristino colore alla sq ospite (score)
					if(status_result == 'FirstHalf'){
						$("#match_header_"+ id +" span").attr('class','opta_col_2');
						$("#match_header_"+ id +" span").html("IN CORSO 1&deg;T");
					}
					else if(status_result == 'SecondHalf'){
						$("#match_header_"+ id +" span").attr('class','opta_col_2');
						$("#match_header_"+ id +" span").html("IN CORSO 2&deg;T");			
					}
					//$("#match_header_"+ id +" span").attr('class','opta_col_1');//1 rosso, 2 verde, 3 arancio
					//$("#match_header_"+ id +" span").html("");
				}
			}
		}
	}else{
		$("#status_header_"+id).html("");
		if(typeResult == 'Postponed'){
			$("#match_header_"+ id +" span").attr('class','opta_col_1');
			$("#match_header_"+ id +" span").html("PARTITA RINVIATA");
		}else if(typeResult == 'Abandoned'){
			$("#match_header_"+ id +" span").attr('class','opta_col_1');
			$("#match_header_"+ id +" span").html("PARTITA SOSPESA");
		}else{
			$("#match_header_"+ id +" span").attr('class','opta_col_1');
			$("#match_header_"+ id +" span").html("");
		}
	}
	if(competition_id =='105')
		$("#status_header_"+id).html("");
}

function resetElement(){
	if(all_list_match != null){
		$.each(all_list_match,function(i,item){
			$("#match_header_"+ item.id).parent().html(item.parentHtml);
			$("#status_header_"+item.id).html(item.time+"'");
		});
	}
}
