﻿// **************************************************************************
// * JScript File: PropertyMap_Google_Maps_1.js                             *
// **************************************************************************
// * Extracts all code related with construction/presentation of Google Map *
// * for listing.                                                           *
// **************************************************************************
// * Rafael E. Martinez                 - Created on: June 29, 2007         *
// **************************************************************************
<!--       
    if (GBrowserIsCompatible())
    {	                                                                       
        if (latitude == 0 || longitude == 0)
        {            
            // Try to get the coordinates using listing's address...
            var geocoder = new GClientGeocoder();
            var p = geocoder.getLatLng(parseAddress(address), getPoint);
        }
        else
        {
            // We have listing's coordinates...
            var geopoint = new GLatLng(latitude, longitude, true);
                                                      
            // Show the map since we have the listing's coordinates...
            showMap(geopoint);
        }	            	            
    }
        
	function showMap(punto)
    {
        // 1.- Default map (blocks and streets)...
	    var map = new GMap2(document.getElementById("dmap"));			
	    map.addControl(new GLargeMapControl());			
	    map.addControl(new GMapTypeControl());
	    map.addControl(new GScaleControl());
	    map.setCenter(punto, 13);
	    map.setZoom(map.getZoom() + 1);
						
	    // 2.- Create a tiny marker icon on the point...
	    var icon = new GIcon();
	    icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	    icon.iconSize = new GSize(12, 20);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(6, 20);
	    icon.infoWindowAnchor = new GPoint(6, 6);
		
	    // 3.- Show the information window with address, above the point...
	    var marker = new GMarker(map.getCenter(), icon, true);
	    map.addOverlay(marker);
	    marker.openInfoWindowHtml(address);	    
    }    
    
    function parseAddress(address)
    {
        var retval = address;	        
        var w_add = "";
        
        var array = address.split("<br>");
        	        
        for (var i = 0; i < array.length; i++)
        {
            w_add += array[i] + " ";
        }
        
        if (w_add != "")
        {	            
            retval = w_add;
        }
        
        return retval;
    }
    
    function getPoint(point)
    {                            
        var punto;	        
        if (point)
        {
            punto = new GLatLng(point.lat(), point.lng(), true);      
            
            // Show the map since we got the listing's coordinates...                          
            showMap(punto);
            
            // Now, save the coordinates into our database...
            storeCoordinates(punto);
        }
        else
        {                
            var msg = "<div style='padding:20px'><b>Our apologies!</b><br /><br />Map and satellite view are not available for this address.</div>";
            document.getElementById("dmap").innerHTML = msg;                
        }                        
    }    

    function storeCoordinates(punto)
    {
        try
        {                                                	            
            AjaxService.PersistLCCoordinates(propertyid, punto.lat(), punto.lng(), "Google Maps API", OKAY, NOOKAY);
        }
        catch(e)
        {                
            alert("ERR:LC" + propertyid);
        }	        
    }
    
    function OKAY (results, context, methodName)
    {
        var ret = results;            
    }
            
    function NOOKAY (error, context, methodName)
    {
         alert("ERR:WBSVC " + error.get_message());
    }
// -->