// using Prototype.js
// using Google Maps API, see: http://www.google.com/apis/maps/
// using In page script:
//    <script language="javascript">
//      var _defaultPlace = '<%=DefaultPlace%>';
//      Event.observe(window, 'load', init_GoogleMaps, true);
//    </script>


// Global Variables
///////////////////////////////////////////////////////////////
var _map;
var _places;

// Initialisation
///////////////////////////////////////////////////////////////
// Initialise the google map for the GetThere page
function init_GoogleMaps() {
  if (GBrowserIsCompatible()) {
    _map = new GMap2($('map')); // Create the map
    _places = createPlaceArray(); // Create an array of places to populate on the map
    _defaultPlace = (_places[_defaultPlace]) ? _defaultPlace : "enfield"; // Set a valid default place
    _map.addControl(new GLargeMapControl()); // Add Controls
    _map.addControl(new GScaleControl()); // Add Controls
    _map.addControl(new GOverviewMapControl()); // Add Controls
    _map.setCenter(_places[_defaultPlace].location, _places[_defaultPlace].defaultZoom); // Goto default place
    addAllMarkers(_places); // Add place markers
    _places[_defaultPlace].marker.openInfoWindowHtml(_places[_defaultPlace].details); // Opn window on default place
  }
  else {
    _map.innerHTML = "Sorry your browser does not support Google maps.";
  }
}

// Initialise the google map for the admin page
function init_GoogleMapsAdmin() {
  if ($("AdminMenu")) { // If AdminMenu does not exist then we are not logged in and so don't display the map
    if (GBrowserIsCompatible()) {
      var details = $('details');
      _map = new GMap2($('map')); // Create the map
      _map.addControl(new GLargeMapControl());
      _map.addControl(new GScaleControl());
      _map.setCenter(new GLatLng(51.6198, -0.01696), 13);
      GEvent.addListener(_map, 'click', function(overlay, point) {
        details.innerHTML = 'new GLatLng' + point;
      });
    }
    else {
      _map.innerHTML = "Sorry your browser does not support Google maps.";
    }
  }
}


// Events
///////////////////////////////////////////////////////////////

function gotoPlace(placeId) {
  if (GBrowserIsCompatible()) {
    _map.setCenter(_places[placeId].location, _places[placeId].defaultZoom);
    _places[placeId].marker.openInfoWindowHtml(_places[placeId].details);
  }
}


// Objects
///////////////////////////////////////////////////////////////

function Place(point, zoom, description) {
  if (GBrowserIsCompatible()) {
    this.location = point;
    this.details = description;
    this.marker = new GMarker(point);
    this.defaultZoom = zoom;
  }
}


// Methods
///////////////////////////////////////////////////////////////

