$(document).ready(function() {

	SuperDuperHide();

	$('#ListingType, #GroupId, #SchoolId, #CoachingStatus, #SportId').change(function() {
		SuperDuperHide();
	});

	function SuperDuperHide() {
		if ($('#ListingType').val()) {
			if ($('#ListingType').val() == 'I') {
				$('#GroupsDisplay').hide();
				$('#SchoolsDisplay').show();
				$('#GroupId').val('');
			} else {
				$('#GroupsDisplay').show();
				$('#SchoolsDisplay').hide();
			}
		} else {
			$('#GroupsDisplay').hide();
			$('#SchoolsDisplay').hide();
			$('#GroupId').val('');
			$('#SchoolId').val('');
		}

		if ($('#ListingType').val() == 'I' || ($('#ListingType').val() == 'G' && $('#GroupId').val())) {
			$('#SchoolsDisplay').show();
		} else {
			$('#SchoolsDisplay').hide();
			$('#SchoolId').val('');
		}

		if ($('#SchoolId').val()) {
			$('#StatusDisplay').show();
		} else {
			$('#StatusDisplay').hide();
			$('#CoachingStatus').val('');
		}

		if ($('#CoachingStatus').val()) {
			$('#SportsDisplay').show();
		} else {
			$('#SportsDisplay').hide();
			$('#SportId').val('');
		}

		if ($('#SportId').val()) {
			$('#YearsDisplay').show();
		} else {
			$('#YearsDisplay').hide();
			$('#YearsCoaching').val('');
		}
	}

	// Run these when page loads
	CoachingStatus();
	LoadSchools(true);

	// Show/hide "Other" field for coaching status
	$('#CoachingStatus').change(function() {
		CoachingStatus();
	});

	// Auto-load school selection into form, based upon district ID
	$('#GroupId').change(function() {
		LoadSchools(false);
	});

	// Auto-load district selection into form, based upon listing type
	$('#ListingType').change(function() {
		// Load districts based upon listing type
		LoadSchools(false);

		// Load empty school selection
		var data = "<option value=''>-</option>";
		$('#SchoolId').html(data);
	});

	/**
	 * Show/Hide "Other" field for Coaching Status
	 */
	function CoachingStatus() {
		if ($('#CoachingStatus').val() == 'C') {
			$('#CoachingStatusSpecific').show();
		} else {
			$('#CoachingStatusSpecific').hide();
		}
	}

    /**
     * Load school names into selection form
     */
    function LoadSchools(pageLoad) {
        // Prepare to build the URL
        var urlBuild = '';

        // Data to inject
        var data;

        // Coach doesn't belong to a group
        if ($('#ListingType').val() == 'I') {
			// Hide groups
			$('#GroupsDisplay').hide();
            $('#ListingTypeI').show();
            $('#ListingTypeG').hide();

            // Build URL with school choice included
            if (pageLoad && $('#SchoolChoice').val()) {
                urlBuild = '?groupid=N&schoolid=' + $('#SchoolChoice').val();
                $('#SchoolChoice').val('');
            // Don't include school choice
            } else if ($('#GroupId').val()) {
                urlBuild = '?groupid=N';
            }
        // Coach belongs to a group; display groups
		} else {
			// Show groups
			if ($('#ListingType').val() == 'G') {
				$('#GroupsDisplay').show();
				$('#ListingTypeI').hide();
				$('#ListingTypeG').show();
			}

            // Build URL with school choice included
            if (pageLoad && $('#SchoolChoice').val()) {
                urlBuild = '?groupid=' + $('#GroupId').val() + '&schoolid=' + $('#SchoolChoice').val();
                $('#SchoolChoice').val('');
            // Don't include school choice
            } else if ($('#GroupId').val()) {
                urlBuild = '?groupid=' + $('#GroupId').val();
            }
		}

		// Load Result
		$.ajax({
			url: 'ajax/populate-schools.php' + urlBuild,
			success: function(data) {
				$('#SchoolId').html(data);
				$('#CoachingStatus').change();
			}
		});
    }

	// Helps prevent double submissions
	$('.submit').click(function() {
		$(this).hide();
		$(this).after('<p class="loading">Loading...</p>');
	});

	// Open rel="external" in new window
	$('a[rel="external"]').attr('target','_blank');

	// Print page
	$('.print').click(function() {
		window.print();
		return false;
	});

});
