mxn.register('googlev3', {

	MapstractionNavigator : {

		init : function(map, api) {
			if (google.maps.DirectionsService) {
				this.navigators[api] = new google.maps.DirectionsService();
				this.maps[api] = map;
			}
		},

		navigate : function(request, options) {
			if (!request.from || !request.to) {
				return;
			}

			var me = this;
			var callback = function(request, options) {
				var navigator = me.navigators[me.api];
				var map = me.maps[me.api];
				var directionsRequest = {};

				if (!request.from || !request.to) {
					return;
				}

				if (request.from.location == "CURRENT_POSITION") {
					directionsRequest.origin = new google.maps.LatLng(me.currentLocation.point.lat,
							me.currentLocation.point.lng);
				} else if (request.from.point) {
					directionsRequest.origin = new google.maps.LatLng(request.from.point.latitude,
							request.from.point.longitude);
				} else {
					directionsRequest.origin = request.from.address;
				}

				if (request.to.location == "CURRENT_POSITION") {
					directionsRequest.destination= new google.maps.LatLng(me.currentLocation.point.lat,
							me.currentLocation.point.lng);
				} else if (request.to.point) {
					directionsRequest.destination = new google.maps.LatLng(request.to.point.latitude,
							request.to.point.longitude);
				} else {
					directionsRequest.destination = request.to.address;
				}

				directionsRequest.travelMode = options.travelMode;
				
				if(options.markerOptions && options.markerOptions.icon instanceof Object) {
					var icon = options.markerOptions.icon;
					var size = new google.maps.Size(icon.size.width, icon.size.height);
					var origin = new google.maps.Point(0,0);
					var anchor = new google.maps.Point(icon.anchor.x, icon.anchor.y);
					var icon = new google.maps.MarkerImage(icon.url, size, origin, anchor);
					options.markerOptions.icon = icon;
				}

				navigator.route(directionsRequest, function(result, status) {
					new google.maps.DirectionsRenderer( {
						'directions' : result,
						'map' : map.getMap(),
						'markerOptions' : options.markerOptions
					});
				});
			};

			if (request.from.location == "CURRENT_POSITION" || request.to.location == "CURRENT_POSITION") {
				this.getCurrentPosition(function() {
					callback(request, options);
				});
			} else {
				callback(request, options);
			}
		}
	}
});
