/*
  http://www.developer.com/lang/jscript/article.php/3615681
*/ 
function rad2deg (angle) {
    // Converts the radian number to the equivalent number in degrees  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/rad2deg
    // +   original by: Enrique Gonzalez
    // *     example 1: rad2deg(3.141592653589793);
    // *     returns 1: 180
    
    return (angle/Math.PI) * 180;
}

function deg2rad (angle) {
    // Converts the number in degrees to the radian equivalent  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/deg2rad
    // +   original by: Enrique Gonzalez
    // *     example 1: deg2rad(45);
    // *     returns 1: 0.7853981633974483
    
    return (angle/180)*Math.PI;
}

 function searchLocationGoogleGeocodeAndAddMap(mapDiv){  
    var address = document.getElementById( 'LocationDistance_address' ).value;
    if( address == '' ){
        return;
    }
    document.getElementById( mapDiv ).innerHTML = 'Creating map...';
    var geocoder = new GClientGeocoder();
    document.getElementById( mapDiv ).innerHTML = 'Looking up address...';
    
    geocoder.getLocations(address,
    function(response)
          {
    if (!response) {
           document.getElementById( mapDiv ).innerHTML = 'Failure: Unable to lookup address...';
           alert( "Address: \"" + address + "\" can't be found.  Try being more specific.  Use your full mailing address if necessary (other people will not see your address)");
    }
                else {
            place = response.Placemark[0];
            point = new GLatLng( place.Point.coordinates[1], place.Point.coordinates[0]);    
            // Update hidden fields with geocoding( NewItem_longitude, NewItem_latitude)
            document.getElementById( 'LocationDistance_latitude' ).value = point.lat();
            document.getElementById( 'LocationDistance_longitude' ).value = point.lng();
              
            // get bounds
            R = 3963;  // earth's radius, mi
            rad = document.getElementById( 'LocationDistance_distance' ).value;
            north = point.lat() - rad2deg(rad/R);
            south = point.lat() + rad2deg(rad/R);
            west = point.lng() - rad2deg( rad/R/Math.cos( deg2rad( point.lat() ) ) );
            east = point.lng() + rad2deg(rad/R/Math.cos(deg2rad(point.lat())));
            sw = new GLatLng( south, west );
            ne = new GLatLng( north, east );
            rect = new GLatLngBounds(sw, ne);
            
            
            // SHow a map
            var map = new GMap2(document.getElementById( mapDiv ));
            map.setUIToDefault();
            
            zoom = map.getBoundsZoomLevel( rect );
                        
            map.setCenter( point, zoom);
            
            
            var options = { 
            //draggable: true,
            title: "Search location center",
            bouncy: false
            };
            var marker = new GMarker(point, options);

            
            map.addOverlay( marker );
            //map.setUIToDefault();
            var accuracy = place.AddressDetails.Accuracy;
            var simpleAddress = 'Unknown';
            try
            {
              var city = place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
              var state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
              simpleAddress = city + ', ' + state;
            }
            catch(err)
            {
              simpleAddress = place.address;
            }
            document.getElementById( 'LocationDistance_general_address' ).value = simpleAddress;
            // alert( "Others will see: " + simpleAddress );
            
            try{
              addSaleMarkers( map );
            }
            catch(err)
            {
            
            }
    }
    }
    );
  }
