//<![CDATA[

var marker;
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(marker, "click");
}

function loadthingmap(clat, clong, zlevel, info) {
  if (GBrowserIsCompatible()) {				
	var map = new GMap2(document.getElementById("gmap"));
	map.addControl(new GMapTypeControl());
	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) {
	  marker = new GMarker(point, { icon: baseicon, title: name });
	  GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml( html, { maxWidth: 275 }	);
	  });
	  return marker;
	}				
	
	function showAddress(thing_id, name, address, phone, url, latitude, longitude) {
	  var html = '<div style="padding: 10px;"><span style="font: 12px arial"><b>' + name + '</b>' + (address ? '<br />' + address : '') + (phone ? '<br /><div id="gmapphonenumber" style="display: inline;"><a href="void(0)" onclick="showPhoneNumber(' + thing_id + ', \'gmapphonenumber\');return false;">Click for phone number</a></div>' : '') + (url?' &bull; <a href="/utah_logs/thing_link_log.php?log=' + url + '||' + thing_id + '||web_address||' + window.location.href + '" target="_blank">Website</a>' : '') + '<br /></span></div>';
	 if(latitude == 0 || longitude == 0){ 
	 
	  	geocoder.getLatLng(address, function(point){ 
				if(point != null){
					map.addOverlay(createMarker(point, name, html));
				}
			} );
	  } else {
	  	point = new GLatLng(latitude / 3600, longitude / -3600);
		if(point != null){
			map.addOverlay(createMarker(point, name, html));
		}
	  } 
	}
	showAddress(info["thing_id"], info["name"], info["address"], info["phone"], info["url"], info["latitude"], info["longitude"]);
	setTimeout("showMapMarker()", 1000);
  }
}
//]]>