function createPlaceArray() {
  var places = new Object(); // http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/
  places["chingford"] = new Place(new GLatLng(51.619848437625706, -0.016930103302001953), 13, "<div class=\"mapinfo\"><b>Wright Rhythm<\/b><br \/>Michael's Dance Studio,<br \/>1 Salisbury Road,<br \/>Chingford E4 6TA</div>");
  places["barnet"] = new Place(new GLatLng(51.64938159689346, -0.17494440078735352), 14, "<div class=\"mapinfo\"><b>Lyonsdown Church Hall</b><br \/>3, Lyonsdown Road,<br \/>New Barnet</div>");
  places["enfield"] = new Place(new GLatLng(51.64491956092291, -0.06787598133087158), 14, "<div class=\"mapinfo\" style=\"width:380px;\"><table><tr><td><img src=\"img/BushHillPark.jpg\" style=\"margin:0 10px 0 0;\" /></td><td><b>Wright Rhythm</b><br />The Bush Hill Park United Reformed Church Hall<br \>25 Main Avenue<br \>Bush Hill Park<br \>Enfield EN1 1DJ</td></tr></table></div>");
  places["enfield2"] = new Place(new GLatLng(51.65635587289016, -0.0470888614654541), 14, "<div class=\"mapinfo\"><b>Enfield Highway Community Centre</b><br />117 Hertford Rd<br />Enfield EN3 5JF</div>");
  places["haywardsheath"] = new Place(new GLatLng(51.00604566101299, -0.10973453521728516), 11, "<div class=\"mapinfo\"><b>The Dolphin Leisure Centre<\/b><br \/>Pasture Hill Road<br \/>Haywards Heath<br \/>West Sussex RH16 1LY</div>");
  places["gillingham"] = new Place(new GLatLng(51.390824475876485, 0.5414199829101562), 12, "<div class=\"mapinfo\"><b>Black Lion Leisure Centre<\/b><br \/>Mill Road<br \/>Gillingham<br \/>Kent ME7 1HF</div>");
  places["reading"] = new Place(new GLatLng(51.425691322071714, -0.932765007019043), 11, "<div class=\"mapinfo\"><b>Loddon Valley Leisure<\/b><br \/>Chalfont Close<br \/>Reading<br \/>Berkshire RG6 5HZ</div>");
  places["sittingbourne"] = new Place(new GLatLng(51.33890330473482, 0.7349199056625366), 12, "<div class=\"mapinfo\"><b>Swallows Leisure Centre<\/b><br \/>Central Avenue<br \/>Sittingbourne ME10 4NT</div>");
  places["wintergardens"] = new Place(new GLatLng(53.8169702497034, -3.0510127544403076), 14, "<div class=\"mapinfo\"><b>The Winter Gardens<\/b><br \/>97 Church St<br \/>Blackpool FY1 1HW</div>");
  places["cambersands"] = new Place(new GLatLng(50.93706677792504, 0.7934296131134033), 12, "<div class=\"mapinfo\"><b>Camber Sands Holiday Centre<\/b><br \/>New Lydd Road<br \/>Camber Sands<br \/>Nr. Rye<br \/>Sussex TN31 7RL</div>");
  places["rivoli"] = new Place(new GLatLng(51.454648722710076, -0.03754019737243652), 13, "<div class=\"mapinfo\"><b>The Rivoli Ballroom<\/b><br \/>350 Brockley Road<br \/>South East London SE4 2BY</div>");
  places["maidstone"] = new Place(new GLatLng(51.230699027852, 0.5193829536437988), 12, "<div class=\"mapinfo\"><b>Cornwallis School<\/b><br />Hubbards Lane<br />Linton<br \/>Maidstone<br \/>Kent<br \/>ME17 4HX</div>");
  places["farnborough"] = new Place(new GLatLng(51.30431237829641, -0.7657492160797119), 13, "<div class=\"mapinfo\"><b>Oak Farm Community School<\/b><br \/>Chaucer Rd<br \/>Farnborough<br \/>GU14 8SS</div>");
  places["prestatyn"] = new Place(new GLatLng(53.3419431640244, -3.4054183959960938), 12, "<div class=\"mapinfo\"><b>Prestatyn Sands Holiday Centre<\/b><br \/>Central Beach, Barkby Avenue,<br \/>Prestatyn, Denbighshire,<br />NORTH WALES, LL19 7LA.</div>");
  places["maidenhead"] = new Place(new GLatLng(51.52594064813257, -0.7177591323852539), 13, "<div class=\"mapinfo\"><b>Magnet Leisure Centre<\/b><br \/>Holmanleaze<br \/>Maidenhead SL6 8AW</div>");
  places["camberley"] = new Place(new GLatLng(51.302649,-0.720302), 13, "<div class=\"mapinfo\"><b>Lakeside Country Club<\/b><br />Frimley Green<br \/>Camberley<br \/>Surrey GU16 6PT</div>");
  
  return places;
}

function addMarker(place) {
  _map.addOverlay(place.marker);
  GEvent.addListener(place.marker, 'click', function() {
    place.marker.openInfoWindowHtml(place.details);
  });
}

function addAllMarkers(placeArray) {
  for (var placeKey in placeArray) { 
    addMarker(placeArray[placeKey]);
  }
}



