$(function() {
	
	$("#gameSite").change(function() {		
		if ($(this).val() == "-1") {
			$("#gameCity").removeOption(/./);
			$("#div_gameCity").hide("fast");
			$("#gameCity").remove();
		} else {
			if ($("#gameCity").length <= 0) {
				$("#div_gameCity").html('<label for="gameCity">Game Site (City):</label><select name="gameCity" id="gameCity" class="inputText"></select></div>');
			}
			$("#gameCity").removeOption(/./);
			$("#gameCity").ajaxAddOption("searchajaxfunctions.php?a=loadCityListBox&stateid=" + $(this).val(), {}, false, function() {
				$("#div_gameCity").show("fast");		
				$("#gameCity").selectedIndex = -1;	
			});
		}
	});
	
	$("input[name='resetbtn']").click(function() {
		window.location.href='index.php';
	});

	$("#searchform").submit(function() {
		$("body").css("display", "none");
		var filterString = "";
		//remove select boxes that are not used in filter to keep URL clean
		$("#searchform select").each(function() {
			if ($(this).val() == -1) {
				$(this).remove();
			} else {
				filterString += "&" + $(this).attr("ID") + "=" + $(this).val();
			}
		});
		//remove entry fields that are blank to keep URL clean
		$("#searchform input[type='text']").each(function() {
			if ($(this).val() == '') {
				$(this).remove();
			} else {
				var scrubbedVal = $(this).val().replace(/ /g, "-").replace(/,/g, "");
				filterString += "&" + $(this).attr("ID") + "=" + scrubbedVal;
			}
		});
		$("#searchform input[type='checkbox']:checked").each(function() {
			filterString += "&" + $(this).attr("ID") + "=" + $(this).val();
		});
		$("#searchform input[type='checkbox']:not(:checked)").each(function() {
			$(this).remove();
		});
		//Save filter set to database
		$.post("searchajaxfunctions.php?a=saveFilter" + filterString, {}, function (data, textStatus) {
			//clear out all form elements
			$("#searchform input[type='text']").each(function() {
				$(this).remove();
			});
			$("#searchform select").each(function() {
				$(this).remove();
			});
			//add a single form element containing the newly added (or found) filter ID
			$("#searchform").after('<form name="finalsearchform" id="finalsearchform" action="index.php" method="get"><input type="hidden" name="filter" value="' + data.filterId + '" /></form>');
			$("#finalsearchform").submit();			
		}, "json"); 
		return false;
	});
	
	$("#beginYear, #endYear").change(function() {
		setPartialDataVisibility();
	});
	
	$("#month").change(function() {
		buildMonthDiv();
	});
	
	$("#televised").change(function() {
		buildStationDiv();
	});
	
	setPartialDataVisibility();
	buildMonthDiv();
	buildStationDiv();
	
	var url = $.jqURL.url();
	$("#linkURL").val(url);
});
	
function setPartialDataVisibility() {
	if ($("#beginYear").val() < 1967 && $("beginYear").val() != -1 && $("#endYear").val() < 1967 && $("#endYear").val() != -1) {
		$("#div_qtr1").hide("fast");
		$("#div_qtr2").hide("fast");
		$("#div_qtr3").hide("fast");
		$("#div_qtr4").hide("fast");
		$("#div_attendance").hide("fast");
		$("#qtr1").val("-1");
		$("#qtr2").val("-1");
		$("#qtr3").val("-1");
		$("#qtr4").val("-1");
		$("#attendance").val("-1");
	} else {
		$("#div_qtr1").show("fast");
		$("#div_qtr2").show("fast");
		$("#div_qtr3").show("fast");
		$("#div_qtr4").show("fast");
		$("#div_attendance").show("fast");
	}
}

function buildMonthDiv() {
	if ($("#month").val() == 0) {
		$("#div_month").after("<div id='div_months'></div>");
		var qs = $.jqURL.qs();
		$("#div_months").load("searchajaxfunctions.php?" + qs + "&a=loadMonths");
	} else {
		$("#div_months").remove();
	}
}

function buildStationDiv() {
	if ($("#televised").val() == 2) {
		$("#div_televised").after("<div id='div_stations'></div>");
		var qs = $.jqURL.qs();
		$("#div_stations").load("searchajaxfunctions.php?" + qs + "&a=loadStations");
	} else {
		$("#div_stations").remove();
	}
}


