// JavaScript Document

    	var map;
    	var mgr;
    	var icons = {};
    	var allmarkers = [];
	
	
	function initialize() { 
	  if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(43.757039,-79.336631), 11);
        map.setUIToDefault();
		mgr = new MarkerManager(map, {trackMarkers:true});		
		
		window.setTimeout(setupOfficeMarkers(map), 0);	 
		} 
    }
		
	function getIcon(images, index) {
      var icon = null;
      if (images) {
        if (icons[images[0]]) {
          icon = icons[images[0]];
        } else {
          icon = new GIcon();
          icon.image = "assets/markers/" 
              + 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.infoWindowAnchor = new GPoint(size.width, size.height);
          icon.shadow = "assets/markers/" 
              + images[1] + ".png";
          size = iconData[images[1]];
          icon.shadowSize = new GSize(size.width, size.height);
          icons[images[0]] = icon;
        }
      } else {
	  	icon = new GIcon();
		icon.image = "assets/markers/marker"+index+".png";
		icon.shadow = "assets/markers/shadow50.png";
		icon.iconSize = new GSize(20,34);
        icon.iconAnchor = new GPoint(20,34);
		icon.infoWindowAnchor = new GPoint(10,40);
	  }
	  
      return icon;
    }

    function setupOfficeMarkers(map) {
	
      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"],j);
          var title = place["name"];
          var posn = new GLatLng(place["posn"][0], place["posn"][1]);
		  var info = place["info"];
          var marker = createMarker(posn,title,icon,info, j); 
          markers.push(marker);         
        }
        mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
			
      }
      mgr.refresh();
		var cluster = new ClusterMarker(map, { markers:markers } );
		cluster.fitMapToMarkers();
    }
  
    function createMarker(posn, title, icon, info, index, maxWidth) {
		var oscInfoContent = "<h3>"+title+"</h3><p>"+info+"</p><p><a href=#"+index+" class='anchorLink'>Offer Details</a></p>";
		
		markerOptions = {title: title, icon:icon, draggable:false}
      	var marker = new GMarker(posn, markerOptions);
		
      	GEvent.addListener(marker,"click",function(){
			var opts = new Object();
			opts.maxWidth = 200;
			marker.openInfoWindowHtml(oscInfoContent,opts);
		});
		return marker;
		
		
    }
	 function deleteMarker() {
      var markerNum = parseInt(document.getElementById("markerNum").value);
      mgr.removeMarker(allmarkers[markerNum]);
    }
   
    function clearMarkers() {
      mgr.clearMarkers();
    }
   
    function reloadMarkers() {
      setupOfficeMarkers();
    }