function prepareMapSearchTo()
{
    if (GBrowserIsCompatible())
    {        
        map_to = new GMap2(document.getElementById('gmap_to'));
        map_to.setCenter(new GLatLng(42.940339,13.447266), 2);
        map_to.disableDoubleClickZoom();
        map_to.enableContinuousZoom();
        map_to.enableScrollWheelZoom();

        gdir_to = new GDirections(map_to, null);
        GEvent.addListener(gdir_to, 'error', handleErrors);

        $('#findAddressTo').click(function(){
				  if (geocoder) {
					geocoder.getLatLng(
					  $('#geocode_address_to').val(),
					  function(point) {
						if (point) {
						  map_to.setCenter(point, 13);
						  var marker = new GMarker(point);

                            newMarkersTo[0] = new GMarker(point, {
                                draggable :true,
                                title :'Draggable'
                            });
                            drawRouteTo();

                          map_to.clearOverlays();
						  map_to.addOverlay(marker);

                          if(newMarkersFrom[0])
                            calculateDistance(newMarkersFrom[0].getLatLng(), marker.getLatLng());
						  //marker.openInfoWindowHtml(address);
						}
					  }
					);
				  }
                
                return false;
        });        
		
		$('#geocode_address_to').bind('keypress', function(e) {
			var code = (e.keyCode ? e.keyCode : e.which);
			if(code == 13) 
			{
				$('#findAddressTo').click();
			}		
		});

    }
}

function drawRoutesTo()
{
	GEvent.addListener(map_to, 'click', onGMap2ClickTo);
	GEvent.addListener(gdir_to, 'addoverlay', onGDirectionsAddOverlayTo);
}

var odd = false;

function onGMap2ClickTo(overlay, latlng, overlaylatlng)
{
	if (overlay != null) return;

	var i = 0;
	//odd = !odd;
	
	if (newMarkersTo.length >= 1)
	{
		map_to.removeOverlay(newMarkersTo[i]);
	}
	
	newMarkersTo[i] = new GMarker(latlng, {
		draggable :true,
		title :'Draggable'
	});

    map_to.clearOverlays();
	map_to.addOverlay(newMarkersTo[i]);

    if(newMarkersFrom[0])
        calculateDistance(newMarkersFrom[0].getLatLng(), newMarkersTo[0].getLatLng());

	drawRouteTo();
}

var pointNamesTo = ['where'];
var newMarkersTo = [];
var latLngsTo = [];
var iconsTo = [];

function drawRouteTo()
{
	jQuery('#geocode_address_to input[type="hidden"][name!="map"]').remove();
	//if (newMarkers.length < 1) return;
	var points = [];
	for (var i = 0; i < newMarkersTo.length; i++)
	{
		points[i] = newMarkersTo[i].getLatLng();
		jQuery('#geocode_address_to').after(
			'<input type="hidden" name="point_to[latitude]" value="' + newMarkersTo[i].getLatLng().lat() + '" />' +
			'<input type="hidden" name="point_to[longitude]" value="' + newMarkersTo[i].getLatLng().lng() + '" />');
	}
	//gdir.loadFromWaypoints(points);
}
              /*
function setDirections(fromAddress, toAddress, locale)
{
	gdir.load("from: " + fromAddress + " to: " + toAddress, {
		"locale": locale, 
		"getSteps": true
	});
}
             */


function onGDirectionsAddOverlayTo() {
	for ( var i = 0; i < newMarkersTo.length; i++)
	{
		map_to.removeOverlay(newMarkersTo[i]);
	}

	for (var i = 0; i <= gdir_to.getNumRoutes(); i++)
	{
		var originalMarker = gdir_to.getMarker(i);
		latLngsTo[i] = originalMarker.getLatLng();
		iconsTo[i] = originalMarker.getIcon();
		newMarkersTo[i] = new GMarker(latLngsTo[i], {
			icon :iconsTo[i],
			draggable :true,
			title :'Draggable'
		});
		map_to.addOverlay(newMarkersTo[i]);

		GEvent.addListener(newMarkersTo[i], "dragend", drawRouteTo);

		copyClickTo(newMarkersTo[i], originalMarker);

		map_to.removeOverlay(originalMarker);
	}

	function copyClickTo(newMarker, oldMarker)
	{
		GEvent.addListener(newMarker, 'click', function() {
			GEvent.trigger(oldMarker, 'click');
		});
	}
}
