function clearSelect(select_obj) {
	for (var x = select_obj.length; x >= 0; x--) 
       select_obj.options[x] = null;
}

var players;

$(function() {
	var bioType = "";
	if (location.search.indexOf("type=premium") >= 0)
		bioType = "premium";
	else
		bioType = "all";
	
	var fileName = "/olympics/bios/"+bioType+"_bios.js";

    $.getJSON(fileName, function(json) {
		var select_obj;
		
		function nameSort(a, b) {
			if (a.name == undefined || b.name == undefined) return -1;
			else if (a.name < b.name) return -1;
			else if (a.name > b.name) return 1;
			else return 0;
		}

		json.bios.teams.sort(nameSort);
		select_obj = document.bio_nav.country;
		clearSelect(select_obj);
		select_obj.options[0] = new Option("All Countries", "");
		$.each(json.bios.teams,function(i,item) {
			if (item.id != undefined) {
				select_obj.options[select_obj.options.length] = new Option(item.name, item.id);
			}
		});
		$("#country_select").change( function() {
//		select_obj.onChange = new function() {
			var country = document.bio_nav.country.options[document.bio_nav.country_select.selectedIndex].value;
			var sport = document.bio_nav.page.options[document.bio_nav.sport_select.selectedIndex].value;
			var select_obj = document.bio_nav.id;

			if (country != "" || sport != "" || bioType == "premium") {
				clearSelect(select_obj);
				select_obj.options[select_obj.options.length] = new Option("Choose a Player", "");
				for (var x = 0; x < json.bios.players.length; x++) {
					if ((country == "" || country == json.bios.players[x].team_id) &&
					    (sport == "" || sport == json.bios.players[x].league_id))
						select_obj.options[select_obj.options.length] = new Option(json.bios.players[x].last_name+', '+json.bios.players[x].first_name, json.bios.players[x].global_id);
				}
			}
		});
		
		
		json.bios.leagues.sort(nameSort);
		select_obj = document.bio_nav.page;
		clearSelect(select_obj);
		select_obj.options[0] = new Option("All Sports", "");
		$.each(json.bios.leagues,function(i,item) {
			if (item.id != undefined) {
				select_obj.options[select_obj.options.length] = new Option(item.name, item.id);
			}
		});
		$("#sport_select").change( function() {
//		select_obj.onChange = new function() {
			var country = document.bio_nav.country.options[document.bio_nav.country_select.selectedIndex].value;
			var sport = document.bio_nav.page.options[document.bio_nav.sport_select.selectedIndex].value;
			var select_obj = document.bio_nav.id;

			if (country != "" || sport != "" || bioType == "premium") {
				clearSelect(select_obj);
				select_obj.options[select_obj.options.length] = new Option("Choose a Player", "");
				for (var x = 0; x < json.bios.players.length; x++) {
					if ((country == "" || country == json.bios.players[x].team_id) &&
					    (sport == "" || sport == json.bios.players[x].league_id))
						select_obj.options[select_obj.options.length] = new Option(json.bios.players[x].last_name+', '+json.bios.players[x].first_name, json.bios.players[x].global_id);
				}
			}
		});
		
		
		players = json.bios.players;
		select_obj = document.bio_nav.id;
		clearSelect(select_obj);
		$("#name_select").change( function() {
			this.form.submit();
		});
		
		if (bioType == "premium") {
			var country = document.bio_nav.country.options[document.bio_nav.country_select.selectedIndex].value;
			var sport = document.bio_nav.page.options[document.bio_nav.sport_select.selectedIndex].value;
			select_obj.options[select_obj.options.length] = new Option("Choose a Player", "");
			for (var x = 0; x < json.bios.players.length; x++) {
				if ((country == "" || country == json.bios.players[x].team_id) &&
				    (sport == "" || sport == json.bios.players[x].league_id))
					select_obj.options[select_obj.options.length] = new Option(json.bios.players[x].last_name+', '+json.bios.players[x].first_name, json.bios.players[x].global_id);
			}
		}
		else
			select_obj.options[select_obj.options.length] = new Option("Choose a Sport or Country", "");
		
		
		var findCountry = new RegExp('[?&]country=([^&]*)','i');
		select_obj = document.bio_nav.country;
		if (location.search.search(findCountry) >= 0) {
			var queryArr = location.search.match(findCountry);
			var selectedCountry = queryArr[1];
			for (var x = 0; x < select_obj.options.length; x++) {
				if (select_obj.options[x].value == selectedCountry) {
					select_obj.options[x].selected = true;
					$("#country_select").change();
					break;
				}
			}
		}
		
		var findSport = new RegExp('[?&]page=([^&]*)','i');
		select_obj = document.bio_nav.page;
		if (location.search.search(findSport) >= 0) {
			var queryArr = location.search.match(findSport);
			var selectedSport = queryArr[1];
			for (var x = 0; x < select_obj.options.length; x++) {
				if (select_obj.options[x].value == selectedSport) {
					select_obj.options[x].selected = true;
					$("#sport_select").change();
					break;
				}
			}
		}
		
		var findAthlete = new RegExp('[?&]id=([^&]*)','i');
		select_obj = document.bio_nav.id;
		if (location.search.search(findAthlete) >= 0) {
			var queryArr = location.search.match(findAthlete);
			var selectedSport = queryArr[1];
			for (var x = 0; x < select_obj.options.length; x++) {
				if (select_obj.options[x].value == selectedSport) {
					select_obj.options[x].selected = true;
					break;
				}
			}
		}
    });
});

