function setupGowallaStreetview(latlng) {
	function getBearing(from, to) {
		var fromlat = from.latRadians();
		var fromlng = from.lngRadians();
		var tolat = to.latRadians();
		var tolng = to.lngRadians();
		return (((Math.atan2(Math.sin(tolng - fromlng) * Math.cos(tolat), Math.cos(fromlat) * Math.sin(tolat) - Math.sin(fromlat) * Math.cos(tolat) * Math.cos(tolng - fromlng))/Math.PI) * 180) + 360) % 360;
	}
	if(!Gowalla.streetviewClient) Gowalla.streetviewClient = new GStreetviewClient();      
	if(!Gowalla.streetviewOverlay) {
		Gowalla.streetviewOverlay = new GStreetviewOverlay();
		Gowalla.map.addOverlay(Gowalla.streetviewOverlay);
	}
	Gowalla.streetviewClient.getNearestPanorama(latlng, function(data) {
		if(data.code != 200 || data.location == null) {
			var clickEventListener = GEvent.addListener(Gowalla.map, "click", function(overlay,newlatlng,overlaylatlng) {
				GEvent.removeListener(clickEventListener);
				setupGowallaStreetview(newlatlng);
			});
			alert("Unable to show Google Streetview for this spot. :-(\n\nTip: Try clicking a nearby blue road (if one exists) to see if you can view the spot from there.");
			return;
		}
	
		var guyIcon = new GIcon();
		guyIcon.image = "http://maps.gstatic.com/mapfiles/cb/man_arrow-0.png";
		guyIcon.transparent = "http://maps.gstatic.com/mapfiles/cb/man-pick.png";
		guyIcon.imageMap = [
        		26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
        		16,20, 16,14, 19,13, 22,8
     		];
		guyIcon.iconSize = new GSize(49, 52);
		guyIcon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
		guyIcon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head
		
		var guyMarker = new GMarker(data.location.latlng, {icon: guyIcon, draggable: false});
		function onYawChanged(newYaw) { var GUY_NUM_ICONS = 16; var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS; if (newYaw < 0) { newYaw += 360; } guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS; guyImageUrl = "http://maps.gstatic.com/mapfiles/cb/man_arrow-" + guyImageNum + ".png"; guyMarker.setImage(guyImageUrl); };
	
		Gowalla.map.addOverlay(guyMarker);
		var yaw = getBearing(data.location.latlng, new GLatLng(Gowalla.spot.lat, Gowalla.spot.lng));
		onYawChanged(yaw);
		
		var el=document.createElement("div"),
			c=document.getElementById('map-wrapper');

		el.id = 'streetview';
		el.style.height = "250px";
		el.style.width = "100%";
		c.appendChild(el);
		
		var sv = new GStreetviewPanorama(el, {latlng:latlng,pov:{yaw:yaw}});
	
		GEvent.addListener(sv, "yawchanged", onYawChanged);
		GEvent.addListener(sv, "initialized", function(location) { guyMarker.setLatLng(location.latlng); onYawChanged(location.pov.yaw); });
		GEvent.addListener(Gowalla.map, "click", function(overlay,latlng,overlaylatlng) {
			var yaw = getBearing(latlng, new GLatLng(Gowalla.spot.lat, Gowalla.spot.lng));
			sv.setLocationAndPOV(latlng, {yaw:yaw});
		});
	
		el.style.marginTop = "10px";	
		c.style.height="480px";
	});
}

if(document.body.id != "spot") {
	alert("You can only use this bookmarklet on Gowalla Spot pages, e.g. http://gowalla.com/spots/23328");
} else if(!document.getElementById('streetview')) {
	setupGowallaStreetview(Gowalla.map.getCenter());
}
