/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.map.streetview.js
 * Includes functions that handle street view functionality that
 * mimics a subset of the street view functionality on maps.google.com
 * Version 1.0 | 05-04-2009
 * Developed by Cartosoft, LLC
 * http://www.cartosoft.com | info@cartosoft.com
 * Copyright 2009 Google, Inc. | http://www.google.com
**********************************************************************/

/***
Toggle Locations on and Off
***/

function activateStreetView() {
	
	var center_lat = map.getCenter();

	var guyIcon = new GIcon(G_DEFAULT_ICON);
	guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
	guyIcon.transparent = "http://maps.google.com/intl/en_us/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
	
	streetMarker = new GMarker(center_lat, {icon: guyIcon, draggable: true});
	map.addOverlay(streetMarker);
	lastMarkerLocation = center_lat;
	streetMarkerEnd = GEvent.addListener(streetMarker, "dragend", onDragEnd);
	streetMarkerClick =GEvent.addListener(streetMarker, "click", openPanoramaBubble);
	//streetViewZoom = GEvent.addListener(map,"zoomend",function(){streetMarker.setLatLng(map.getCenter());});

}

function deActivateStreetView() {
	
	map.removeOverlay(streetMarker);
	GEvent.removeListener(streetMarkerEnd);
	GEvent.removeListener(streetMarkerClick);
	//GEvent.removeListener(streetViewZoom);
	//GEvent.removeListener(streetViewChange);
	
}


function openPanoramaBubble() {

	if (lastMarkerLocation == streetMarker.getLatLng() && sv_available == false) {
		streetMarker.openInfoWindowHtml('<div class="small-italic">Sorry, no Street View is available for this location<br/>Zoom in or drag me to an area highlighted in <span style="color:#2f63e3;">blue</span></div>');
		sv_available = true;
		return false;
	}

	var contentNode = document.createElement('div');
	contentNode.style.textAlign = 'center';
	contentNode.style.width = '450px';
	contentNode.style.height = '350px';
	contentNode.innerHTML = 'Loading panorama';
	
	var smallNode = document.createElement('div');
	smallNode.style.width = '330px';
	smallNode.style.height = '260px';
	smallNode.id = 'pano';
	streetMarker.openInfoWindow(smallNode, {maxContent: contentNode, maxTitle: "Street View"});
	
	panorama = new GStreetviewPanorama(smallNode);
	panorama.setLocationAndPOV(streetMarker.getLatLng(), null);
	GEvent.addListener(panorama, "newpano", onNewLocation);
	GEvent.addListener(panorama, "yawchanged", onYawChange); 
	
	var iw = map.getInfoWindow();
	
	//iw.maximize();
	
	GEvent.addListener(iw, "maximizeend", function() {
		panorama.setContainer(contentNode);  
		window.setTimeout("panorama.checkResize()", 5);
	});
	GEvent.addListener(streetMarker, "infowindowbeforeclose", function() {
		panorama.remove();
	});
	
}

function onYawChange(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.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
	streetMarker.setImage(guyImageUrl);
	
}

function onNewLocation(lat, lng) {

	var latlng = new GLatLng(lat, lng);
	streetMarker.setLatLng(latlng);
	
}

function onDragEnd() {

	var latlng = streetMarker.getLatLng();
	
	if (panorama) {
		svClient.getNearestPanorama(latlng, onResponse);
	}
	
}

function onResponse(response) {

	if (response.code != 200) {
		//streetMarker.setLatLng(lastMarkerLocation);
		GEvent.removeListener(streetMarkerClick);
		streetMarker.openInfoWindowHtml('<div class="small-italic">Sorry, no Street View is available for this location<br/>Zoom in or drag me to an area highlighted in <span style="color:#2f63e3;">blue</span></div>');
		return false;
	} else {
		if (streetMarkerClick) {
			streetMarkerClick =GEvent.addListener(streetMarker, "click", openPanoramaBubble);
		}
		var latlng = new GLatLng(response.Location.lat, response.Location.lng);
		streetMarker.setLatLng(latlng);
		lastMarkerLocation = latlng;
		openPanoramaBubble();
	}
	
}

function checkPanoData(response) {

  	if (response.code != 200) {
		return false;
  	}
  	
  	return ture;
}
