   var map;
   var loc;
   var tooltip;
    var i = 0;
    var baseIcon = new GIcon();
    baseIcon.iconSize=new GSize(32,32);
    baseIcon.shadowSize=new GSize(56,32);
    baseIcon.iconAnchor=new GPoint(16,32);
    baseIcon.infoWindowAnchor=new GPoint(16,0);
    var propertyIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal5/icon12.png", null, "http://maps.google.com/mapfiles/kml/pal5/icon12s.png");
    var personIcon = new GIcon(baseIcon, "http://www.rivierarentalguide.com/images/gman.png", null, "http://www.rivierarentalguide.com/images/gmans.png");
    var cafeIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon62.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon62s.png");
    var restaurantIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon63.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon63s.png");
    var barIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon27.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon27s.png");
    var parkIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon12.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon12s.png");
    var supermarketIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon26.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon26s.png");
    var nightclubIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon27.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon27s.png");
    var airportIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon56.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon56s.png");
    var beachIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon58.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon58s.png");
    var cinemaIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon30.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon30s.png");
    var garageIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon29.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon29s.png");
    var stadiumIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon57.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon57s.png");
    var hospitalIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon63.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon63s.png");
    var car_hireIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon31.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon31s.png");
    var convenience_storeIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon26.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon26s.png");
    var bankIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon58.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon58s.png");
    var atm_machineIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon58.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon58s.png");
    var religious_centreIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon11.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon11s.png");
    var police_stationIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon8.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon8s.png");
    var dry_cleanersIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon12.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon12s.png");
    var casinoIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon57.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon57s.png");
    var tabacIcon = casinoIcon;
    var operaIcon = casinoIcon;
    var playgroundIcon = casinoIcon;
    var taxi_rankIcon = casinoIcon;
    var scooter_hireIcon = casinoIcon;
    var bike_hireIcon = casinoIcon;
    var roller_skate_hireIcon = casinoIcon;
    var boat_hireIcon = casinoIcon;
    var bakeryIcon = casinoIcon;
    var post_officeIcon = casinoIcon;
    var veterinary_clinicIcon = casinoIcon;
    var vending_machineIcon = casinoIcon;
    var exhibition_hallIcon = casinoIcon;
    var museumIcon = casinoIcon;
    var historic_monumentIcon = casinoIcon;
    var marinaIcon = casinoIcon;
    var tourist_attractionIcon = casinoIcon;
    var chemistIcon = casinoIcon;
    var train_stationIcon = casinoIcon;
    var bus_stationIcon = casinoIcon;
    var shopping_centreIcon = casinoIcon;
    var dry_cleanersIcon = casinoIcon;
    var galleries_and_ateliersIcon = casinoIcon;
    var helipadIcon = casinoIcon;
    var holiday_activityIcon = casinoIcon;
    var gallery_or_atelierIcon = casinoIcon;




   function load() 
   {
      if (GBrowserIsCompatible()) 
      {
         map = new GMap2(document.getElementById("map"));
         eval('map.setMapType('+server_g_map_type+');');
         map.addControl(new GSmallMapControl());
         try
         {
         map.setCenter(new GLatLng(server_geo_lat,server_geo_lng), server_zoom_factor);          
         } catch(err) { }
         tooltip = document.createElement("div");
         map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
         tooltip.style.visibility="hidden";
         loc = createMarker(new GLatLng(server_geo_lat,server_geo_lng),server_property_name,'',propertyIcon,'',2);
      }
   }



   function locateOnMap(lat,lng,tooltip,icon,link)
   {
      if(dir_polyline) map.removeOverlay(dir_polyline);      
      var pnt = new GLatLng(lat,lng);
      try
      {
	      // ZOOM TO BOUNDS
	      var latlng = [new GLatLng(server_geo_lat,server_geo_lng),pnt];
		  var latlngbounds = new GLatLngBounds()
		  for ( var i = 0; i < latlng.length; i++ )
		  {
			  latlngbounds.extend(latlng[i]);
		  }
		  map.setCenter(latlngbounds.getCenter(),map.getBoundsZoomLevel(latlngbounds)-2); 
      } catch(err) { }  
      createMarker(pnt,tooltip,link,icon,'',1); 
   }
   
   var dir_polyline = null;
   function showDirections(from_geo_lat, from_geo_lng, to_geo_lat, to_geo_lng)
   {
      var directions = new GDirections();   
      var load_str = "from: "+from_geo_lat+","+from_geo_lng+" to: "+to_geo_lat+","+to_geo_lng;
      GEvent.addListener(directions, "load", function() {
      dir_polyline = directions.getPolyline();
      map.addOverlay(dir_polyline);
      });
      directions.load(load_str, {preserveViewport:true, getPolyline:true}); //, {travelMode:G_TRAVEL_MODE_WALKING}
   }

   function returnToProperty()
   {
      try
      {
      map.panTo(new GLatLng(server_geo_lat,server_geo_lng));
      } catch(err) { }  
      showTooltip(loc);
   }

    function createMarker(pPoint,pName,pLink,pIcon,pStyling,pImportance)
    {
        var marker = new GMarker(pPoint,{icon:pIcon,zIndexProcess:importanceOrder});
        marker.importance = pImportance;
        if(pName != '')
        {
           marker.tooltip = '<div class="tooltip" style="'+pStyling+'">'+pName+'</div>';
        }
        marker.url = pLink;
        map.addOverlay(marker);
        
        if(pName != '')
        {
           showTooltip(marker);  
           GEvent.addListener(marker,"mouseover", function()
           {
               showTooltip(marker);
           });
           GEvent.addListener(marker,"mouseout", function()
           {
             tooltip.style.visibility="hidden"
           });
        }
        
        if(pLink != '')
        {
           GEvent.addListener(marker,"click", function()
           {
               window.location.href = marker.url;
           });
        }
        return marker;
    }

    function showTooltip(marker)
    {
         tooltip.innerHTML = marker.tooltip;
         var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
         var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
         var anchor=marker.getIcon().iconAnchor;
         var width=marker.getIcon().iconSize.width;
         var height = 25;
         var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
         pos.apply(tooltip);
         tooltip.style.visibility="visible";
    }
    
    
   function importanceOrder (marker,b) 
   {
      return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
   }
    
      
    var street_view_pano;
    var street_view_loc;
    var polygon;
    var street_view_client;
    var street_view_geo_lat = server_street_view_geo_lat;
    var street_view_geo_lng = server_street_view_geo_lng;
    var current_pitch = server_street_view_pov_pitch;
    var current_yaw = server_street_view_pov_yaw;
    var current_zoom = server_street_view_pov_zoom;
   
 
 
   function loadStreetViewIfAvailable()
   {   
      if(server_street_view_enabled != 1) return; 
      var street_view_obj = document.getElementById('street_view');
      if(street_view_obj) loadStreetView();   
   }
   
   function loadStreetView()
   {
        // Normalise street view geo lat and geo lng, i.e.
        // find nearest street view panorama and then attempt
        // to render
        street_view_client = new GStreetviewClient();
        street_view_client.getNearestPanoramaLatLng(new GLatLng(street_view_geo_lat,street_view_geo_lng),showPano);
   }
   
   
    function renderStreetView(prop,prop_pov)
    {
        var panoramaOptions = { latlng:prop, pov:prop_pov };
        street_view_pano = new GStreetviewPanorama(document.getElementById("street_view"), panoramaOptions);
        GEvent.addListener(street_view_pano, "yawchanged", onYawChange);
        GEvent.addListener(street_view_pano, "pitchchanged", onPitchChange);
        GEvent.addListener(street_view_pano, "zoomchanged", onZoomChange);
        GEvent.addListener(street_view_pano, "initialized", onLocationChangeStreetView); 
        GEvent.addListener(street_view_pano, "error", onError);
    }
    
    
    function onError(errorCode)
    {
       if(errorCode == 603)
       {
          var street_view_obj = document.getElementById('street_view');
          if(street_view_obj) street_view_obj.style.display = 'none';  
       }   
    }
    
    
    
    
    function showPano(latlng)
    {
       if(latlng != null)
       {
          // The street view can be rendered 
          
          // Center the map to the new point
          try
          {
          map.setCenter(latlng);
          } catch(err) { }  
          
          // Render the polygon and current position marker &c.
          if(street_view_loc) map.removeOverlay(street_view_loc);
          street_view_loc = createMarker(latlng,'','',personIcon,'',3);
          map.addOverlay(street_view_loc);
          renderPolygon();
          
          
          // Create the lat lng object
          var to_eval = "var _latlng = new GLatLng"+latlng+";"
          eval(to_eval);
          street_view_geo_lat = _latlng.lat();
          street_view_geo_lng = _latlng.lng();
          
          
          // Define the parameters and create or re-render
          // the street view object
          var prop_pov = {yaw:current_yaw,pitch:current_pitch,zoom:current_zoom}
          panoramaOptions = { latlng:_latlng, pov:prop_pov };
          if(street_view_pano)
          {
             street_view_pano.setLocationAndPOV(_latlng,prop_pov);  
          }
          else
          {
             renderStreetView(_latlng,prop_pov);
          }
          
          // Add a click event to the map to center the map, move the marker
          // and attempt to load the street view to the new location
          GEvent.addListener(map, "click", function(marker, point) 
          {
              if(point != null && typeof(point) != 'undefined')
              {
                 street_view_client.getNearestPanoramaLatLng(point,showPano);
              }
          });
          
          // Make it all visible
          var street_view_obj = document.getElementById('street_view');
          street_view_obj.style.display = '';
       }
       else
       {
          // No point so remove the polygon, the current position
          // marker and hide the street view layer 
          if(polygon) map.removeOverlay(polygon);  
          if(street_view_loc) map.removeOverlay(street_view_loc); 
          var street_view_obj = document.getElementById('street_view');
          street_view_obj.style.display = 'none';
       }
    }
    
    
    function EOffsetBearing(point,dist,bearing) 
    {
       var latConv = point.distanceFrom(new GLatLng(point.lat()+0.1,point.lng()))*10;
       var lngConv = point.distanceFrom(new GLatLng(point.lat(),point.lng()+0.1))*10;
       var lat=dist * Math.cos(bearing * Math.PI/180)/latConv;
       var lng=dist * Math.sin(bearing * Math.PI/180)/lngConv; 
       return new GLatLng(point.lat()+lat,point.lng()+lng)      
    }
    
    
    function renderPolygon()
    {
       
       // Hide the tooltip
       tooltip.style.visibility="hidden"; 
       
       var polypoints = [];
       
       // Start point is street view marker
       polypoints[0] = street_view_loc.getPoint();
       var yaw = parseInt(current_yaw);
       var angle = 40;
       var angle_either_side = angle/2;
       var curvature = 10;
       var length = 500;
       
       
       // Define the triangle
       var left_vector = yaw-angle_either_side;
       if(left_vector < 0) left_vector = 360 + left_vector;
       var right_vector = yaw+angle_either_side;
       if(right_vector >= 360) right_vector = right_vector - 360;
       var left_stem_vertex = EOffsetBearing(polypoints[0],length,left_vector);
       var right_stem_vertex = EOffsetBearing(polypoints[0],length,right_vector);  
       
       
       // Set up the points
       polypoints[1] = left_stem_vertex;
       var segment_length = 36.6;
       for(var i = 0; i < curvature-1; i++)
       {
          polypoints[polypoints.length] = EOffsetBearing(polypoints[i+1],segment_length,left_vector+(80+(i*6.6)));    
       }
       polypoints[polypoints.length] = right_stem_vertex;
       

  
       if(polygon) map.removeOverlay(polygon);
       if(polypoints.length >= 3)
       {
          if(!polypoints[polypoints.length-1].equals(polypoints[0]))
          {
             polypoints[polypoints.length] = polypoints[0]; // Join
          }
          polygon = new GPolygon(polypoints, "#99BBDD", 2, 1, "#3880C0", 0.2);  
          map.addOverlay(polygon); 
          GEvent.addListener(polygon, "click", function(point) 
          {
             street_view_client.getNearestPanoramaLatLng(point,showPano);    
          });
       }
    }
    
    function onLocationChangeStreetView(latlng)
    {
       var point = new GLatLng(latlng.lat,latlng.lng);
       try
       {
       map.setCenter(point);
       } catch(err) { }  
       if(street_view_loc) map.removeOverlay(street_view_loc);
       street_view_loc = createMarker(point,'','',personIcon,'',3);
       map.addOverlay(street_view_loc,personIcon);
       renderPolygon();
    }
    
    function onYawChange()
    {
       var new_yaw = street_view_pano.getPOV().yaw;
       current_yaw = new_yaw;
       renderPolygon();
    }
    
    function onPitchChange()
    {
       var new_pitch = street_view_pano.getPOV().pitch;
    }
    
    function onZoomChange()
    {
       var new_zoom = street_view_pano.getPOV().zoom;
       current_zoom = new_zoom;
    }
       
