/*Inline Odds*/
var livescore_inlineodds = false;
function update_inline_odds_action(data){
	try{
		var init = false;
		if(!livescore_inlineodds){
			init = true;
			livescore_inlineodds = data;
		}
		if(data.h != livescore_inlineodds.h || init){
			for(var eventFK in data){
				if(!eventFK.match('^(0|[1-9][0-9]*)$')){continue;}
				if(typeof(livescore_inlineodds[eventFK]) === 'undefined' || livescore_inlineodds[eventFK].h != data[eventFK].h || init){
					if(!document.getElementById('e_' + eventFK)){continue;}
					$('#e_' + eventFK).find('td.odds').html(data[eventFK].odds);
				}
			}
			livescore_inlineodds = data;
		}
	}catch(e){
		
	}
	
	jTimeout('inline_odds', 1000*60, update_inline_odds);
}

function update_inline_odds(){
	if(typeof(PAGE_oddsProvider) !== 'undefined' && PAGE_oddsProvider != 0){
		$.getJSON('/data/inlineodds/'+PAGE_sportFK+'/'+session.site.sub_sitefk+'?output=json', update_inline_odds_action);
	}
}

var livescore_settings = {
	'date_format': 'd/m H:i'
}

var livescore_data = false;
var livescore_tmp_data = false;
var is_livescore_init = false;
var livescore_new_master = false;
var livescore_timer;
var livescore_interval = 5000;
var livescore_selected = {};



var livescore_xmlhttp_init = StandardXMLHttpRequest();
livescore_xmlhttp_init.onreadystatechange = proccess_update_init;

function stop_update(){if(livescore_xmlhttp.abort){livescore_xmlhttp.abort();}; clearTimeout(livescore_timer);}
function start_update(interval){
	interval=(interval)?interval:livescore_interval;
	livescore_interval = interval; livescore_timer = setTimeout(do_update, 10);
	jTimeout('inline_odds', 1000, update_inline_odds);
}

/*First get master update and incremental update and merge*/
function start_livescore_update(){
	var url = '/static/default/livescore/master.'+PAGE_sportFK+'.json?id=' + Math.random().toString().replace('.','');
	livescore_xmlhttp_init.open("GET", url , true);
	livescore_xmlhttp_init.send(null);
}

function proccess_update_init(){
	if(livescore_xmlhttp.readyState==4 || livescore_xmlhttp.readyState=='complete'){
		try{
			livescore_data = jQuery.parseJSON(livescore_xmlhttp.responseText);
			do_update(is_init);
		}catch(e){
			try_error(e);
			do_update();
		}
	}
}

function de(obj){
	$('#debug_box').append('<div>'+(new Date()).toString()+'</div><div>' + obj + '</div>');
}


var livescore_xmlhttp = StandardXMLHttpRequest();

function do_update(is_init){
	
	/*Get Master Update if initializing or Master File has changed*/
	if(!livescore_data || livescore_new_master){
		var url = '/static/default/livescore/master.'+PAGE_sportFK+'.json?id=' + Math.random().toString().replace('.','');
		livescore_new_master = false;
	/*Else get incremental update file*/
	}else{
		de('get update');
		var url = '/static/default/livescore/update.'+PAGE_sportFK+'.json?id=' + Math.random().toString().replace('.','');
		if(window.use_test_data === true){
			var url = '/js/default/test_data.js?_=' + Math.random().toString().replace('.','');
		}
	}
	
	/*If another request is running abort it*/
	if(livescore_xmlhttp.readyState == 1 ||livescore_xmlhttp.readyState == 2 || livescore_xmlhttp.readyState == 3){
		try{livescore_xmlhttp.abort();}catch(e){}
	}
	/*Send Request*/
	
	livescore_xmlhttp.open("GET", url , true);
	livescore_xmlhttp.onreadystatechange = proccess_update;
	livescore_xmlhttp.send(null);
	
	
}

