var map;
var mgr;
var icons = {};
var allmarkers = [];
var officeLayer = [
  {
    "zoom": [0, 17],
    "places": [
      {
        "name": "Headquarters",
        "posn": [18.8500, -73.1419],
        "html": ""
      },
      {
        "name": "New York Sales & Engineering Office",
        "posn": [18.7500, -73.1419],
        "html": ""
      },
      {
        "name": "Atlanta Sales &amp; Engineering Office",
        "posn": [18.6500, -73.1419],
        "html": ""
      },
      {
        "name": "Boulder Sales & Engineering Office",
        "posn": [18.5500, -73.1419],
        "html": ""
      },
      {
        "name": "Cambridge Sales & Engineering Office",
        "posn": [18.4500, -73.1419],
        "html": ""
      },
      {
        "name": "Chicago Sales Office",
        "posn": [41.889232, -87.628767],
        "html": ""
      },
      {
        "name": "Dallas Sales Office",
        "posn": [32.925355, -96.816087],
        "html": ""
      },
      {
        "name": "Denver Sales Office",
        "posn": [39.563011, -104.868962],
        "html": ""
      },
    ]
  }
];


function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
		map.setMapType(G_PHYSICAL_MAP);
    var customUI = map.getDefaultUI();
    customUI.maptypes.hybrid = false;
		map.setUI(customUI);
    map.setCenter(new GLatLng(46.7300, 28.6020), 07);
    map.enableDoubleClickZoom();
	map.addControl(new GOverviewMapControl());
    mgr = new MarkerManager(map, {trackMarkers:true});
    window.setTimeout(setupOfficeMarkers, 0);
  }
}

function getIcon(images) {
  var icon = null;
  if (images) {
    if (icons[images[0]]) {
      icon = icons[images[0]];
    } else {
      icon = new GIcon();
      icon.image = "http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/examples/images/"
          + images[0] + ".png";
      var size = iconData[images[0]];
      icon.iconSize = new GSize(size.width, size.height);
      icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
      icon.shadow = "http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/examples/images/"
          + images[1] + ".png";
      size = iconData[images[1]];
      icon.shadowSize = new GSize(size.width, size.height);
      icons[images[0]] = icon;
    }
  }
  return icon;
}

function setupOfficeMarkers() {
  allmarkers.length = 0;
  for (var i in officeLayer) {
    var layer = officeLayer[i];
    var markers = [];
    for (var j in layer["places"]) {
      var place = layer["places"][j];
      var icon = getIcon(place["icon"]);
      var title = place["name"];
      var posn = new GLatLng(place["posn"][0], place["posn"][1]);
      var html = place["html"];
      var marker = createMarker(posn,title,icon,html);
      markers.push(marker);
      allmarkers.push(marker);
    }
    mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
  }
  mgr.refresh();
}

function createMarker(posn, title, icon, html) {
  var marker = new GMarker(posn, {title: title, icon: icon, draggable:false });
  GEvent.addListener(marker, 'click', function(){ 
      marker.openExtInfoWindow(
        map,
      	"extInfoWindow_coolBlues",
        html,
        {beakOffset: 1}
      ); 
    });
  return marker;
}