﻿//Utility function to addEvent
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}


function showMarkerByAddress(map, markerManager, geocoder, address, infoHtml, setAsCenter, zoomLevel) {
	
	if(map != null && map.isLoaded()) {
	    geocoder.getLatLng(
         address,
	    function(point) {
		if (!point) {
        		alert(address + " not found");
     	} else {

	   //We have a valid point. Set it as center?
	   if(setAsCenter) {
		   if(zoomLevel != null) {
		     map.setCenter(point, zoomLevel);
		   } else {
			map.setCenter(point);
		   }
	   }

	   //Create the marker and add it 
        var marker = new GMarker(point);
        map.addOverlay(marker);
	   //markerManager.refresh();

	   //If InfoHtml is specified, show it
	   if(infoHtml != null && infoHtml != "") {
        		marker.openInfoWindowHtml(infoHtml);
	   }
      }
     });
    }
}


function showMarkerByCoordinates(map, latitude, longitude, infoHtml, setAsCenter, zoomLevel, showInfoHtml) {
	
	// if the map is valid and loaded
	if(map != null && map.isLoaded()) {

		// create a point using the coordinates	
	    	var point = new GLatLng(parseFloat(latitude), parseFloat(longitude), true);

	   	// set the point as center?
	   	if(setAsCenter) {
			if(zoomLevel != null) {
		     	map.setCenter(point, zoomLevel);
		   	} 
			else {
				map.setCenter(point);
		   	}
	   	}

		// create the marker and add it 
        	var marker = new GMarker(point);
        	map.addOverlay(marker);
	   	//markerManager.refresh();

		
	   	//If InfoHtml is specified, show it
	   	if(infoHtml != null && infoHtml != "") {
			if(showInfoHtml) {
	        		marker.openInfoWindowHtml(infoHtml);
			}
	   		GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(infoHtml);});
     		} 
	}
}