function proccess_update(){
	clearTimeout(livescore_timer);
	
	if(livescore_xmlhttp.readyState==4 || livescore_xmlhttp.readyState=='complete'){
		
		
		
		//try_group('Proccess Update');
		//try_time('Proccess Time');
		var tmp_data = false;
		try{
			tmp_data = jQuery.parseJSON(livescore_xmlhttp.responseText);
		}catch(e){
			tmp_data = false;
		}
		
		/*Check if JSON data is valid*/
		if(livescore_xmlhttp.status != 200){
			try_warning('XMLHttpRequest status is not 200 (status='+livescore_xmlhttp.status+') - restarting update');
			livescore_timer = setTimeout(do_update, livescore_interval);
		}else if(tmp_data===false || tmp_data===null || tmp_data==={}){
			try_warning('Not JSON! - restarting update');
			livescore_timer = setTimeout(do_update, livescore_interval);
		}else if(!tmp_data.t || !tmp_data.ts){
			try_warning('Invalid JSON! - restarting update');
			livescore_timer = setTimeout(do_update, livescore_interval);
		}else{
			
			//try_debug('Valid JSON!');
			//try_debug(tmp_data);
			try{
				/*Check if Master has changed*/
				
				if(livescore_data.ts !== tmp_data.ts && livescore_data){
					livescore_new_master = true;
					livescore_data.ts = tmp_data.ts;
				}
				
				if(window.use_test_data === true){
					livescore_new_master = false;
					livescore_data.ts = tmp_data.ts;
				}
				/*Check if it is the first update*/
				if(!livescore_data){
					livescore_data = tmp_data;
					is_livescore_init = true;
					var update_data = livescore_data;
					var update_count = 1;
					
				}else{
					var update_data = {};
					var update_count = 0;
					
					for(var eventFK in tmp_data){
						
						if(!livescore_data[eventFK]){
							livescore_data[eventFK] = tmp_data[eventFK];
						}
						if(!eventFK.match('^(0|[1-9][0-9]*)$')){continue;}

						if(tmp_data[eventFK].h !== livescore_data[eventFK].h){
							/*livescore_data[eventFK] = tmp_data[eventFK];*/
							if(is_livescore_init){
								livescore_data[eventFK] = tmp_data[eventFK];
							}else{
								update_data[eventFK] = tmp_data[eventFK];
								update_count = update_count+1;
							}
						}
					}
					
					if(is_livescore_init){
						for(var eventFK in livescore_data){
							if(!eventFK.match('^(0|[1-9][0-9]*)$')){continue;}
							update_data[eventFK] = livescore_data[eventFK];
							update_count = update_count+1;
						}
						if(typeof(livescore_default_button) != 'undefined'){
							$('#' + livescore_default_button).trigger('click');
						}
					}
					//$('#update_status').html('RUN UPDATE');
					if(update_count){
						
						//try_debug(update_count + ' Updates!');
						window.tmp_do_filter = false;
						window.tmp_do_filter = livescore_dom_update(update_data, is_livescore_init);
						
						for(var eventFK in update_data){
							livescore_data[eventFK] = update_data[eventFK];
						}
						
						if(typeof(window.tmp_do_filter) != 'undefined' && window.tmp_do_filter && !is_livescore_init){
							//try_debug('Status change detected! Running filter events!');
							livescore_filter_events();
							window.tmp_do_filter = void(0);
						}
					}else{
						
						//try_debug('No Updates');
					}
					if(is_livescore_init){
						$('#filter-list li.invisible').removeClass('invisible');
						if(typeof(livescore_run_after_update)==='function'){livescore_run_after_update();}
					};
					is_livescore_init = false;
					update_data = null;
				}
			}catch(e){
				
				try_error(e);
				is_livescore_init = false;
			}
			
			livescore_timer = setTimeout(do_update, (is_livescore_init ? 100 : livescore_interval));
		}
	
		tmp_data = null;
		//try_time_end('Proccess Time');
		//try_group_end();
	}
	
}

function livescore_dom_update(data, init){
	var filterme = false;
	//var t = new Date();
	
	for(var eventFK in data){
		
		/*New Event Data*/
		var val = data[eventFK];
		/*Old Event Data*/
		var old = livescore_data[eventFK];
		
		try{
			livescore_dom_update_each(eventFK, val, old, init);
			//jQueue.add(livescore_dom_update_each, [eventFK, val, old, init]);
			//livescore_dom_update_each(eventFK, val, old, init);
			if(val.st !== old.st){
				filterme = true;
			}else{
				filterme = false;
			}
		}catch(e){
			try_warning(e);
		}
		
		
	}
	//try_info('Time: ' + (new Date()-t));
	return filterme;
	
}

