
var map;
var streetviewClient;
var panorama;
var marker;
var centre;
var geocoder = null;
	

// initializes the map	
function loadDemo(latPosition, longPosition){    
// load the geocoder
geocoder = new GClientGeocoder();
// set the position
centre = new GLatLng(latPosition,longPosition);
 // set up the map
 map = new GMap2(document.getElementById("mapDiv"));
 map.setCenter(centre,17);
 map.addControl(new GScaleControl());
 map.addControl(new GMapTypeControl());
 map.addControl(new GSmallMapControl()); 
 
 // set up street view            
 var streetOverlay = new GStreetviewOverlay();
 streetviewClient = new GStreetviewClient();
 panorama = new GStreetviewPanorama(document.getElementById("pano"));
 panorama.setLocationAndPOV(centre, {yaw:0});
 GEvent.addListener(panorama, "error", handleError);
 
 function handleError(errorCode) {
      if (errorCode == 603/*FLASH_UNAVAILABLE*/) {
        //alert("Error: Flash doesn't appear to be supported by your browser! You need Flash to get Streetview images");
		document.getElementById("pano").innerHTML = "<img src='http://streetviewtours.com/images/street-view-errors/error-603.jpg' />";
        return;
      }
	  else if(errorCode == 600/*NO_NEARBY_PANO*/) {
		map.setCenter(marker.getLatLng(),8);
		//alert("Error: There is no nearby Streetview image in this area. Please try another location");
		document.getElementById("pano").innerHTML = "<img src='http://streetviewtours.com/images/street-view-errors/error-600.jpg' />";
        return;
      }
	  else
	  {
		map.setCenter(marker.getLatLng(),8);
		//alert("Error: Cannot load a Street View here (" +errorCode  +")");
		document.getElementById("pano").innerHTML = "<img src='http://streetviewtours.com/images/street-view-errors/error.jpg' />";
	  }
    }
	
             
 GEvent.addListener(panorama, "initialized", function(call) {
 var centre = call.latlng;
 map.setCenter(centre,map.getZoom());
 marker.setLatLng(centre);
 
 }) 

				
				var arrowIcon = new GIcon();
				arrowIcon.iconSize = new GSize(40, 40);
				arrowIcon.shadowSize = new GSize(22, 20);
				arrowIcon.iconAnchor = new GPoint(6, 40);
				arrowIcon.infoWindowAnchor = new GPoint(5, 1);
				//arrowIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/dir_0.png";
				arrowIcon.image = "http://streetviewtours.com/images/street-view-man/man_arrow-0.png";
			 
				marker = new GMarker(centre, {"icon":arrowIcon, "draggable":true});

				GEvent.addListener(marker, "dragstart", function() {
				  // nothing to do right now..
				});

				GEvent.addListener(marker, "dragend", function() {
				  panorama.setLocationAndPOV(marker.getLatLng());
				});

				map.addOverlay(marker);		


		//listener reacts to the yawchanged event on This streetview panorama
        GEvent.addListener(panorama, "yawchanged", function(yaw) {
            var dir = Math.round(yaw / 24);
            while (dir >= 15) { dir -= 15; }
            //marker.setImage("http://www.google.com/intl/en_ALL/mapfiles/dir_" + dir + ".png");
			marker.setImage("http://streetviewtours.com/images/street-view-man/man_arrow-" + dir + ".png");
        });

}


function showAddress(address) {

 if (geocoder) {
 geocoder.getLatLng(
 address,
 function(point) {
 if (!point) {
 alert(address + " could not be found");
 } else {
 
/* reset everything to the new position*/
 map.setCenter(point,17);
 marker.setPoint(point);
 panorama.setLocationAndPOV(marker.getLatLng());
 
// panorama.setLocationAndPOV(centre);
 }
 }
 );
 }
 
}

function loadSearch(address)
{
	// load the geocoder
geocoder = new GClientGeocoder();

 if (geocoder) {
 geocoder.getLatLng(
 address,
 function(point) {
 if (!point) {
 loadDemo(40.759011,-73.9844722);
 alert("Location " +address + " could not be found ");
 } else {
	 // load the demo
 loadDemo(point.lat(),point.lng());
 }
 })
 }
 
}
