/* DIRECTIONS */
	
	//google.load("maps", "2",{"other_params":"sensor=true"});

	var _destination = "15 Fostertown Road, Medford, NJ 08055";
	var _predestination = "South White Horse Pike, Audubon NJ 08106";
	var _map_canvas;
	var _directions;
	
	function initialize() {
		if (GBrowserIsCompatible()) {
			
			_map_canvas = new GMap2(document.getElementById("map_canvas"));
			_map_canvas.addControl(new GMapTypeControl());
			/*
			var infoWindow = "<p><b>Big League Dreams</b><br/> "+_destination+"</p>";
			_map_canvas.openInfoWindowHtml(
				new GLatLng(39.919964, -74.808226), 
				infoWindow, 
				{ "maxWidth":150 }
			);
			*/
			_directions = new GDirections(_map_canvas, document.getElementById("directions"));
			GEvent.addListener(_directions, "error", _handleErrors);
			GEvent.addListener(_directions, "addoverlay", _overlayHandler);
			
			_map_canvas.setCenter(new GLatLng(0,0),0);
			getDirections(_predestination);
			_origination();
			
		}
	}
	
	
	
	
	function getDirections(from) {
		document.getElementById("errors").innerHTML = "";
		_directions.load("from: " + from + " to: " + _destination, { "locale": "en_US", "getSteps": true});
	}
	
	
	
	function _origination() {
		document.getElementById("data_input").innerHTML = ('<form onsubmit="getDirections(this.from.value); return false"><input type="text" size="50" name="from" value="'+_predestination+'"/> &nbsp; <input type="submit" value="Get Directions!"/></form>');
	}
	
	
	
	function _handleErrors() {
		var errors = "";
		
		if (_directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS) errors = ("No corresponding geographic location could be found for one of the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + _directions.getStatus().code);
		
		else if (_directions.getStatus().code == G_GEO_SERVER_ERROR) errors = ("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + _directions.getStatus().code);
		
		else if (_directions.getStatus().code == G_GEO_MISSING_QUERY) errors = ("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + _directions.getStatus().code);
		
		else if (_directions.getStatus().code == G_GEO_BAD_KEY) errors = ("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + _directions.getStatus().code);
		
		else if (_directions.getStatus().code == G_GEO_BAD_REQUEST) errors = ("A directions request could not be successfully parsed.\n Error code: " + _directions.getStatus().code);
		
		else errors = ("An unknown error occurred.");
		
		document.getElementById("errors").innerHTML = errors;
	}
	
	
	
	var _markers = [];
	var _coordinates = [];
	var _icons = [];
	
	function _overlayHandler() {
		
		for (var i = 0; i<_markers.length; i++) {
			_map_canvas.removeOverlay(_markers[i]);
		}
		
		for (var i = 0; i <= _directions.getNumRoutes(); i++) {
			
			var originalMarker = _directions.getMarker(i);
			_coordinates[i] = originalMarker.getLatLng();
			_icons[i] = originalMarker.getIcon();
			
			_markers[i] = new GMarker( 
				_coordinates[i], 
				{ icon: _icons[i], draggable: true, title: "Draggable"} 
			);
			_map_canvas.addOverlay(_markers[i]);
			
			GEvent.addListener(
				_markers[i], 
				"dragend", 
				function() {
					var _points = [];
					for (var i = 0; i < _markers.length; i++){
						_points[i]= _markers[i].getLatLng();
					}
					_directions.loadFromWaypoints(_points);
				}
			);
			
			_copyClick(_markers[i],originalMarker);
			_map_canvas.removeOverlay(originalMarker);
		}
		
		function _copyClick(newMarker,oldMarker) {
			GEvent.addListener(
				newMarker, 
				"click", 
				function() { GEvent.trigger(oldMarker, "click"); }
			);
		}
		
	}
	
	
	
	
	