function livescore_dom_update_each(eventFK, val, old, init){
	/*Exit function if row does not exist in DOM*/
	if(!document.getElementById('e_'+eventFK)){return false;}
	
	/*Event Row DOM*/
	var event_row = $('#e_' + eventFK);
	
	/*Check if sound is turned on and if event row is visible*/
	var snd_on = $('#audio_chooser').hasClass('active') && event_row.is(':visible');
	
	/*Check if sound is on PER row (soccerstand)*/
	if($('#audio_' + val.tsid).hasClass('active') && event_row.is(':visible')){snd_on = true;}
	
	/*Update status (only if changed or init)*/
	if(init || val.stid !== old.stid || val.st !== old.st){
		/*Update status Class on row*/
		
		event_row.removeClass('inprogress finished notstarted interrupted cancelled postponed stfk5').addClass(val.st);
		if(val.stid==5){
			event_row.addClass('stfk5');
		}
		
	
		
		if(val.st === 'notstarted' && val.stid != 5 && val.stid != 106 && val.stid != 12){
			
			var timezone = readCookie('custom_timezone');
			timezone=(timezone)?parseInt(timezone):0;
			
			/*Format event_date (if gbl_timezone var exists then use timezone widget settings to offset the date)*/
			if(typeof(gbl_timezone) !== 'undefined'){
				var format = event_row.find('.tz_field').attr('data-phpformat') || gbl_timezone[event_row.find('.tz_field').attr('data-format')];
				val.sd = $.PHPDate(format, new Date(val.sd.replace(/\-/igm, '/')));
			}else{
				val.sd = $.PHPDate('d/m H:i', new Date(val.sd.replace(/\-/igm, '/')));
			}
			event_row.find('.status').html(val.sd);
			
			event_row.find('.status_text').html(enet_labels.get_label('status_desc_1', 'Not Started'));
		}else{
			event_row.find('.status').removeClass('tz_field');
			
			
			var status_text = enet_labels.get_label('status_desc_' + ((val.stid) ? val.stid : '0'),'');
			
			
			if(typeof(short_status_desc) !== 'undefined' && typeof(short_status_desc['sd_'+((val.stid) ? val.stid : '0')]) !== 'undefined'){
				var short_status_text = short_status_desc['sd_'+((val.stid) ? val.stid : '0')];
				event_row.find('.short_status').removeClass('tz_field').html(short_status_text).attr('title', status_text);
			}
			
			
			if(status_text){
				event_row.find('.status, .status_text').html(status_text);
			}else{
				if(val.st === 'finished'){
					event_row.find('.status').html(enet_labels.get_label('status_desc_6', 'Finished'));
				}else if(val.st === 'inprogress'){
					event_row.find('.status').html( enet_labels.get_label('status_desc_45','Started') );
				}else if(val.st === 'cancelled'){
					event_row.find('.status').html('<b>' + enet_labels.get_label('status_desc_106','Cancelled') + '</b>');
				}else if(val.st === 'interrupted'){
					event_row.find('.status').html('<b>' + enet_labels.get_label('status_desc_12','Interrupted') + '</b><br/>' + val.sd);
				}else if(val.st === 'postponed'){
					event_row.find('.status').html('<b>' + enet_labels.get_label('status_desc_5','Postponed') + '</b>');
				}
			}
			
			if((val.stid !== old.stid || val.st !== old.st) && !init){
				highlight_dom(event_row.find('.status'), 1000, 'status_'+eventFK);
				filterme = true;
			}
			if(val.ip != old.ip && !init){
				filterme = true;
			}
		}
		if(val.st==='finished'){
			event_row.find('td.team_1, td.team_2, td.teams div.team_1, td.teams div.team_2').removeClass('winner').end()
			.find('td.team_' + val.w+', td.teams div.team_' + val.w).addClass('winner');
			if(val.wc){
				event_row.find('.status').html(event_row.find('.status').html() + '<br/><em>' + val.wc + '</em>');
			}
		}
		
		if(val.cm){
			event_row.find('.status').append('<div style="font-weight:normal; font-style:italic; font-size:10px;">'+val.cm+'</div>');
		}
	}/*End (init || val.stid !== old.stid)*/
	
	/*Run update per sport*/
	livescore_dom_update_sport(eventFK, val, old, event_row, init, snd_on);
}

