//<![CDATA[

var gmarkers = [];
var i = 0;
var baseicon = new GIcon();
baseicon.image = "http://stage.utah.com/art/maps/orange-marker.png";
baseicon.shadow = "http://stage.utah.com/art/maps/marker-background.png";
baseicon.iconSize = new GSize(20, 20);
baseicon.shadowSize = new GSize(20, 20);
baseicon.iconAnchor = new GPoint(9, 19);
baseicon.infoWindowAnchor = new GPoint(9, 10);

// This function picks up the click and opens the corresponding info window
function showMapMarker() {
  GEvent.trigger(gmarkers[i], "click");
}
	
function load(clat, clong, zlevel, info) {
  if (GBrowserIsCompatible()) {				
	var map = new GMap2(document.getElementById("gmap"));
	map.addControl(new GSmallMapControl());
	var geocoder = new GClientGeocoder();
	map.setCenter(new GLatLng(clat, clong), zlevel);
	var boundaryPoints = map.getBounds();
	
	// Add event listener for movend or dragend or both. When the user quits moving the map, then make the ajax call to get
	// properties within the bounds of the map
	//GEvent.addListener(map, "zoomend", function(){ boundaryPoints = map.getBounds(); getProperties(boundaryPoints); });
	//GEvent.addListener(map, "dragend", function(){ boundaryPoints = map.getBounds(); getProperties(boundaryPoints); });
	
	
	// Creates a marker at the given point with the given number label
	function createMarker(point, name, html) {
	  var marker = new GMarker(point, { icon: baseicon, title: name });
	  GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml( html	);
	  });
	  gmarkers[i] = marker;
	  return marker;
	}				
	
	function showAddress(event_id, event, address, contact, phone, url, latitude, longitude) {
	  var html = '<span style="font: 12px arial"><b>' + event + '</b><br />' + address + '<br /><b>Contact:</b> ' + contact + '<br /><b>Phone:</b> ' + phone + ' &bull; <a href="/utah_logs/thing_link_log.php?log=' + url + '||' + event_id + '||web_address||' + window.location.href + '" target="_blank">Website</a><br /></span>';
	 if(latitude == 0 || longitude == 0){ 
	  	geocoder.getLatLng(address, function(point){ 
				if(point != null){
					map.addOverlay(createMarker(point, event, html));
				}
			} );
	  } else {
	  	point = new GLatLng(latitude / 3600, longitude / -3600);
		//alert(name + ' ' + point);
		if(point != null){
			map.addOverlay(createMarker(point, event, html));
		}
	  } 
	}
	showAddress(info["id"], info["name"], info["address"], info["contact"], info["phone"], info["url"], info["latitude"], info["longitude"]);
	setTimeout("showMapMarker()", 1000);
  }
}
//]]>