// JavaScript Document


	
/////////////////////////////////////
///////  DIRECTIONS STUFF   /////////
	
	var directions_visible = 0;
	var marker;
	var from = '';
	var to = new GLatLng();
	var directions = new GDirections();
	var marker_on = 0;

	function clear_directions() {
		
		directions.clear();
		document.getElementById("lower_panel").innerHTML = '';
		directions_visible = 0;
		map.returnToSavedPosition(last_map);
	}
	
	
	function create_dir_marker() {
		if(marker_on == 1) {map.removeOverlay(marker);}	
		if(directions_visible == 1) {clear_directions();}	
		var center = new GLatLng();
		center = map.getCenter(); 
		to = new GLatLng();
		to = map.getCenter();

       marker = new GMarker(center, {draggable: true});
	   var infowindow = '<small>Drag to the road closest to your destination, enter a place and go!<p /><form  name="get_directions" action="map.d.php" target="_blank" method="post"><input type="text" SIZE=30 MAXLENGTH=40 name="from_loc" id="from_loc" /><input type="hidden" name="d_lat" id="d_lat" type="text" value="' + to.lat() + '"/><input type="hidden" name="d_lng" id="d_lng" type="text" value="' + to.lng() + '"/> <INPUT value="Get Directions" TYPE="SUBMIT"> </form></small>';

        GEvent.addListener(marker, "dragstart", function() {
		from = document.getElementById("from_loc");
		from = from.value;
        map.closeInfoWindow();
        });

        GEvent.addListener(marker, "dragend", function() {
		to = marker.getLatLng();
          marker.openInfoWindowHtml('<small>Drag to the nearest road, enter a place and go!<br/><br/><form name="get_directions" action="map.d.php" target="_blank" method="post"><input type="text" SIZE=30 MAXLENGTH=40 name="from_loc" id="from_loc" value = "' + from + '" /><input type="hidden" name="d_lat" id="d_lat" type="text" value="' + to.lat() + '"/><input type="hidden" name="d_lng" id="d_lng" type="text" value="' + to.lng() + '"/>  <INPUT value="Get Directions" TYPE="SUBMIT"> </form></small>');
		document.getElementById("from_loc").select();

        });

        map.addOverlay(marker);
		marker_on = 1;
		to = marker.getLatLng();
		var dir_wind = marker.openInfoWindowHtml(infowindow);
		var close_remove = GEvent.addListener(map.getInfoWindow(), "closeclick", function() {map.removeOverlay(marker);
				GEvent.removeListener(close_remove);});
		var close_remove_onmap = GEvent.addListener(map, "click", function() {map.removeOverlay(marker);
				GEvent.removeListener(close_remove_onmap);});
		document.getElementByName("from_loc").select();
	}
	
	var dir_string = '';
	var last_map;
	function get_directions(lat,lng,from) {
		map.setMapType(G_MAP_TYPE);
		last_map = map.savePosition();
		if(directions_visible == 0) {
		document.getElementById("lower_panel").innerHTML = '<div align="center"><small><hr><table width="95%" border="0" cellpadding="0"><tr><td><div align="left"><a href="javascript:route_gpx()()">Download directions for GPS</a> (.gpx file)</div></td><td><div align="right"><a href="javascript:clear_directions()"><img src="../images/close.gif" /></a> <small>clear directions</div></td></tr></table> </small></div> <div id="route" align="left"></div><div id="gpx"></div>';	
		directions_visible = 1;}
		directionsPanel = document.getElementById("route");
		directions = new GDirections(map, directionsPanel);
if(from == 0) {
		var from_addr = document.getElementById("from_loc");
		from_addr = from_addr.value; } else {from_addr = from;}
		dir_string = from_addr + " to " + lat + ", " + lng;
    	directions.load(dir_string);
		document.getElementById("gpx").innerHTML = '<form name="print_directions" id="print_directions" action="print_directions.php" method="post" target="_blank"><input type="hidden" name="from" id="from"/></form>';
//		map.removeOverlay(marker);
		map.closeInfoWindow();
		marker_on = 0;
		}
	
	
	
	
	var gpx_data = '';
	
	function route_gpx() {
		var g_route = directions.getRoute(0); 
		var gpx_bounds = new GLatLngBounds();
		gpx_bounds = directions.getBounds();
		var sw = new GLatLng(gpx_bounds.getSouthWest());
		var ne = new GLatLng(gpx_bounds.getNorthEast());
		
		gpx_data = '<?xml version="1.0" standalone="yes"?><gpx version="1.1"  creator="roadless.js"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"  xmlns="http://www.topografix.com/GPX/1/1" ><metadata><desc>This file was created by http://www.roadlessland.org</desc><bounds maxlat="' + ne.lat() + '"  maxlon="' + ne.lng() + '"  minlat="' + sw.lat() + '"  minlon="' + sw.lng() + '" /></metadata>';
		
		gpx_data += '<rte>';

		for ( var i=0; i < g_route.getNumSteps(); i++ ) { 
				var step = g_route.getStep(i); 
				var pt = new GLatLng;
				pt = step.getLatLng();
				var lat = pt.lat();
				var lng = pt.lng();
				var description = step.getDescriptionHtml(); 
				gpx_data += '<rtept lat="' + lat + '"  lon="' + lng + '" >';
				gpx_data += '<name>' + description + '</name><desc>' + description + '</desc> <sym>Waypoint</sym></rtept>';
				gpx_data += '<number>' + i + '</number>';

			} 
		gpx_data += '</rte></gpx>';
		document.getElementById("gpx").innerHTML = '<form name="download_route_gpx" id="download_route_gpx" action="gpx.php" method="post"><input type="text" name="output" id="output"/></form>';
		var text = document.getElementById("output");
        text.value = "";
        text.value = gpx_data;
		document.download_route_gpx.submit();
	}
	
	
	
	
///////  DIRECTIONS STUFF   /////////
/////////////////////////////////////