function livescore_dom_update_sport(eventFK, val, old, event_row, init, sound){
	switch(PAGE_sport){
		case 'soccer':
			/*Update Status*/
			if(val.st == 'inprogress'){
				
				if(!val.el){
				}else if(val.stid != 10 && val.stid != 20 && val.stid != 14 && val.stid != 4){
					if( $('#gameCenter').data('eventFK') == eventFK ){
						
					}else{
						event_row.find('.status, .status_el').html(val.el);
					}
				}
			}
			
			var ht1 = (typeof(val.p1.ht) != 'undefined') ? val.p1.ht : '';
			var ht2 = (typeof(val.p2.ht) != 'undefined') ? val.p2.ht : '';
			
			if(ht1 !== ''){
				event_row.find('.halftime').html('('+ht1+'-'+ht2+')');
			}
			
			var et1 = (val.p1.et)?val.p1.et:0;
			var et2 = (val.p2.et)?val.p2.et:0;
			var et1_old = (old.p1.et)?old.p1.et:0;
			var et2_old = (old.p2.et)?old.p2.et:0;
			
			var score1 = score2 = old_score1 = old_score2 = 0;
			score1 = (livescore_settings.soccer_livescore==='ord') ? val.p1.ord+et1 : val.p1.rs;
			score2 = (livescore_settings.soccer_livescore==='ord') ? val.p2.ord+et2 : val.p2.rs;
			
			old_score1 = (livescore_settings.soccer_livescore==='ord') ? old.p1.ord+et1_old : old.p1.rs;
			old_score2 = (livescore_settings.soccer_livescore==='ord') ? old.p2.ord+et2_old : old.p2.rs;
			
			/*Update Result*/
			if(val.st !== 'notstarted' && val.st !== 'postponed' && val.st !== 'cancelled'){
				event_row
				.find('.team_1_result').html(score1).end()
				.find('.team_2_result').html(score2).end()
				.find('.result').html('<div>'+score1 + '-' + score2+'</div>');
			}else{
				event_row
				.find('.team_1_result, .team_2_result').html('').end()
				.find('.result').html('<div>-</div>');
			}
			
			/*Update Cards*/
			event_row.find('.team_1 span').html('');
			event_row.find('.team_2 span').html('');
	
			for(var t=1; t<=2; t=t+1){
				$.each(['y2c','rc'], function(k,card){
					for(var x=1; x<=val['p'+t][card];x=x+1){
						$('<div class="sprite10 card itfk_' + card + '"></div>').appendTo(event_row.find('.team_' + t + ' span'));
					}
				});
			}
			
			/*Sounds and highlight*/
			if(!init){
				/*Cards team1*/
				if(old.p1.rc < val.p1.rc || old.p1.y2c < val.p1.y2c){
					if(sound){ play_sound('/snd/default/card.mp3'); }
					highlight_dom(event_row.find('.team_1'), 10000, 'card_1_' + eventFK);
				}
				/*Cards team2*/
				if(old.p2.rc < val.p2.rc || old.p2.y2c < val.p2.y2c){
					if(sound){ play_sound('/snd/default/card.mp3'); }
					highlight_dom(event_row.find('.team_2'), 10000, 'card_2_' + eventFK);
				}
				
				
				/*Team 1 score*/
				if(old_score1 < score1 && val.st == 'inprogress'){
					if(sound){ play_sound('/snd/default/goal.mp3'); }
					highlight_dom(event_row.find('.result'), 10000, 'result_' + eventFK);
					highlight_dom(event_row.find('.team_1'), 10000, 'result_t1_' + eventFK);}
				
				/*Team 2 score*/
				if(old_score2 < score2 && val.st == 'inprogress'){
					if(sound){ play_sound('/snd/default/goal.mp3'); }
					highlight_dom(event_row.find('.result'), 10000, 'result_' + eventFK);
					highlight_dom(event_row.find('.team_2'), 10000, 'result_t2_' + eventFK);}
					
				/*Game Started*/
				if(old.st == 'notstarted' && val.st == 'inprogress'){
					if(sound){ play_sound('/snd/default/startEnd.mp3'); }
					highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
				/*Game Finished*/
				if(old.st == 'inprogress' && val.st == 'finished'){
					if(sound){play_sound('/snd/default/startEnd.mp3');}
					highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
			}
			break;
		case 'handball':
			/*Update Result*/
			var tmp = (event_row.find('.rs').length)?event_row.find('.rs'):event_row.find('.result');
			
			if(val.el && val.st === 'inprogress'){
				event_row.find('td.status').html(val.el);
			}
			
			if(val.st !== 'notstarted'){
				tmp.html('<div>'+val.p1.rs + '-' + val.p2.rs+'</div>');
			}else{
				tmp.html('<div>-</div>');
			}
			
			var ht1 = (typeof(val.p1.ht) != 'undefined') ? val.p1.ht : '';
			var ht2 = (typeof(val.p2.ht) != 'undefined') ? val.p2.ht : '';
			
			if(ht1 !== ''){
				event_row.find('.halftime').html('('+ht1+'-'+ht2+')');
			}
			
			/*Sounds and highlight*/
			if(old.p1.rs < val.p1.rs || old.p2.rs < val.p2.rs){
				//if(sound){ me.play_sound('/snd/default/goal.mp3'); }
				highlight_dom(event_row.find('.rs'), 10000, 'rs'+eventFK);
			}
			if(old.st == 'notstarted' && val.st == 'inprogress'){
				//if(sound){ me.play_sound('/snd/default/startEnd.mp3'); }
				if(sound){ play_sound('/snd/default/startEnd.mp3'); }
				highlight_dom(event_row.find('.status'), 10000, 'started'+eventFK);
				
			}
			if(old.st == 'inprogress' && val.st == 'finished'){
				//if(sound){ me.play_sound('/snd/default/startEnd.mp3'); }
				if(sound){ play_sound('/snd/default/startEnd.mp3'); }
				highlight_dom(event_row.find('.status'), 10000, 'ended'+eventFK);
			}
			
			break;
		case 'tennis':
			/*Update GAMESCORE AND SERV*/
			if(val.st !== 'notstarted'){
				/*Serv Change*/
				event_row.find('.serv').removeClass('serv0 serv1 serv2 serv3').addClass('serv' + val.serv);
				if(val.serv != old.serv && !init){highlight_dom(event_row.find('.serv'), 3000, 'serv_' + eventFK);}
				
				/*Gamescore*/
				var gs1 = gs2 = old_gs1 = old_gs2 = 0;
				gs1 = (val.p1.gamescore) ? val.p1.gamescore : 0; old_gs1 = (old.p1.gamescore) ? old.p1.gamescore : 0;
				gs2 = (val.p2.gamescore) ? val.p2.gamescore : 0; old_gs2 = (old.p2.gamescore) ? old.p2.gamescore : 0;
				
				if(typeof(val.p1.gamescore) != 'undefined' && typeof(val.p2.gamescore) != 'undefined'){
					event_row.find('.gamescore .team_1 td').html(gs1);
					event_row.find('.gamescore .team_2 td').html(gs2);
				}else{
					event_row.find('.gamescore .team_1 td').html('&nbsp;');
					event_row.find('.gamescore .team_2 td').html('&nbsp;');
				}
				if(gs1 != old_gs1 && !init){highlight_dom(event_row.find('.gamescore .team_1 td'), 4000, 'gs_1_' + eventFK);}
				if(gs2 != old_gs2 && !init){highlight_dom(event_row.find('.gamescore .team_2 td'), 4000, 'gs_2_' + eventFK);}
				/*Game Started*/
				if(old.st == 'notstarted' && val.st == 'inprogress'){
					if(sound){ play_sound('/snd/default/tennis.mp3'); }
					highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
				/*Game Finished*/
				if(old.st == 'inprogress' && val.st == 'finished'){
					if(sound){play_sound('/snd/default/tennis.mp3');}
					highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
			}
			
			var sets_var = ['s1','s2','s3','s4','s5'];
			var extra_wrap = '<span>[x]</span>';
			var extra_var = ['t1','t2','t3','t4','t5'];
			var score_var = 'sw';
			break;
		case 'icehockey':
			/*Game Started*/
			if(old.st == 'notstarted' && val.st == 'inprogress'){
				if(sound){ play_sound('/snd/default/icehockey.mp3'); }
				highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
			/*Game Finished*/
			if(old.st == 'inprogress' && val.st == 'finished'){
				if(sound){play_sound('/snd/default/icehockey.mp3');}
				highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
			var sets_var = ['p1','p2','p3','et','pn'];
			var score_var = 'rs';
			break;
		case 'basketball':
			var sets_var = ['q1','q2','q3','q4','et'];
			var sets_or = ['p1','p2'];
			var score_var = 'rs';
			
			/*Game Started*/
			if(old.st == 'notstarted' && val.st == 'inprogress'){
				if(sound){ play_sound('/snd/default/basketball.mp3'); }
				highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
			/*Game Finished*/
			if(old.st == 'inprogress' && val.st == 'finished'){
				if(sound){play_sound('/snd/default/basketball.mp3');}
				highlight_dom(event_row.find('.status'), 10000, 'status_' + eventFK);}
			break;
		case 'football':
			var sets_var = ['q1','q2','q3','q4','ot'];
			var score_var = 'rs';
			break;
		case 'aufootball':
			var sets_var = ['q1','q2','q3','q4','et'];
			var score_var = 'rs';
			break;
		case 'baseball':
			
			var score1 = score2 = old_score1 = old_score2 = 0;
			score1 = val.p1.rs || 0;
			score2 = val.p2.rs || 0;
			old_score1 = old.p1.rs || 0;
			old_score2 = old.p2.rs || 0;
			if(val.st !== 'notstarted'){
				event_row.find('.runningscore_1').html(score1);
				event_row.find('.runningscore_2').html(score2);
			}
			var sets_var = ['i1','i2','i3','i4','i5','i6','i7','i8','i9','ei','rs','hit','er'];
			break;
		case 'cricket':
			var sets_var = ['tp_1','tp_2','tov'];
			var extra_wrap = '-[x]';
			var extra_var = ['w_1','w_2'];
			var score_var = 'tp';
			break;
		case 'rugby':
			var sets_var = ['h1','h2','et'];
			var score_var = 'rs';
			break;
		case 'badminton':
			var sets_var = ['s1','s2','s3','s4','s5'];
			var score_var = 'sw';
			break;
		case 'volleyball':
			var sets_var = ['s1','s2','s3','s4','s5','s6'];
			var score_var = 'rs';
			break;
		case 'nrl':
			var sets_var = ['h1','h2','et'];
			var score_var = 'rs';
			break;
			
		
	}/*END: Switch(PAGE_sport)*/

	
	/*Dynamic "set" update*/
	if(val.st !== 'notstarted'){
		
		/*SETS*/
		if(sets_var){
			/*Update sets*/
			$.each(sets_var, function(index,value){
				if(typeof(val['p1'][value]) !== 'undefined'){
					var value1 = (val['p1'][value])?val['p1'][value]:0;
					var value2 = (val['p2'][value])?val['p2'][value]:0;
					var old_value1 = (old['p1'][value])?old['p1'][value]:0;
					var old_value2 = (old['p2'][value])?old['p2'][value]:0;
				}else if(typeof(sets_or) !== 'undefined' && sets_or[index] && typeof(val['p1'][sets_or[index]]) != 'undefined'){
					var value1 = (val['p1'][sets_or[index]])?val['p1'][sets_or[index]]:0;
					var value2 = (val['p2'][sets_or[index]])?val['p2'][sets_or[index]]:0;
					var old_value1 = (old['p1'][sets_or[index]])?old['p1'][sets_or[index]]:0;
					var old_value2 = (old['p2'][sets_or[index]])?old['p2'][sets_or[index]]:0;
				}
				
				
				
				if(typeof(value1) !== 'undefined'){
					var tmp_cell_1 = $(event_row.find('.rs_cell .team_1 td').get(index));
					var tmp_cell_2 = $(event_row.find('.rs_cell .team_2 td').get(index));
					
					if(extra_var){
						var xvalue1 = (val['p1'][extra_var[index]])?val['p1'][extra_var[index]]:0;
						var xvalue2 = (val['p2'][extra_var[index]])?val['p2'][extra_var[index]]:0;
						var xold_value1 = (old['p1'][extra_var[index]])?old['p1'][extra_var[index]]:0;
						var xold_value2 = (old['p2'][extra_var[index]])?old['p2'][extra_var[index]]:0;
						
						if(xvalue1 > xold_value1){ highlight_dom(tmp_cell_1, 4000, 'val_'+index+'_1_'+eventFK); }
						if(xvalue2 > xold_value2){ highlight_dom(tmp_cell_2, 4000, 'val_'+index+'_2_'+eventFK); }
						xvalue1 = (!xvalue1)?'':extra_wrap.replace('[x]', xvalue1);
						xvalue2 = (!xvalue2)?'':extra_wrap.replace('[x]', xvalue2);
						
					}else{
						var xvalue1 = xvalue2 = '';
					}
					
					if(!value1&&!value2){value1=value2='-';}
					
					tmp_cell_1.html(value1+xvalue1); tmp_cell_2.html(value2+xvalue2);
					
					if(value1 > old_value1){ highlight_dom(tmp_cell_1, 10000, 'val_'+index+'_1_'+eventFK); }
					if(value2 > old_value2){ highlight_dom(tmp_cell_2, 10000, 'val_'+index+'_2_'+eventFK); }
				}
			});
		}
		/*SCORE*/
		if(score_var){
			/*Update Score*/
			var rs1 = rs2 = old_rs1 = old_rs2 = 0;
			rs1 = (val.p1[score_var]) ? val.p1[score_var] : 0; old_rs1 = (old.p1[score_var]) ? old.p1[score_var] : 0;
			rs2 = (val.p2[score_var]) ? val.p2[score_var] : 0; old_rs2 = (old.p2[score_var]) ? old.p2[score_var] : 0;
			event_row.find('.score .team_1 td').html(rs1);
			event_row.find('.score .team_2 td').html(rs2);
			if(rs1>old_rs1){highlight_dom(event_row.find('.score .team_1 td'), 10000, 'rs_1_' + eventFK);}
			if(rs2>old_rs2){highlight_dom(event_row.find('.score .team_2 td'), 10000, 'rs_2_' + eventFK);}
		}
	}
}

function livescore_filter_events(){
	var status_array = [];
	if($('#filter_button_livestats').hasClass('active')){status_array[status_array.length] = 'livestats';}
	if($('#filter_button_selected').hasClass('active')){status_array[status_array.length] = 'selected';}
	if($('#filter_button_inprogress').hasClass('active')){status_array[status_array.length] = 'inprogress';}
	if($('#filter_button_finished').hasClass('active')){status_array[status_array.length] = 'finished';}
	if($('#filter_button_notstarted').hasClass('active')){
		status_array[status_array.length] = 'notstarted'; 
		status_array[status_array.length] = 'interrupted'; 
		status_array[status_array.length] = 'postponed'; 
	}
	
	var c_array = [];
	$('#country_checkboxes input:checked').each(function(){
		if( $(this).val() === 'ALL'){
			$('div.LeagueContainer').show();
		}else{
			c_array.push($(this).val());
		}
	});
	
	
	$.each(livescore_data, function(eventFK, val){
		if(status_array.length === 0 || String(status_array).indexOf(val.st) > -1 || (String(status_array).indexOf('inprogress') > -1 && val.ip) || (String(status_array).indexOf('livestats') > -1 && val.ls) || (String(status_array).indexOf('selected') > -1 && livescore_selected['e_' + eventFK])){
			$('#e_' + eventFK).show();
		}else{
			$('#e_' + eventFK).hide();
		}
	});
	
	
	$('div.LeagueContainer, li.LeagueContainer').each(function(){
		if($('#check_all').is(':checked') || !$('#check_all').length){
			$(this).show();
		}else if($('#check_' + $(this).attr('data-cid')).is(':checked')){
			$(this).show();
		}else{
			$(this).hide();
		}
		if($(this).find('.event_row:visible').length === 0){
			$(this).hide();
		}
	});
	
	if(livescore_settings.odd_rows_filter){
		$('table.default tbody', '#' + me.UID).find('tr.event_row').removeClass('odd').end().find('tr.event_row:visible:odd').addClass('odd');
	}
	//resize_iframe();
	
	if($('.event_row:visible').length === 0 || ($('div.LeagueContainer:visible').length === 0 && $('div.LeagueContainer').length > 0)){
		if( !$('.no_events').length ){
			$('#no_events_found').show();
		}
	}else{
		$('#no_events_found').hide();
	}
	if(typeof(livescore_odd_rows)==='function'){livescore_odd_rows();}
	
}
