+ }
+
+ var engines = OSM.Directions.engines;
+
+ engines.sort(function (a, b) {
+ a = I18n.t('javascripts.directions.engines.' + a.id);
+ b = I18n.t('javascripts.directions.engines.' + b.id);
+ return a.localeCompare(b);
+ });
+
+ var select = $('select.routing_engines');
+
+ engines.forEach(function(engine, i) {
+ select.append("<option value='" + i + "'>" + I18n.t('javascripts.directions.engines.' + engine.id) + "</option>");
+ });
+
+ setEngine('osrm_car');
+
+ select.on("change", function (e) {
+ chosenEngine = engines[e.target.selectedIndex];
+ if (map.hasLayer(polyline)) {
+ getRoute();
+ }
+ });
+
+ $(".directions_form").on("submit", function(e) {
+ e.preventDefault();
+ $("header").addClass("closed");
+ getRoute();
+ });
+
+ $(".routing_marker").on('dragstart', function (e) {
+ e.originalEvent.dataTransfer.effectAllowed = 'move';
+ e.originalEvent.dataTransfer.setData('id', this.id);
+ var xo = e.originalEvent.clientX - $(e.target).offset().left;
+ var yo = e.originalEvent.clientY - $(e.target).offset().top;
+ e.originalEvent.dataTransfer.setData('offsetX', e.originalEvent.target.width / 2 - xo);
+ e.originalEvent.dataTransfer.setData('offsetY', e.originalEvent.target.height - yo);
+ });
+
+ var page = {};
+
+ page.pushstate = page.popstate = function() {
+ $(".search_form").hide();
+ $(".directions_form").show();
+
+ $("#map").on('dragend dragover', function (e) {
+ e.preventDefault();
+ });
+
+ $("#map").on('drop', function (e) {
+ e.preventDefault();
+ var oe = e.originalEvent;
+ var id = oe.dataTransfer.getData('id');
+ var pt = L.DomEvent.getMousePosition(oe, map.getContainer()); // co-ordinates of the mouse pointer at present
+ pt.x += Number(oe.dataTransfer.getData('offsetX'));
+ pt.y += Number(oe.dataTransfer.getData('offsetY'));
+ var ll = map.containerPointToLatLng(pt);
+ endpoints[id === 'marker_from' ? 0 : 1].setLatLng(ll);
+ getRoute();
+ });
+
+ var params = querystring.parse(location.search.substring(1)),
+ route = (params.route || '').split(';');
+
+ if (params.engine) {
+ setEngine(params.engine);
+ }
+
+ var o = route[0] && L.latLng(route[0].split(',')),
+ d = route[1] && L.latLng(route[1].split(','));
+
+ if (o) endpoints[0].setLatLng(o);
+ if (d) endpoints[1].setLatLng(d);
+
+ map.setSidebarOverlaid(!o || !d);
+
+ getRoute();
+ };
+
+ page.load = function() {
+ page.pushstate();
+ };
+
+ page.unload = function() {
+ $(".search_form").show();
+ $(".directions_form").hide();
+ $("#map").off('dragend dragover drop');
+
+ map
+ .removeLayer(popup)
+ .removeLayer(polyline)
+ .removeLayer(endpoints[0].marker)
+ .removeLayer(endpoints[1].marker);