var map,track,photo,client,key,tracklist,photoicon=createPhotoIcon(""),cameraicon=createPhotoIcon("-gray"),progressbar=new ProgressBar;function createPhotoIcon(a){var c=new GIcon;c.image="img/camera"+a+".png";c.shadow="img/camera_shadow.png";c.iconSize=new GSize(34,34);c.shadowSize=new GSize(60,34);c.iconAnchor=new GPoint(17,17);c.infoWindowAnchor=new GPoint(30,1);return c}
function queryParam(a){var c=window.location.href;a=(new RegExp("[\\?&#]"+a+"=([^&#]*)")).exec(c);return a===null?"":decodeURIComponent(a[1])}
function onLoad(){var a,c,b,d,f;if(GBrowserIsCompatible()){map=new GMap2(document.getElementById("map"),{mapTypes:[G_HYBRID_MAP,G_SATELLITE_MAP,G_NORMAL_MAP,G_PHYSICAL_MAP]});map.setCenter(new GLatLng(51,10),6);map.setUIToDefault();map.addControl(new GOverviewMapControl);resizeMap();window.onresize=resizeMap;osmMap();client=queryParam("client");a=queryParam("region");c=queryParam("fileid");b=queryParam("tmp");d=queryParam("detect_pause");f=queryParam("optimize_track");tracklist=new Tracklist(document.getElementById("tracklistbody"),
a);c!==""?parseTrackFile(c,b,"",d,f):loadTrackListAndOverviewMap()}else document.getElementById("map").innerHTML='<p style="color:red">Sorry, your browser cannot display Google Maps!</p>'}
function resizeMap(){var a,c,b;if(window.innerHeight){a=window.innerWidth;c=window.innerHeight}else if(document.documentElement&&document.documentElement.clientHeight){a=document.documentElement.clientWidth;c=document.documentElement.clientHeight}else if(document.body){a=document.body.clientWidth;c=document.body.clientHeight}a-=350;if(a<500)a=500;c-=210;if(c<500)c=500;b=document.getElementById("map");b.style.width=a+"px";b.style.height=c+"px";map&&map.checkResize()}
function osmMap(){var a=new GCopyrightCollection("&copy; openstreetmap.org");a.addCopyright(new GCopyright(1,new GLatLngBounds(new GLatLng(-90,-180),new GLatLng(90,180)),1,"Data is licensed under the Creative Commons Attribution-Share Alike license"));a=new GTileLayer(a,1,18,{tileUrlTemplate:"http://tile.openstreetmap.org/{Z}/{X}/{Y}.png",isPng:true,opacity:1});a=new GMapType([a],new GMercatorProjection(20),"OSM");map.addMapType(a)}
function divUpload(){document.getElementById("divupload").style.display="inline";document.getElementById("divphotoupload").style.display="none";document.getElementById("divbusy").style.display="none";progressbar.stop();document.getElementById("divmenu").style.display="none";document.getElementById("divlocatephoto").style.display="none"}
function divPhotoUpload(){document.getElementById("divupload").style.display="none";document.getElementById("divphotoupload").style.display="inline";document.getElementById("divbusy").style.display="none";progressbar.stop();document.getElementById("divmenu").style.display="none";document.getElementById("divlocatephoto").style.display="none"}
function divBusy(){document.getElementById("divupload").style.display="none";document.getElementById("divphotoupload").style.display="none";document.getElementById("divmenu").style.display="none";document.getElementById("divbusy").style.display="inline";document.getElementById("divlocatephoto").style.display="none";progressbar.isRunning()||progressbar.start(document.getElementById("progressbar"))}
function divMenu(a){document.getElementById("divbusy").style.display="none";progressbar.stop();document.getElementById("divphotoupload").style.display="none";document.getElementById("divupload").style.display="none";if(a){document.getElementById("message").innerHTML=a;document.getElementById("message").style.display="inline"}else document.getElementById("message").style.display="none";if(track&&track.canUploadImage())document.getElementById("divphotouploadpossible").style.display="inline";else document.getElementById("divphotouploadpossible").style.display=
"none";if(track)document.getElementById("gpxdownload").style.display="inline";else document.getElementById("gpxdownload").style.display="none";if(track&&track.tdem)document.getElementById("demdata").style.display="inline";else document.getElementById("demdata").style.display="none";document.getElementById("divmenu").style.display="inline";document.getElementById("divlocatephoto").style.display="none"}
function divTrack(a){document.getElementById("tracklist").style.display="none";document.getElementById("trackinfo").style.display="inline";if(a===true)document.getElementById("divprofile").style.display="inline";else document.getElementById("divprofile").style.display="none";document.getElementById("divlocatephoto").style.display="none";if(track.getFirstPhotoHref())document.getElementById("divthumbnail").innerHTML='<img src="thumbnail.php?href='+track.getFirstPhotoHref()+'" id="thumbnail" name="thumbnail" alt="Thumbnail"/>';
else document.getElementById("divthumbnail").innerHTML=""}function divLocatePhoto(){document.getElementById("divupload").style.display="none";document.getElementById("divphotoupload").style.display="none";document.getElementById("divmenu").style.display="none";document.getElementById("divbusy").style.display="none";progressbar.stop();document.getElementById("divlocatephoto").style.display="inline"}
function divTracklist(){document.getElementById("tracklist").style.display="inline";document.getElementById("trackinfo").style.display="none";document.getElementById("divprofile").style.display="none";document.getElementById("divthumbnail").innerHTML=""}
function parseTrackFile(a,c,b,d,f){divBusy();GDownloadUrl("parser.php?fileid="+encodeURIComponent(a)+"&client="+encodeURIComponent(client)+"&tmp="+c+"&filename="+encodeURIComponent(b)+"&detect_pause="+d+"&optimize_track="+f+"&key="+encodeURIComponent(key),returnParseTrack)}
function returnParseTrack(a,c){var b;if(c===200)try{b=JSON.parse(a);if(b.ok){track=new Track(map,b);track.displayTrack();track.displayTrackinfo();track.displayProfile();window.location.hash="client="+encodeURIComponent(b.client)+"&region="+encodeURIComponent(tracklist.region)+"&fileid="+encodeURIComponent(b.fileid)+"&tmp="+b.tmp+"&detect_pause="+b.detect_pause+"&optimize_track="+b.optimize_track;divTrack(true);callDEMService(b.fileid)}divMenu(b.message)}catch(d){divMenu("Invalid data from server")}else divMenu("Server error "+
c)}function newTrackList(){track=null;loadTrackList();divMenu()}function loadTrackList(){GDownloadUrl("tracklist.php?client="+encodeURIComponent(client)+"&key="+encodeURIComponent(key),returnLoadTrackList)}function returnLoadTrackList(a,c){var b;if(c===200)try{divTracklist();b=JSON.parse(a);tracklist.display(b);setHash()}catch(d){}}function newTrackListAndOverviewMap(){track=null;loadTrackListAndOverviewMap();divMenu()}
function loadTrackListAndOverviewMap(){GDownloadUrl("tracklist.php?client="+encodeURIComponent(client)+"&key="+encodeURIComponent(key),returnLoadTrackListAndOverviewMap)}function returnLoadTrackListAndOverviewMap(a,c){var b;if(c===200)try{b=JSON.parse(a);divTracklist();tracklist.display(b);displayOverviewMap(b);setHash()}catch(d){}}function selectRegion(a){tracklist.select(decodeURIComponent(a));setHash()}
function setHash(){window.location.hash="client="+encodeURIComponent(client)+"&region="+encodeURIComponent(tracklist.region)}function processPhoto(a,c){divBusy();GDownloadUrl("processphoto.php?fileid="+encodeURIComponent(a)+"&client="+encodeURIComponent(client)+"&filename="+encodeURIComponent(c)+"&key="+encodeURIComponent(key),returnProcessPhoto)}
function returnProcessPhoto(a,c){var b;if(c===200){try{b=JSON.parse(a)}catch(d){divMenu("Invalid data from server");return}if(b.hasGPS){displayThumbnail(b.fileid);divMenu(TEXT.has_gps+": "+TEXT.lat+": "+b.lat.toFixed(5)+" "+TEXT.lon+": "+b.lon.toFixed(5));photo=new Photo(map,b,track.getTrackpoints(),track.getTracktimes());photo.showPhotoMarker()}else if(b.hasDate){displayThumbnail(b.fileid);a=new Date(b.date*1E3);document.getElementById("exposuretime").innerHTML=a.toLocaleString();divLocatePhoto();
photo=new Photo(map,b,track.getTrackpoints(),track.getTracktimes());photo.locate()}else divMenu(b.message)}else divMenu("Server error "+c)}function displayThumbnail(a){document.getElementById("divthumbnail").innerHTML='<img src="thumbnail.php?id='+encodeURIComponent(a)+'" id="thumbnail" name="thumbnail" alt="Thumbnail"/>'}
function submitDownloadTrack(a){var c=JSON.stringify(track.getTrackdata());document.forms.trackdownloadform.trackdata.innerHTML=c;document.forms.trackdownloadform.format.value=a;document.forms.trackdownloadform.submit()}
function callDEMService(a){var c=JSON.stringify(track.getTrackdata()),b=GXmlHttp.create();b.onreadystatechange=function(){var d,f;if(b.readyState===4)if(b.status===200){d=b.responseText;try{f=JSON.parse(d);if(f.ok&&f.demAvailable){track.addDEMData(f);document.getElementById("demdata").style.display="inline"}}catch(h){}}};b.open("POST","demservice.php?fileid="+encodeURIComponent(a));b.setRequestHeader("Content-Type","application/x-www-form-urlencoded");b.send("trackdata="+encodeURIComponent(c))}
function useDEMData(){track.useDEMData()}function displayOverviewMap(a){var c;map.clearOverlays();for(var b=0;b<a.length;++b){c=a[b];c.lat===0&&c.lon===0||map.addOverlay(createMarker(new GLatLng(c.lat,c.lon),c.linktext))}centerAndZoomMap(map,a)}
function centerAndZoomMap(a,c){for(var b=90,d=-90,f=180,h=-180,e,g,i=0;i<c.length;++i){e=c[i];if(e.getPoint)e=e.getPoint();if(typeof e.lat==="function"){g=e.lat();e=e.lng()}else{g=parseFloat(e.lat);e=parseFloat(e.lon)}if(g>d)d=g;if(g<b)b=g;if(e>h)h=e;if(e<f)f=e}if(d>b&&h>f){c=new GLatLngBounds(new GLatLng(b,f),new GLatLng(d,h));b=a.getBoundsZoomLevel(c);a.setCenter(c.getCenter(),b>16?16:b)}}
function createMarker(a,c){var b=new GMarker(a);c.length>0&&GEvent.addListener(b,"click",function(){b.openInfoWindowHtml('<span class="markerinfo"> '+c+"</span>",{maxWidth:300})});return b}function createCameraMarker(a,c){a=new GMarker(a,cameraicon);c.length>0&&GEvent.addListener(a,"click",function(){document.getElementById("divthumbnail").innerHTML='<img src="thumbnail.php?href='+c+'" id="thumbnail" name="thumbnail" alt="Thumbnail"/>'});return a}
function newPhoto(){photo&&photo.removeMarker();displayThumbnail("");divPhotoUpload()}function ProgressBar(){}ProgressBar.prototype.start=function(a){this.pb=a;this.x=21;this.running=true;a.style.width="1%";window.setTimeout("animateProgressBar()",1E3)};ProgressBar.prototype.stop=function(){this.running=false};ProgressBar.prototype.isRunning=function(){return this.running};
function animateProgressBar(){var a;if(progressbar.running){a=100-Math.round(2E3/progressbar.x);progressbar.pb.style.width=a+"%";progressbar.x++;window.setTimeout("animateProgressBar()",1E3)}};
