/*
var submit = false;                         

$(document).ready(function() {
    $('#search_form').submit(function(){    
        alert(submit);
        return false;
        return submit;
    });
});
*/
var geocoder = null;

function formSubmit(type)
{
    $('#route_type').val(type);
    
    if($('input[name="point_from[latitude]"]').val() == undefined || $('input[name="point_to[latitude]"]').val() == undefined)
    {
        $("#message2").children('p').html(i18n[5]);
        $("#message2").dialog('option', 'title', i18n[4]).dialog('open');
        
        return false;
    }
    else
    {
        $('#search_form').submit();
    }
}

function prepareMapSearchFrom()
{
    if (GBrowserIsCompatible())
    {       
		geocoder = new GClientGeocoder();
		
        map_from = new GMap2(document.getElementById('gmap_from'));
        map_from.setCenter(new GLatLng(42.940339,13.447266), 2);
        map_from.disableDoubleClickZoom();
        map_from.enableContinuousZoom();
        map_from.enableScrollWheelZoom();

        gdir_from = new GDirections(map_from, null);
        GEvent.addListener(gdir_from, 'error', handleErrors);
        
        $('#findAddressFrom').click(function(){
				  if (geocoder) {
					geocoder.getLatLng(
					  $('#geocode_address_from').val(),
					  function(point) {
						if (point) {
						  map_from.setCenter(point, 13);
						  var marker = new GMarker(point);

                            newMarkersFrom[0] = new GMarker(point, {
                                draggable :true,
                                title :'Draggable'
                            });
                            drawRouteFrom();
                            
                          map_from.clearOverlays();
						  map_from.addOverlay(marker);

                          if(newMarkersTo[0])
                            calculateDistance(marker.getLatLng(), newMarkersTo[0].getLatLng());
						  //marker.openInfoWindowHtml(address);
						}
					  }
					);
				  }
                
                return false;

        });
        
    }
}

function drawRoutesFrom()
{
	GEvent.addListener(map_from, 'click', onGMap2ClickFrom);
	GEvent.addListener(gdir_from, 'addoverlay', onGDirectionsAddOverlayFrom);
}

var odd = false;

function onGMap2ClickFrom(overlay, latlng, overlaylatlng)
{
	if (overlay != null) return;
	
	var i = 0;
	//odd = !odd;
	
	if (newMarkersFrom.length >= 1)
	{
		map_from.removeOverlay(newMarkersFrom[i]);
	}
	
	newMarkersFrom[i] = new GMarker(latlng, {
		draggable :true,
		title :'Draggable'
	});
    
	map_from.clearOverlays();
	map_from.addOverlay(newMarkersFrom[i]);
    
    if(newMarkersTo[0])
        calculateDistance(newMarkersTo[0].getLatLng(), newMarkersFrom[0].getLatLng());

	drawRouteFrom();
}

var pointNamesFrom = ['where'];

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

function onGDirectionsAddOverlayFrom() {
	for ( var i = 0; i < newMarkersFrom.length; i++)
	{
		map_from.removeOverlay(newMarkersFrom[i]);
	}

	for (var i = 0; i <= gdir_from.getNumRoutes(); i++)
	{
		var originalMarker = gdir_from.getMarker(i);
		latLngsFrom[i] = originalMarker.getLatLng();
		iconsFrom[i] = originalMarker.getIcon();
		newMarkersFrom[i] = new GMarker(latLngsFrom[i], {
			icon :icons[i],
			draggable :true,
			title :'Draggable'
		});
		map_from.addOverlay(newMarkersFrom[i]);

		GEvent.addListener(newMarkersFrom[i], "dragend", drawRouteFrom);

		copyClickFrom(newMarkersFrom[i], originalMarker);

		map_from.removeOverlay(originalMarker);
	}

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

function calculateDistance(glatlng1, glatlng2)
{
    var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
    var kmdistance   = (miledistance * 1.609344).toFixed(1);
    var radius       = kmdistance / 10;

	if(radius <= 1)
		radius = 1;
	
    $('input[name=radius_from]').val(radius.toFixed(0));
    $('input[name=radius_to]').val(radius.toFixed(0));
}
