Event.observe(window, "load", function() {
  var map = new GMap2(document.getElementById("map"));
  map.addControl(new GSmallZoomControl());
  
  function createMarker(latlng, city) {
    var marker = new GMarker(latlng);
    var html = "<strong><a href='/?city=" + escape(city.name + ", " + city.abbreviation) + "'>"+city.name+", "+city.abbreviation+"</a></strong>";
    GEvent.addListener(marker, "click", function () {
      map.openInfoWindowHtml(latlng, html);
    });             
    return marker;
  }

  function createNewCityMarker(latlng, city) {
    var custom_icon = new GIcon();
    custom_icon.image = "/images/blue-dot.png";
    custom_icon.shadow = "/images/msmarker.shadow.png";
    custom_icon.iconSize = new GSize(32, 32);
    custom_icon.shadowSize = new GSize(59, 32);
    custom_icon.iconAnchor = new GPoint(16, 32);
    custom_icon.infoWindowAnchor = new GPoint(16, 32);
    var marker = new GMarker(latlng, {icon:custom_icon});
    var html = "<strong>"+city.name+" - Famplosion!</strong><br/>Coming soon!";
    map.openInfoWindowHtml(latlng, html);
    GEvent.addListener(marker, "click", function () {
      map.openInfoWindowHtml(latlng, html);
    });
    return marker;
  }                  

  var bounds = new GLatLngBounds;
  
  if (yourcity != null) {
    var plot_yourcity = true;
    for (var i = 0; i < cities.length; i++) {
      if ((cities[i].region.latitude == yourcity.city.latitude) && (cities[i].region.longitude == yourcity.city.longitude)) {
        plot_yourcity = false;
      }
    }    
    
    if (plot_yourcity) {
      var latlng = new GLatLng(yourcity.city.latitude, yourcity.city.longitude);
      bounds.extend(latlng);
      map.addOverlay(createNewCityMarker(latlng, yourcity.city));
    }
  }  
  
  for (var i = 0; i < cities.length; i++) {
    var latlng = new GLatLng(cities[i].region.latitude, cities[i].region.longitude);
    bounds.extend(latlng);
    map.addOverlay(createMarker(latlng, cities[i].region));
  }

  map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
});