	function addLoadEvent (func) {
		var oldonload = window.onload;
		if(typeof window.onload != 'function') {
			window.onload = func;
		}
		else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
       
	function showGMap () {
		document.getElementById("gaddress").style.display = 'none';
		var address = document.getElementById('gaddress').getAttribute('title');
		var listing_name='';
		var map_div = document.getElementById("map");
		var map = new GMap2(map_div);
		map.addControl(new GSmallMapControl());
		var mapTypeControl = new GMapTypeControl();
		var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(-10,20));
		showAddress(map, listing_name, address, '', '');
	}

        function showAddress(map, listing_name, address, lat, long) {
		// if lat and long provided, use that instead of geocoder
                if(lat.length && long.length) {
                        point = new GLatLng(lat, long);
                        map.setCenter(point, 12);
                        marker = new GMarker(point);
                        map.addOverlay(marker);
			setDirectionsLink (point);
                }
                else {
                        var geocoder = new GClientGeocoder();
                        geocoder.getLatLng(
                                        address,
                                        function(point) {
                                                if (!point) {
                                                        // show by lat/long from zip code
                                                        if(lat.length && long.length) {
                                                                point = new GLatLng(lat, long);
                                                        }
                                                        else {
                                                                alert(address + ' not found.');
                                                                document.getElementById("map").style.display = "none";
                                                                return;
                                                        }
                                                }
                                                map.setCenter(point, 12);
                                                marker = new GMarker(point);
                                                map.addOverlay(marker);
						//setDirectionsLink (point);
						setDirectionsLink (address);
                                        }
                        );
                }
        }

	//function setDirectionsLink (point) {
	function setDirectionsLink (address) {
		var the_link = document.getElementById('directions');
		the_link.onclick = function() {
			//window.location.href = 'http://maps.google.com/maps?daddr=' + point.lat() + ',' + point.lng();
			window.location.href = 'http://maps.google.com/maps?daddr=' + address;
			return false;
		}
	}

	addLoadEvent(showGMap);
