function Service(name, postcode, type, link, canClick , lat, lng){
    this.name = name;
    this.postcode = postcode;
    this.type = type;
    this.link = link;
    this.updated = function(){};
    this.text = function(){ return name + "<br /> " + link };
    this.lat = lat;
    this.lng = lng;
    this.canClick = canClick;
}

Service.prototype.setLatLng = function(lat, lng){
  this.lat = lat;
  this.lng = lng;
  this.updated();
};

var hotelIcon;
var libraryIcon;
var map;
var geocoder;


function getLatLangAndAddPoint(service){
  geocoder.getLatLng(
  service.postcode + ' london uk', function(point) {
        addPoint(service, point);
        service.setLatLng(point.lat(),point.lng());
  });
}

function addPoint(service, point){
    if(!point && service.lat && service.lng){
      point = new GLatLng(service.lat, service.lng);
    }
    
    var markerOptions = { icon:libraryIcon };
    if(service.type=='H'){
        markerOptions = { icon:hotelIcon };
    }
    if(point){
        var marker = new GMarker(point, markerOptions);
        if(service.canClick){
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(service.text());
            });
        }
        map.addOverlay(marker);
        
    }
}

function initialize() {
  if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map_canvas"));
      map.setCenter(new GLatLng(51.50917,-0.12085), 11);
      map.addControl(new GSmallMapControl());
      geocoder = new GClientGeocoder();
      hotelIcon = new GIcon(G_DEFAULT_ICON);
      hotelIcon.image = "/images/hostel.png";
      hotelIcon.iconSize = new GSize(28, 56);
      hotelIcon.shadow = "/images/shadow.png";
      hotelIcon.shadowSize = new GSize(52, 56);
      hotelIcon.iconAnchor = new GPoint(13, 56);
      libraryIcon = new GIcon(G_DEFAULT_ICON);
      libraryIcon.iconSize = new GSize(28, 56);
      libraryIcon.image = "/images/library.png";
      libraryIcon.shadow = "/images/shadow.png";
      libraryIcon.shadowSize = new GSize(52, 56);
      libraryIcon.iconAnchor = new GPoint(13, 56);
      drawPoints();
  }
}
function drawPoints(){
    map.clearOverlays();
    if (geocoder && window.services){

          for(var i = 0; i < services.length; i++){
            
            aServ = services[i];
            if(aServ.lat && aServ.lng){
              addPoint(aServ);
            }else if(aServ.postcode){
              getLatLangAndAddPoint(aServ);
            }
            // if this is the only service, center the map on it
            // and dont make it clickable
            if(services.length==1 && aServ.lat && aServ.lng){
                map.setCenter(new GLatLng(aServ.lat, aServ.lng))
            }
          }

    }
}