var gCourseReference=null,radius=null,A=null,B=null,indice=null,time=null,coeff=null,dg=null,position=null;function init_gCourseReference(a){gDbHelper.getPositions(a,fillgCourseReference)}function fillgCourseReference(a){gCourseReference=a}function distEuclidienne(a,b){var c=Math.pow(a.coords.longitude-b.coords.longitude,2)+Math.pow(a.coords.latitude-b.coords.latitude,2);return Math.sqrt(c)}function getTwoPointsDistance(c,d){var g=6378.155,b=.017453292519943295,i=c.coords.longitude*b,e=c.coords.latitude*b,j=d.coords.longitude*b,f=d.coords.latitude*b,h=j-i,a=Math.acos(Math.sin(e)*Math.sin(f)+Math.cos(e)*Math.cos(f)*Math.cos(h));if(a<0)a=a+Math.PI;a=a*g*1e3;if(isNaN(a))a=0;return a}function initTableaux(d,b){position=[];A=new Array(b);B=new Array(b);for(var a=0;a<b;a++){A[a]=new Array(d+1);B[a]=new Array(d)}for(var a=0;a<b;a++)for(var c=0;c<d;c++){A[a][c]=0;B[a][c]=0}for(var a=0;a<b;a++)A[a][d]=0}function initialisation(d){var b=2*d.length;tabGeodesique(d);position=[];A=new Array(b);B=new Array(b);for(var a=0;a<b;a++){A[a]=new Array(b);B[a]=new Array(b)}for(var a=0;a<b;a++)for(var c=0;c<b;c++){A[a][c]=0;B[a][c]=0}}function getDistances(a){var c=0;if(a.length>1)for(var b=0;b<a.length-1;b++)c+=distEuclidienne(a[b],a[b+1]);return c}function distGeodesique(b,d,e){var c=0;if(d<=e)for(var a=d;a<e;a++)c+=getTwoPointsDistance(b[a],b[a+1]);else for(var a=e;a<d;a++)c+=getTwoPointsDistance(b[a],b[a+1]);return c}function tabGeodesique(b){dg=new Array(b.length);for(var c=0,a=0;a<b.length;a++){c+=distGeodesique(b,0,a);dg[a]=c}}function getDistGeodesique(d,b,c){var a=0;if(b<=c)a=dg[c]-dg[b];else a=dg[b]-dg[c];return a}function algo1(f,d){for(var i=.7,a=f.length-1;a>0;a--)for(var b=0;b<d.length;b++){for(var e=Number.MAX_VALUE,h=0,c=b;c<d.length;c++){var k=getTwoPointsDistance(f[a],d[c]),l=getDistGeodesique(d,b,c),j=A[c][a+1],g=i*k+(1-i)*l+j;if(g<e){e=g;h=c}}A[b][a]=e;B[b][a]=h}}function algo2(d,a){for(var h=.7,b=0;b<a.length;b++){for(var e=Number.MAX_VALUE,g=0,c=b;c<a.length;c++){var j=getTwoPointsDistance(d[d.length-2],a[c]),k=getDistGeodesique(a,b,c);if(d.length>3)var i=A[c][d.length-3];else var i=0;var f=h*j+(1-h)*k+i;if(f<e){e=f;g=c}}A[b][a.length-2]=e;B[b][a.length-2]=g}}function recSolution(d,b){var c=[];indice=[];for(var f=Number.MAX_VALUE,a=0;a<b.length;a++){var e=getTwoPointsDistance(d[0],b[a])+A[a][1];if(e<f){f=e;indice[0]=a}}c[0]=b[indice[0]];for(var a=1;a<d.length;a++){indice[a]=B[indice[a-1]][a];c[a]=b[indice[a]]}return c}function recSolution2(b,c){var f=[];ind=0;for(var e=Number.MAX_VALUE,a=0;a<c.length;a++){var d=getTwoPointsDistance(b[b.length-1],c[a])+A[a][b.length-2];if(d<e){e=d;ind=a}}return ind}function getClosestWpt(a,b){var c=null;if(a!=b.length-1){a++;while(!b[a].isWaypoint&&a!=b.length)a++;if(a!=b.length)c=b[a]}return c}function timeToWpt(a,d){var c=null,b=null;c=distEuclidienne(a,d);b=c/a.coords.speed;return b}function distPointLigne(a,b,d){var i=null,c=null,h=a.coords.longitude-b.coords.longitude,g=a.coords.latitude-b.coords.latitude,e=a.coords.latitude-d.coords.latitude,f=a.coords.latitude-d.coords.latitude;s=Math.abs(e*h-f*g)/2;c=2*s/distEuclidienne(a,b);return c}function getAverageSpeed(f,b,c){for(var e=0,d=0,a=b;a<c;a++)e+=f[a].coords.speed.toString().repla;d=e/(c-b);return d}function checkReference2(g,h){var a=g.arrayPositions,b=g.arrayPositionsRef,c=0,e=0,d=0;b[0].isWaypoint=true;b[b.length-1].isWaypoint=true;algo2(a,b);c=recSolution2(a,b);position.push(b[c]);if(position[position.length-1].isWaypoint!=null&&getTwoPointsDistance(position[position.length-1],a[a.length-1])<30)d=a[a.length-1].timestamp-a[0].timestamp-(position[position.length-1].timestamp-position[0].timestamp);else{var i=getClosestWpt(c,b);e=getTwoPointsDistance(a[a.length-1],i);d=e/getAverageSpeed(a,0,a.length)*(getAverageSpeed(b,0,b.length)/getAverageSpeed(b,0,c+1))}h(e,d);if(c!=b.length&&c!=0){var f=0;f=distPointLigne(b[c-1],b[c],a[a.length-1])+distPointLigne(b[c],b[c+1],a[a.length-1]);f>60&&alertlost()}}function checkReference(k,m){var f=k.arrayPositions,c=k.arrayPositionsRef,a=null,j=0,i=0;time=0;c[0].isWaypoint=true;c[c.length-1].isWaypoint=true;var n=(new Date).getTime(),h=echantillonage(c),e=[];if(f.length<20)e=f;else{var g=19;while(g>=0){e[g]=f[f.length-20+g];g-=1}}initTableaux(e.length,h.length);algo1(e,h);a=recSolution(e,h);var o=(new Date).getTime();alert("time"+(o-n));var d=[];if(a[a.length-1].position+coeff>c.length){for(var b=0;b<coeff;b++){d[b]=c[a[a.length-1].position-coeff+b];d[b].position=a[a.length-1].position-coeff+b}for(var b=coeff;b<c.length;b++){d[b]=c[a[a.length-1].position+b];d[b].position=a[a.length-1].position+b}}else{for(var b=0;b<coeff;b++){d[b]=c[a[a.length-1].position-coeff+b];d[b].position=a[a.length-1].position-coeff+b}for(var b=coeff;b<2*coeff+1;b++){d[b]=c[a[a.length-1].position+b];d[b].position=a[a.length-1].position+b}}initTableaux(e.length,d.length);algo1(e,d);a=recSolution(e,d);if(a[a.length-1].isWaypoint!=null&&getTwoPointsDistance(a[a.length-1],f[f.length-1])<30)i=a[a.length-1].timestamp-a[0].timestamp-(c[a[a.length-1].postion].timestamp-c[a[0].postion].timestamp);else{var p=getClosestWpt(a[a.length-1].position,c);j=getTwoPointsDistance(f[f.length-1],p)}m(j,i);if(a[a.length-1].position!=c.length||a[a.length-1].position!=0){var l=0;l>60&&alertlost()}}function echantillonage(c){var b=[];coeff=Math.ceil(c.length/20);var a=coeff,d=0;while(a<c.length){b[d]=c[a];b[d].position=a;d++;a+=coeff}return b}function update(b,a){alert(b+"  "+a)}
