var streetview;
var panoramaClient;



//
// Page initialisation function called after the page has loaded.
//
function Initialise()
{
	DetectFlash();
	CreateMap();
}



//
// Detection routine which decides whether the Flash photo slideshow
// should be used or the DHTML version.
//
function DetectFlash()
{
	var noFlash = false;

	
	//
	// Has slideshow been generated?
	//
	var slideshow = document.getElementById( "monoSlideshow" );
	if ( slideshow.innerHTML == "" && slideshow.innerText == "" )
	{
		noFlash = true;
	}

	
	//
	// Does the browser have the Flash plug-in installed?
	//
	if ( !((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("Mac") == -1 && navigator.appVersion.indexOf("3.1") == -1) ||
		(navigator.plugins && navigator.plugins["Shockwave Flash"]) || navigator.plugins["Shockwave Flash 2.0"]) )
	{
		noFlash = true;
	}
	
	//
	// If this browser doesn't support Flash, use static photos instead.
	//
	if ( noFlash )
	{
		document.getElementById( "Slideshow" ).style.display = "none";
		document.getElementById( "StaticPhotos" ).style.display = "";
		document.getElementById( "StaticThumbs" ).style.display = "";
		document.getElementById( "StaticPhotos" ).className = "";
		
		ShowPhoto( 0 );
	}

}



//
// Creates the Google map pointing to the property.
//
function CreateMap()
{
	//
	// Create the map.
	//
	map = new GMap2( document.getElementById( "map_canvas" ) );
	map.setCenter( new GLatLng( marker_latitude, marker_longitude ), map_zoom );
	map.disableScrollWheelZoom();
	map.checkResize();

	//
	// Add controls.
	//
	map.addControl( new GSmallMapControl() );
	map.addControl( new GMapTypeControl() );

	//
	// Create the position marker.
	//
	map_marker = new GMarker( new GLatLng( marker_latitude, marker_longitude ) );
	map.addOverlay( map_marker );

	
	//
	// Add streetview.
	//
	var sv = document.getElementById( "streetview" );
	if ( sv != null || sv != undefined )
	{
		panoramaClient = new GStreetviewClient();      
		streetview = new GStreetviewPanorama(document.getElementById( "panorama" ));
		panoramaClient.getNearestPanorama( map_marker.getLatLng(), UpdateStreetview );
	}
}



//
// Updates the current street view to point at 'panoData'.
//
function UpdateStreetview( panoData )
{
	if (panoData.code != 200) // 200 = Success
	{
		//
		// If no street view is available for this location, hide the street view box.
		//
		var hideStreetview = document.getElementById( "streetview" );
		if ( hideStreetview != null && hideStreetview != undefined )
		{
			hideStreetview.style.display = "none";
		}
	}
	else
	{
		var angle = ComputeAngle(map_marker.getLatLng(), panoData.location.latlng);
		streetview.setLocationAndPOV(panoData.location.latlng, {yaw: angle});
	}
}



//
// Calculates the angle between to points.
//
function ComputeAngle( endLatLng, startLatLng )
{
	var DEGREE_PER_RADIAN = 57.2957795;
	var RADIAN_PER_DEGREE = 0.017453;

	var dlat = endLatLng.lat() - startLatLng.lat();
	var dlng = endLatLng.lng() - startLatLng.lng();

	//
	// We multiply dlng with cos(endLat), since the two points are very closeby,
	// so we assume their cos values are approximately equal.
	//
	var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat) * DEGREE_PER_RADIAN;
	return WrapAngle(yaw);
}



//
// Ensures an angle remains between 0-359.
//
function WrapAngle(angle)
{
	if (angle >= 360)
	{
		angle -= 360;
	}
	else if (angle < 0)
	{
		angle += 360;
	}

	return angle;
}
