/**
 * Wrapper around document.getElementById().
 */
function $(id) {
  return document.getElementById(id);
}

//initialize global gmap variables
var geocoder = null;
var mycontrol=null;
var mytypecontrol=null;
var map=null;
var gxmarkerjs=0;
var gmapMarkerLocation="/modules/gmap/markers";
var markerlink=0;
var geoXml = new GGeoXml("http://corsica.forhikers.com/files/kml/bus-routes.kml");

var baseIcon = new GIcon();
baseIcon.iconSize=new GSize(32,32);
baseIcon.shadowSize=new GSize(56,32);
baseIcon.iconAnchor=new GPoint(16,32);
baseIcon.infoWindowAnchor=new GPoint(16,0);

var gicons = [];
gicons["bus"] = new GIcon(baseIcon, "http://corsica.forhikers.com/files/kml/bus.png");
gicons["hiker"] = new GIcon(baseIcon, "http://corsica.forhikers.com/files/kml/hiker.png");
gicons["rail"] = new GIcon(baseIcon, "http://corsica.forhikers.com/files/kml/rail.png");
gicons["ferry"] = new GIcon(baseIcon, "http://corsica.forhikers.com/files/kml/ferry.png");

function gmap_load_map() {
  if (GBrowserIsCompatible()) {
    map = new GMap2($("map"));
    geocoder = new GClientGeocoder();
    map.setCenter(new GLatLng(42.248,9.069214), 8);
    mycontrol=new GLargeMapControl();
    map.addControl(mycontrol);
    keyboardhandler=new GKeyboardHandler(map);
    map.addOverlay(geoXml);

    var mm = new GMarkerManager(map);
    var gmarkersA = [];
    var gmarkersB = [];

    var point = new GLatLng(42.560921,8.979435);
    var marker = createMarker(point, 'Speloncato', '<h4>Bus to/from:</h4><ul><li><a href="http://www.corsicabus.org/busCalvi/IR_Speluncato.html">Ile Rousse</a></li></ul>', 'bus');
    gmarkersB.push(marker);
    var point = new GLatLng(42.508804,8.851204);
    var marker = createMarker(point, 'Calenzana', '<h4>Bus to/from:</h4><ul><li><a href="http://www.corsicabus.org/busCalvi/CLY_Calenzana.html">Calvi</a></li></ul>', 'bus');
    gmarkersB.push(marker);
    var point = new GLatLng(42.402164,8.917809);
    var marker = createMarker(point, 'Haut-Asco', '<h4>Bus to/from:</h4><ul><li><a href="http://www.corsicabus.org/busPLeccia/PLeccia_Asco.html">Ponte-Leccia</a></li></ul>', 'bus');
    gmarkersB.push(marker);
    var point = new GLatLng(42.413319,8.650875);
    var marker = createMarker(point, 'Galeria', '<h4>Bus to/from:</h4><ul><li><a href="http://www.corsicabus.org/busCalvi/CLY_Galeria.html">Calvi</a></li></ul>', 'bus');
    gmarkersB.push(marker);
    var point = new GLatLng(42.138714,8.596973);
    var marker = createMarker(point, 'Cargese', '<h4>Bus to/from:</h4><ul><li><a href="http://www.corsicabus.org/busAjaccio/index.html">Ajaccio - Sagone, Piana - Porto - Ota</a></li></ul>', 'bus');
    gmarkersB.push(marker);

    var point = new GLatLng(42.988575,9.435883);
    var marker = createMarker(point, 'Sentier des Douaniers', '<ul><li><a href="http://corsica.forhikers.com/sentier-des-douaniers">Sentier des Douaniers</a></li></ul>', 'hiker');
    gmarkersB.push(marker);
    var point = new GLatLng(42.784313,9.408417);
    var marker = createMarker(point, 'Monte Stello', '<ul><li><a href="http://corsica.forhikers.com/hikes/monte-stello">Hike to Monte Stello</a></li></ul>', 'hiker');
    gmarkersB.push(marker);
    var point = new GLatLng(42.693539,9.254608);
    var marker = createMarker(point, 'Sentier du Littoral', '<ul><li><a href="http://corsica.forhikers.com/sentier-littoral">Sentier du Littoral</a></li></ul>', 'hiker');
    gmarkersB.push(marker);
    var point = new GLatLng(42.466019,8.852234);
    var marker = createMarker(point, 'GR 20, Mare e Monti Nord', '<ul><li><a href="http://corsica.forhikers.com/gr20">GR 20</a></li><li><a href="http://corsica.forhikers.com/mare-monti-nord">Mare e Monti Nord</a></li></ul>', 'hiker');
    gmarkersB.push(marker);
    var point = new GLatLng(41.738464,9.328508);
    var marker = createMarker(point, 'GR 20 (South)', '<ul><li><a href="http://corsica.forhikers.com/gr20">GR 20</a></li></ul>', 'hiker');
    gmarkersB.push(marker);
    var point = new GLatLng(42.306515,9.138222);
    var marker = createMarker(point, 'Mare a Mare Nord, Sentier Ile Rousse - Corte', '<ul><li><a href="http://corsica.forhikers.com/mare-mare-nord">Mare a Mare Nord</a></li><li><a href="http://corsica.forhikers.com/sentier-ile-rousse-corte">Sentier Ile Rousse - Corte</a></li></ul>', 'hiker');
    gmarkersB.push(marker);
    var point = new GLatLng(41.985016,9.397774);
    var marker = createMarker(point, 'Mare a Mare Centre', '<ul><li><a href="http://corsica.forhikers.com/mare-mare-centre">Mare a Mare Centre</a></li></ul>', 'hiker');
    gmarkersB.push(marker);
    var point = new GLatLng(41.595032,9.247570);
    var marker = createMarker(point, 'Mare a Mare Sud', '<ul><li><a href="http://corsica.forhikers.com/mare-mare-sud">Mare a Mare Sud</a></li></ul>', 'hiker');
    gmarkersB.push(marker);

    var point = new GLatLng(41.927120,8.740096);
    var marker = createMarker(point, 'Gare SNCF', '<p>Bus to Bastelica leaves from this station.</p>', 'bus');
    gmarkersA.push(marker);
    var point = new GLatLng(41.922047,8.742886);
    var marker = createMarker(point, 'Ferry terminal', '<p>Ferries to/from Marseille, Toulon and Nice.</p>', 'ferry');
    gmarkersA.push(marker);
    var point = new GLatLng(41.927120,8.739023);
    var marker = createMarker(point, 'Train station', '<p>Trains to/from Corte, Ponte Leccia and Bastia.</p>', 'rail');
    gmarkersA.push(marker);
    var point = new GLatLng(42.702621,9.448156);
    var marker = createMarker(point, 'Gare SNCF', '<p>Square Maréchal Leclerc</p><p>Buses to/from Patrimonio, Saint Florent and Nonza.</p>', 'bus');
    gmarkersA.push(marker);
    var point = new GLatLng(42.702133,9.450667);
    var marker = createMarker(point, 'Boulevard Paoli', '<p>Buses to/from Porto Vecchio.</p>', 'bus');
    gmarkersA.push(marker);
    var point = new GLatLng(42.695724,9.446697);
    var marker = createMarker(point, 'Palais de Justice', '<p>Buses to/from Loreto di Casinca and Biguglia</p>', 'bus');
    gmarkersA.push(marker);
    var point = new GLatLng(42.701927,9.446783);
    var marker = createMarker(point, 'Train station', '<p>Trains to Ponte Lecchia, Corte, Ajaccio, Ile Rousse and Calvi.</p>', 'rail');
    gmarkersA.push(marker);
    var point = new GLatLng(42.701237,9.454422);
    var marker = createMarker(point, 'Ferry terminal', '<p>Ferries to/from France and Italy.</p>', 'ferry');
    gmarkersA.push(marker);

    mm.addMarkers(gmarkersA, 14, 17);
    mm.addMarkers(gmarkersB, 9, 17);
    mm.refresh();
  }
  // display a warning if the browser was not compatible
  else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
  }
}

function createMarker(point,name,html,icontype) {
  var marker = new GMarker(point, gicons[icontype]);
  html = '<div style="font-weight: bold; font-size: medium; margin-bottom: 0em;">'+name+'</div>'+html;
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

function zoomTo(lat, long, scale) {
 map.setZoom(scale);
 map.panTo(new GLatLng(lat, long));
}
