private function IndexGrounds()
 {
     require_once 'stoolball/ground-manager.class.php';
     require_once "stoolball/team-manager.class.php";
     require_once 'search/ground-search-adapter.class.php';
     $this->SearchIndexer()->DeleteFromIndexByType("ground");
     $manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
     $manager->FilterByActive(true);
     $manager->ReadAll();
     $grounds = $manager->GetItems();
     unset($manager);
     $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
     foreach ($grounds as $ground) {
         /* @var $ground Ground */
         # Get teams based at the ground
         $team_manager->FilterByGround(array($ground->GetId()));
         $team_manager->FilterByActive(true);
         $team_manager->ReadTeamSummaries();
         $teams = $team_manager->GetItems();
         $ground->Teams()->SetItems($teams);
         $adapter = new GroundSearchAdapter($ground);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
     }
     $this->SearchIndexer()->CommitChanges();
     unset($team_manager);
 }
    public function OnLoadPageData()
    {
        require_once 'stoolball/ground-manager.class.php';
        $manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
        $manager->FilterByActive(true);
        # Check for team type
        if (isset($_GET["team-type"])) {
            # Sanitise input to ensure we work only with integers
            $team_types = explode(",", $_GET['team-type']);
            foreach ($team_types as $key => $value) {
                if (!is_numeric($value)) {
                    unset($team_types[$key]);
                } else {
                    $team_types[$key] = (int) $team_types[$key];
                }
            }
            if (count($team_types)) {
                $manager->FilterByTeamType($team_types);
            }
        }
        # Check for player type
        $player_type = null;
        if (isset($_GET['player']) and is_numeric($_GET['player'])) {
            $player_type = (int) $_GET['player'];
        }
        if ($player_type === 0) {
            $manager->FilterByActive(false);
        } else {
            if (!is_null($player_type)) {
                $a_player_types = is_null($player_type) ? null : array($player_type);
                if ($player_type == PlayerType::JUNIOR_MIXED) {
                    $a_player_types[] = PlayerType::GIRLS;
                    $a_player_types[] = PlayerType::BOYS;
                }
                $manager->FilterByPlayerType($a_player_types);
            }
        }
        $manager->ReadAll();
        $grounds = $manager->GetItems();
        unset($manager);
        # JavaScript will create two sets of markers, one for each ground, and one for each team.
        # This is so that, when clustered, we can display the number of teams by creating a marker for each (even though they're actually duplicates).
        # You can't get an infoWindow for a cluster though, so once we're zoomed in far enough switch to using the ground markers, which are unique.
        ?>
$(function() {
// Make the placeholder big enough for a map
var mapControl = document.getElementById("map");
mapControl.style.width = '100%';
mapControl.style.height = '600px'; // Create the map var
myLatlng = new google.maps.LatLng(51.8157917, -0.9166621); // Aylesbury
var myOptions = { zoom : 6, center : myLatlng, mapTypeId : google.maps.MapTypeId.ROADMAP };
var map = new google.maps.Map(mapControl, myOptions);
var groundMarkers = [], teamMarkers = [];
var info;
var clusterer;
var previousZoom = map.getZoom();

function createGroundMarkers() {

		<?php 
        foreach ($grounds as $ground) {
            /* @var $ground Ground */
            if (!$ground->GetAddress()->GetLatitude() or !$ground->GetAddress()->GetLongitude()) {
                continue;
            }
            $content = $this->InfoWindowContent($ground);
            echo $this->MarkerScript($ground, $content, "groundMarkers");
        }
        ?>
}

function createTeamMarkers() {

        <?php 
        foreach ($grounds as $ground) {
            /* @var $ground Ground */
            if (!$ground->GetAddress()->GetLatitude() or !$ground->GetAddress()->GetLongitude()) {
                continue;
            }
            $content = $this->InfoWindowContent($ground);
            $teams = $ground->Teams()->GetItems();
            $length = count($teams);
            for ($i = 0; $i < $length; $i++) {
                echo $this->MarkerScript($ground, $content, "teamMarkers");
            }
        }
        ?>
}

function removeMarkers(removeMarkers) {
    var length = removeMarkers.length;
    for (var i = 0; i < length; i++) {
        removeMarkers[i].setMap(null);
    }
}

function plotMarkers(addMarkers) {    
    var style = [{ url: '/images/features/map-markers.gif', height: 67, width: 31, textColor: '#ffffff', textSize: 10 }];
    if (clusterer) clusterer.clearMarkers();
    clusterer = new MarkerClusterer(map, addMarkers, { 'gridSize': 30, styles: style});
}

function zoomChanged() {
    var currentZoom = map.getZoom();
    if (previousZoom < 14 && currentZoom >= 14) {
        removeMarkers(teamMarkers);
        plotMarkers(groundMarkers);
    } else if (previousZoom >= 14 && currentZoom < 14) {
        removeMarkers(groundMarkers);
        plotMarkers(teamMarkers);
    }
    previousZoom = currentZoom;
}

google.maps.event.addListener(map, 'zoom_changed', zoomChanged);

createGroundMarkers();
createTeamMarkers();
plotMarkers(teamMarkers);
});
		<?php 
        exit;
    }