// JavaScript Document
// Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        
		var index = 0;
		var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20.5, 21.5);
        baseIcon.shadowSize = new GSize(20.5, 21.5);
        baseIcon.iconAnchor = new GPoint(0, 15);
        baseIcon.infoWindowAnchor = new GPoint(10, 10);
		
		
     var map = null;
     var geocoder = null;
	 var gdir = null;
	 //Used for markers display
	 var myAddTxt = new Array();
	 var myAddLong = new Array();
	 var myAddLat = new Array();	 
	 var myAdd = new Array();
	 //-------------------------------
	 var delay = 220; //Delay used when reverse geocoding is used
     var theZoom = 5;
     var idx = 0;
	 var useDir = false;
	 var finishedGMaps = false;
	 var initiaCoords = null;
	 
	 
	 

        // Creates a marker whose info window displays the letter corresponding
        // to the given index.
        function createMarker(point, index, text, useLetterIcons) {
          // Create a lettered icon for this point using our icon class
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
          var letteredIcon = new GIcon(baseIcon);
          //var letteredIconsingle = new GIcon2(baseIcon1);
          letteredIcon.image = "/images/bluemarkerdot.png";
        //" + letter + "
          //letteredIconsingle.image = "/images/bluemarker.png";

          // Set up our GMarkerOptions object
          markerOptions = { icon:letteredIcon };
          //markerOptions2 = ( icon:letteredIconsingle );
          var marker = null;
		  if (useLetterIcons)
		     marker =  new GMarker(point, markerOptions);
		  else
		      marker =  new GMarker(point, markerOptions);
		

        //This event opens the bubble in Google Maps 
	GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml(text);
         });
          return marker;
        }

       function createMarkerByAddress(address, text, useIcons)
		{			
			  if (geocoder)
			   {
		  
		         geocoder.getLocations(address, 
					 function(response) {
     					
     					 if (!response || response.Status.code != 200) {
       						//alert(response.Status.code);
      					} else {
       							 place = response.Placemark[0];
        						point = new GLatLng(place.Point.coordinates[1],
                           		 place.Point.coordinates[0]);
								 var marker =  createMarker(point, index, text,useIcons);
							     map.addOverlay(marker);
       
							
							
        
      					}
						 index++;
						 if (!useIcons)
						     window.setTimeout(geocodeAll, delay);
					    else
						     window.setTimeout(geocodeAllIcons, delay);
    				}
          			
		     	);
			
		   }
		}
     function geocodeAll() {
	 if (index < myAdd.length) {
        createMarkerByAddress(myAdd[index], myAddTxt[index]);
      }
    }
	
	 function geocodeAllIcons()
	 {
	   if (index < myAdd.length) {
        		createMarkerByAddress(myAdd[index], myAddTxt[index],true);
      		}
      
    
   
        }
	


  function CreateMarkersByGeocode(useChars)
      {
        var i;
         for(i = 0; i < myAddLat.length; i++)
         {
            var place = new GLatLng(myAddLat[i], myAddLong[i]);
            var marker =  createMarker( place, i, myAddTxt[i],useChars);
			map.addOverlay(marker);
			   
         
         }
         
      
      }	
	
	

function positionToAddress(address) {
	
	positionToAddress(address , null,8);
    
	
}
    

function positionToAddress(address , postbackItem, zoom) {
      
      if (geocoder) {
		 if (zoom) 
		     theZoom = zoom;      
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
			map.setCenter(point, theZoom);
					
            	 
			  if (postbackItem != null)	
			  {
				
			     __doPostBack(postbackItem,'');
			  }
				 
            }
          }
        );
      }
    }
	
	
	 function positionDropDown(drp)
	   {
		  if(drp.selectedIndex > 0)
		  {
		  //Modified for the findhome page
		       positionToAddress(drp.options[drp.selectedIndex].text,  drp.name,9);			  
		  }
		  else
		  {
		       map.setCenter(initialCoords, 6);
		       __doPostBack(drp.name,'');
		  }
	   
	   }
	
	 
	   
	   
	   function GetNeighborhoodHTML( cityCode, neighborhoodID , address, logo, name, phone, pricerange)
	   {
	      var html ='<table width=280px><tr>  <td colspan=2 align=left> <b>'+  name +'</b> </td></tr><td colspan=2 align=left> <a href=../Neighborhoods/Main.asp?CityCode=' + cityCode + '&NeighborhoodID=' +  neighborhoodID + ' class=SearchResults>   Visit Neighborhood   </a>  </td>   </tr>   <tr>  <td valig=top style=width:155px><b>Sales Center</b><br>' + address +'<br>'+ phone +'<br><br>'+ pricerange +'</td> <td valign=top>  <img alt="" src=../images/' + logo + '> </td>  </tr>	   </table>';
	   
	      return html;
	   }
	   
	   
	    function setDirections(fromAddress, toAddress, locale)
		{
     		 gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    	}
		
		
		function handleErrors()
		{
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}
	
	  function initialize(add)
	  {
	
	
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
		 //map.addControl(new GSmallMapControl());      
           geocoder = new GClientGeocoder();
	       positionToAddress(add, null,9);
		  }
		  
    }
     function initialize(lat, lon)
	  {
	

      if (GBrowserIsCompatible())
       {
          map = new GMap2(document.getElementById("map_canvas"));
        
		 //map.addControl(new GSmallMapControl());      
           geocoder = new GClientGeocoder();
	       map.setCenter(new GLatLng(lat, lon), 9);
		   CreateMarkersByGeocode(true);
       
		  }
		  
    }
	
		
		

    
	   
	   