// initialise global variables
var map;

var sidebar_html = '';
var i = 0;
var gpoints = [];
var gmarkers = [];
var htmls = [];

// centre on Ireland
function centreIreland() {
   //var centre = new GPoint(-7.382813, 53.448807);
   // map.centerAndZoom(centre, 11);
   var centre = new GLatLng(53.448807,-7.382813);
   map.setCenter(centre, 6);
}

// Creates a marker with info window display
function createMarker(longitude, lat, id, name, rating, type, description) {
  var point = new GLatLng(lat, longitude);
  var marker = new GMarker(point);
  var html = "<div class='infowin' style='width:200px; text-align:left;'>" +
    "<b><a href='http://vegans.frommars.org/ireland/details.php?key=" + id +
    "'>" + name + "</a></b>&nbsp;" +
    "<img src='http://vegans.frommars.org/images/" + rating + ".gif' " +
    "alt='Rating: " + rating + "'>" +
    "<br /><b>" + type + "</b>&nbsp;&nbsp;" +
    "<br /><span style='font-size:smaller;'>" + description.substring(0,150) +
    "..." +
    '<a href="javascript:goto_place(' + i + ')">[Zoom in]</a>' +
    '</span></div>';

  GEvent.addListener(marker, "click", function() {
    // marker.openInfoWindowHtml(html);
    marker.openInfoWindowHtml(html);
    });

  // build sidebar links
  gpoints[i] = point;
  gmarkers[i] = marker;
  htmls[i] = html;
  sidebar_html +=
    '<li><a href="javascript:goto_place(' + i + ')">' + name + '</a></li>';
  i++;

  return marker;
}

// Creates a marker with info window display (ireland sightseeing)
function createMarker2(longitude, lat, id, name, location, county, description) {
  var point = new GLatLng(lat, longitude);
  var marker = new GMarker(point);
  var html = "<div class='infowin' style='width:200px; text-align:left;'>" +
    "<b>" + name + "</a></b>&nbsp;" +
    "<br />" + location +
    "<br /><span style='font-size:smaller;'>" + description.substring(0,150) +
    "..." +
    '<a href="javascript:goto_place(' + i + ')">[Zoom in]</a>' +
    '</span></div>';

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
    });

  // build sidebar links
  gpoints[i] = point;
  gmarkers[i] = marker;
  htmls[i] = html;
  sidebar_html +=
    '<li><a href="javascript:goto_place(' + i + ')">' + name + '</a></li>';
  i++;

  return marker;
}

// GMap helper function to centre on  point & pop a GMarker infowindow
// assumes gpoints[], gmarkers[], htmls[] arrays populated and map called 'map'
function goto_place(i) {
  // map.centerAndZoom(gpoints[i], 1);
  map.setCenter(gpoints[i], 17);
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}

