// this script is currently in a very primitive state
// it can map-match a trace more or less, but I need 
// to start storing data somewhere



// variables prefixed with $ are global
// all others are local to their functions, or should be

// abreviated document elements that may be used a lot
var $d; // document
var $b; // body element
var $m; // map object
// coordinate string
var $c = [];


// initialize onload by getting some global elements, then pass the ball off
function start(){
	$d = document;
	$b = $d.getElementsByTagName('body')[0];
	makeTheMap();
}

// creates the map
function makeTheMap(){
	// leaflet: start map at a random location
	$m = L.map('map',{
		'center': randomCenter(),
		'zoom': 16,
		'zoomControl': false,
		'attributionControl':false,
		'animate':true
	});
	// disable all map movement
	$m.dragging.disable();
	$m.touchZoom.disable();
	$m.doubleClickZoom.disable();
	$m.scrollWheelZoom.disable();
	$m.keyboard.disable();
	// use OSM tiles for now
	L.tileLayer($tilesource).addTo($m);
	// add n event listener for clicks
	$m.on('click',trackCursor)
}

// start tracking finger movement
function trackCursor(event){
	// stop listening for clicks
	$m.off('click',trackCursor);
	// listen for mouse movement
	$m.on('mousemove',addCoordinate);
	// listen for clicks in a different way
	$m.on('click',closeTrack);
}

function closeTrack(){
	$m.off('mousemove',addCoordinate);
	$m.off('click',closeTrack);
	$m.on('click',trackCursor);
	mapMatch($c)
	$c = []
}

// append this event's coordinate object to the list
function addCoordinate(event){
	// right now the sampling rate is too high
	// so I am artificially lowering it by sampling
	if(Math.random() > 0.9){
		$c.push(event.latlng)
	}
}

// send the coordinates to be matched on the cloud
function mapMatch(coords){
	// make list into pairs of coordinate strings
	var c = [];
	var radii = [];
	for(i=0;i<coords.length;i++){
		c.push(coords[i].lng+','+coords[i].lat);
		radii.push(30);
	}
	var r = new XMLHttpRequest();
	URL = $OSRMserver + '/match/v1/transit/';
	URL += c.join(';');
	URL += '?geometries=geojson&overview=full';
	URL += '&radiuses='+radii.join(';');
	r.open('get',URL,true);
	r.onreadystatechange = function(){
		if(r.readyState == 4){ // finished
			if(r.status == 200){ // got good response
				console.log('match returned');
				var data = JSON.parse(r.responseText);
				var matchGeom = data.matchings[0].geometry
				L.geoJson(matchGeom).addTo($m)
			}
		}
	}
	r.send();
}


// project and unproject points. mask for overly long names
// lat-lon to pixel
function lToP(latLngObject){
	return $m.latLngToContainerPoint(latLngObject);
}
// pixel to lat-lon
function pToL(point){
	return $m.containerPointToLatLng(point);
}

// calculate euclidean distance from two perpendicular lengths
function euclid(a,b){
	return Math.sqrt( Math.pow(a,2) + Math.pow(b,2) );
}


@@ -392,16 +392,114 @@
 c = %5B%5D;%0A
+// constantly updated current lat/lon of cursor on the map%0Avar $cursorLocation;%0Avar $cursorTimer;%0A
 %0A%0A// ini
@@ -1178,16 +1178,105 @@
 Cursor)%0A
+%09// monitor cursor position%0A%09$m.on('mousemove',function(e)%7B$cursorLocation = e.latlng%7D);%0A
 %7D%0A%0A// st
@@ -1401,53 +1401,68 @@
 %09// 
-listen for mouse movement%0A%09$m.on('mousemove',
+start sampling cursor locations%0A%09$cursorTimer = setInterval(
 addC
@@ -1466,24 +1466,28 @@
 ddCoordinate
+,100
 );%0A%09// liste
@@ -1577,40 +1577,34 @@
 )%7B%0A%09
-$m.off('mousemove',addCoordinate
+clearInterval($cursorTimer
 );%0A%09
@@ -1700,38 +1700,23 @@
 end 
-this event's coordinate object
+cursor location
  to 
@@ -1751,161 +1751,36 @@
 ate(
-event)%7B%0A%09// right now the sampling rate is too high%0A%09// so I am artificially lowering it by sampling%0A%09if(Math.random() %3E 0.9)%7B%0A%09%09$c.push(event.latlng)%0A%09%7D
+)%7B%0A%09$c.push($cursorLocation)
 %0A%7D%0A%0A

set fixed sampling rate for cursor position