var map;
var gdir;

function initialize() {
 if (GBrowserIsCompatible()) {
  map = new GMap2(document.getElementById("map_canvas"));
  map.setUIToDefault();

  gdir = new GDirections(map, document.getElementById("directions"));
  GEvent.addListener(gdir, "error", function() {
   if (document.address.from.value.length > 0) {
    document.getElementById("directions").innerHTML = '<b>Unable to find directions</b>';
   }
  });

  if (document.address.from.value.length > 0) {
   setDirections();
  } else {
   map.setCenter(new GLatLng(52.455141, -1.1995375));
   map.setZoom(15);
  }

  var baseIcon = new GIcon(G_DEFAULT_ICON);
  baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  baseIcon.iconSize = new GSize(20, 34);
  baseIcon.shadowSize = new GSize(37, 34);
  baseIcon.iconAnchor = new GPoint(9, 34);
  baseIcon.infoWindowAnchor = new GPoint(9, 2);

  var iconSchool = new GIcon(baseIcon);
  iconSchool.image = "./images/map/mschool.png"
  var mSchool = new GMarker(new GLatLng(52.460096,-1.206319), iconSchool);
  map.addOverlay(mSchool);
  GEvent.addListener(mSchool, "click", function() {
   mSchool.openInfoWindowHtml("<p><b>Lutterworth Community College</b><br>Bitteswell Road<br>Lutterworth,<br>Leicestershire,<br>LE17 4EW</p>");
  });       

  var iconPool = new GIcon(baseIcon);
  iconPool.image = "./images/map/mpool.png"
  var mPool = new GMarker(new GLatLng(52.457204,-1.206754), iconPool);
  map.addOverlay(mPool);
  GEvent.addListener(mPool, "click", function() {
   mPool.openInfoWindowHtml("<p><b>Lutterworth Sports Centre</b><br>Coventry Road<br>Lutterworth,<br>Leicestershire,<br>LE17 4RB<br>Phone: 01455 200800</p>");
  });

  var boundaries = new GLatLngBounds(new GLatLng(52.45940634781102, -1.2093731760978699), new GLatLng(52.460860062670726, -1.206422746181488));
  var oldmap = new GGroundOverlay("./images/map/college.png", boundaries);
  map.addOverlay(oldmap);

  document.getElementById("address").style.visibility = "visible";
 }
}
addEvent(window, 'load', initialize);


function setDirections() {
 if (GBrowserIsCompatible()) {
  if (document.address.from.value.length > 0) {
   gdir.load("from: " + document.address.from.value + ", UK to: " + document.address.to.value);
  } else {
   gdir.clear();
   map.setZoom(15);
   map.panTo(new GLatLng(52.455141, -1.1995375));
  }
 }
}
