$(document).ready(function(){
	var loc = window.location.protocol + "//" + window.location.host + "/";
	if (window.location.host == "localhost") { loc += "betscout/html/" };

	var page = "livescore";
	
	// Hide all but sport select
	$('#livescore_select_country').hide();
	$('#livescore_select_league').hide();
	
	// Add options to sport and time
	$("#livescore_select_sport").ajaxAddOption(loc+"ajax/boxvalues/"+page+"/not_set/not_set/not_set/1",{},false);
	
	$('#livescore_select_sport').change(function(){
		var sport_id = ($("select#livescore_select_sport").val() != "") ? $("select#livescore_select_sport").val() : "not_set";
		// var country_id = ($("select#livescore_select_country").val() != "") ? $("select#livescore_select_country").val() : "not_set";
		// var league_id = ($("select#livescore_select_league").val() != "") ? $("select#livescore_select_league").val() : "not_set";
		var country_id = "not_set";
		var league_id = "not_set";
		var string = page+'/'+sport_id+'/'+country_id+'/'+league_id;
		
		$(".tableloading.livescore").show();
		
		// Hide all but sport select
		$('#livescore_select_country').hide();
		$('#livescore_select_league').hide();

		$("#livescore_select_country").removeOption(/./);
		$("#livescore_select_league").removeOption(/./);
		$("#livescore_select_country").ajaxAddOption(loc+"ajax/boxvalues/"+string,{},false);
		
		$("#livetable").load(loc+"ajax/tablevalues/"+string, '', function() {
			$(".tableloading.livescore").hide();
			
			if (sport_id != 'not_set') {
				// Show the next select
				$("#livescore_select_country").show();
			}
		});
	});
	
	$('#livescore_select_country').change(function(){
		var sport_id = ($("select#livescore_select_sport").val() != "") ? $("select#livescore_select_sport").val() : "not_set";
		var country_id = ($("select#livescore_select_country").val() != "") ? $("select#livescore_select_country").val() : "not_set";
		// var league_id = ($("select#livescore_select_league").val() != "") ? $("select#livescore_select_league").val() : "not_set";
		var league_id = "not_set";
		var string = page+'/'+sport_id+'/'+country_id+'/'+league_id;
		
		$(".tableloading.livescore").show();
		
		// Hide all but sport select
		$('#livescore_select_league').hide();
		
		$("#livescore_select_league").removeOption(/./);
		$("#livescore_select_league").ajaxAddOption(loc+"ajax/boxvalues/"+string,{},false);
		
		$("#livetable").load(loc+"ajax/tablevalues/"+string, '', function() {
			
			$(".tableloading.livescore").hide();
			
			// Show the next select
			$("#livescore_select_league").show();
			
			
		});
	});
	
	$('#livescore_select_league').change(function(){
		var sport_id = ($("select#livescore_select_sport").val() != "") ? $("select#livescore_select_sport").val() : "not_set";
		var country_id = ($("select#livescore_select_country").val() != "") ? $("select#livescore_select_country").val() : "not_set";
		var league_id = ($("select#livescore_select_league").val() != "") ? $("select#livescore_select_league").val() : "not_set";
		var string = page+'/'+sport_id+'/'+country_id+'/'+league_id;
		
		$(".tableloading.livescore").show();
		
		$("#livetable").load(loc+"ajax/tablevalues/"+string, '', function() {
			$(".tableloading.livescore").hide();
		});
	});
	
	update();
});

function update() {
	var loc = window.location.protocol + "//" + window.location.host + "/";
	if (window.location.host == "localhost") { loc += "betscout/html/" };

	var page = "livescore";

	var sport_id = ($("select#livescore_select_sport").val() != "") ? $("select#livescore_select_sport").val() : "not_set";
	var country_id = ($("select#livescore_select_country").val() != "") ? $("select#livescore_select_country").val() : "not_set";
	var league_id = ($("select#livescore_select_league").val() != "") ? $("select#livescore_select_league").val() : "not_set";
	var string = page+'/'+sport_id+'/'+country_id+'/'+league_id;
	
	$(".tableloading.livescore").show();
	
	$('#livetable').load(loc+'ajax/tablevalues/'+string, '', function() {
		$(".tableloading.livescore").hide();
		setTimeout('update();', 5000); //Refresh every 10 seconds
	});
}
