﻿var SurgestedZoomLevel=14;var vMapMode="add";var AllMarkers=[];var sLineDrawMode="cycle";var UserClickPoint="";var routeNodes=[];var wCount=0;var GDir1;var GDir2;var normalProj;var waypoints=[];var gpolys=[];var routeNodes=[];var myNode;var markerDragged;var isDragged;var lastIndex;var prevIndex;var strDefaultMarkerText="0 km";var geoCoder=new GClientGeocoder();var y=0;var arWaypoints=[];var TotalWaypointsToLoadOntoMap=0;var BulkAddMakerCount=0;var SaveFormIsHidden=false;function controls_LineMode(a){switch(a){case'line':sLineDrawMode="line";$('#controls_LineMode_Cycle_Inner').removeClass('selected');$('#controls_LineMode_Line_Inner').addClass('selected');break;case'cycle':sLineDrawMode="cycle";$('#controls_LineMode_Cycle_Inner').addClass('selected');$('#controls_LineMode_Line_Inner').removeClass('selected');break}}function addPoint(a){UserClickPoint=a;if(a){if(waypoints.length==0){fnAddWayPoint("")}else{if(sLineDrawMode=="cycle"){GDir1.loadFromWaypoints([waypoints[waypoints.length-1].getPoint(),a],{getPolyline:true,travelMode:G_TRAVEL_MODE_DRIVING})}if(sLineDrawMode=="line"){fnAddWayPoint(a)}}}}var timeout;var clickCoords;function UserClickedMap(){clearTimeout(timeout);timeout=null;point=clickCoords;if(SaveFormIsHidden){popUpBox('Start creating a new route? <br /> <a href="#" class="NewRouteYes">Yes</a> or <a href="#" class="NewRouteNo">No</a>?');$(".NewRouteYes").bind("click",function(){ShowSaveForm(true);popUpBoxClose()});$(".NewRouteNo").bind("click",function(){popUpBoxClose()})}else{var a=point.lng();var b=point.lat();if(a>0.55||a<-0.9||b>51.77||b<51.11){popUpBox("Sorry the Your Route tool is only available in the London area.")}else{zoom=map.getZoom();if(zoom<SurgestedZoomLevel){popUpBox('We recommend you zoom in to plot your route. <br /> <a href="#" class="zoomInNow">Zoom in</a> or <a href="#" class="AppPointNow">Add point</a>?');$(".zoomInNow").bind("click",function(){map.setCenter(point,15);popUpBoxClose()});$(".AppPointNow").bind("click",function(){addPoint(point);popUpBoxClose()})}else{addPoint(point)}}}}function StartMap(){if(!GBrowserIsCompatible()){alert("Sorry, the Google Maps API is not compatible with this browser")}else{StartMapSub();map.setMapType(G_NORMAL_MAP);var c=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(75,7));c.apply(document.getElementById("controls_undo"));map.getContainer().appendChild(document.getElementById("controls_undo"));var d=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(155,7));d.apply(document.getElementById("controls_LineMode_Cycle"));map.getContainer().appendChild(document.getElementById("controls_LineMode_Cycle"));var e=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(285,7));e.apply(document.getElementById("controls_LineMode_Line"));map.getContainer().appendChild(document.getElementById("controls_LineMode_Line"));var f=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(75,50));f.apply(document.getElementById("MapWorkingMsg"));map.getContainer().appendChild(document.getElementById("MapWorkingMsg"));controls_LineMode('cycle');AutoRunPostCodeSearch();loadOff();normalProj=G_NORMAL_MAP.getProjection();GDir1=new GDirections();GDir2=new GDirections();GEvent.addListener(map,'mousemove',getProximity);GEvent.addListener(map,"zoomend",function(){routeNodes=[]});GEvent.addListener(map,"click",function(a,b){if(a!=null){return}if(timeout==null){clickCoords=b;timeout=setTimeout('UserClickedMap()',500)}else{clearTimeout(timeout);timeout=null}})}function fnActiveDragNode(){iconNode=new GIcon();iconNode.image='/common/images/icons/spinner_small.gif';iconNode.shadow='';iconNode.iconSize=new GSize(10,10);iconNode.shadowSize=new GSize(0,0);iconNode.iconAnchor=new GPoint(5,5);iconNode.infoWindowAnchor=new GPoint(5,5);iconNode.maxHeight=1;myNode=new GMarker(map.getCenter(),{icon:iconNode,draggable:true,bouncy:false,zIndexProcess:function(a,b){return 1}});map.addOverlay(myNode);myNode.show();myNode.hide()}fnActiveDragNode();function fnUpdateRouteOnDrag(){myNode.show();if(isDragged==2){markerDragged=myNode;return}if(myNode.MyIndex<waypoints.length){isDragged=2;markerDragged=false;lastIndex=myNode.MyIndex;prevIndex=-1;GDir2.loadFromWaypoints([waypoints[lastIndex].getPoint(),myNode.getPoint().toUrlValue(6),waypoints[lastIndex+1].getPoint()],{getPolyline:true})}}GEvent.addListener(GDir1,"load",function(){fnAddWayPoint("")})}function clearDown(){map.clearOverlays();gpolys=[];routeNodes=[];waypoints=[];UpdateLHSDistanceIcon(strDefaultMarkerText);document.getElementById("controls_undo").style.color="#cfcfcf"}function fnRemoveWaypoint(){document.getElementById("controls_undo").style.fontWeight="bold";pCount=gpolys.length-1;wCount=waypoints.length-1;if(wCount==0){clearDown()}if(wCount>0){map.removeOverlay(waypoints[wCount]);map.removeOverlay(gpolys[pCount]);wCount--;waypoints.pop();gpolys.pop();routeNodes.pop();if(pCount>0){map.addOverlay(waypoints[wCount]);sDistanceText=waypoints[wCount].DistanceText}else{sDistanceText=strDefaultMarkerText}map.panTo(waypoints[wCount].getPoint());UpdateLHSDistanceIcon(sDistanceText)}document.getElementById("controls_undo").style.fontWeight="normal"}function getProximity(a,b){var c,zoom;if(routeNodes.length==0){document.getElementById("controls_undo").style.color="#cfcfcf";c=0;zoom=map.getZoom();if(gpolys.length>0){if(gpolys.length>0&&gpolys[0].getVertexCount()>0){}document.getElementById("controls_undo").style.color="#000000";debugBox("gpolys[0]:"+gpolys[0]);routeNodes.push(normalProj.fromLatLngToPixel(gpolys[0].getVertex(0),zoom));for(var i=0;i<gpolys.length;i++){c+=gpolys[i].getLength();for(var j=1;j<gpolys[i].getVertexCount();j++){var e=normalProj.fromLatLngToPixel(gpolys[i].getVertex(j),zoom);e.MyIndex=i;routeNodes.push(e)}}}if(routeNodes.length>0){document.getElementById("controls_undo").style.color="#000000";sDistanceText=(c/1000).toFixed(1)+" km";UpdateLHSDistanceIcon(sDistanceText)}}if(!a||routeNodes.length<=1||isDragged>0)return;zoom=map.getZoom();var f=normalProj.fromLatLngToPixel(a,zoom);var g=999;var h=f.x;var k=f.y;if(routeNodes.length>1){var x,y,d1,d2,d;var l=f.x-routeNodes[0].x;var m=f.y-routeNodes[0].y;d2=l*l+m*m;for(var n=0;++n<routeNodes.length;){d1=d2;x=routeNodes[n].x;l=f.x-x;y=routeNodes[n].y;m=f.y-y;d2=l*l+m*m;l=x-routeNodes[n-1].x;m=y-routeNodes[n-1].y;d=l*l+m*m;var u=((f.x-x)*l+(f.y-y)*m)/d;x+=(u*l);y+=(u*m);l=f.x-x;m=f.y-y;c=l*l+m*m;if((d1-c)+(d2-c)>d){if(d1<d2){c=d1;x=routeNodes[n-1].x;y=routeNodes[n-1].y}else{c=d2;x=routeNodes[n].x;y=routeNodes[n].y}};if(g>c){g=c;h=x;k=y;myNode.MyIndex=routeNodes[n].MyIndex}}if(g>25){myNode.hide()}else{for(n=waypoints.length;--n>=0;){var o=normalProj.fromLatLngToPixel(waypoints[n].getPoint(),zoom);l=o.x-h;m=o.y-k;if(l*l+m*m<25){myNode.hide();return}}myNode.setPoint(normalProj.fromPixelToLatLng(new GPoint(h,k),zoom));myNode.show()}}}function DistanceMarkerURL(a){return"http://chart.apis.google.com/chart?chst=d_bubble_icon_text_small&chld=bicycle|bb|"+a+"|000000|20AF34&ext=.png"}function UpdateLHSDistanceIcon(a){document.formAddRoute.TotalDistance.value=a}function SaveButtonDisabled(a){if(a){$("#SaveButton").attr("disabled","disabled")}else{$("#SaveButton").removeAttr("disabled")}}function TidyUp(a){a=a.replace(/</,"|");a=a.replace(/>/,"|");return a}function fnSaveRoute(){if(waypoints.length>1){popUpBox("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Saving your route...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");SaveButtonDisabled(true);BuildSaveString=fnBuildSave();SaveRoute(BuildSaveString)}else{popUpBox("No route created.")}return false}function fnBuildSave(){var a=map.getCenter().lng();var b=map.getCenter().lat();var c='';var d='';var e='';if(gpolys.length>0){dist=0;var f="";var g=[];ThisPolyCount="";var h=new Array(0);TotalgPolys=gpolys.length;for(var i=0;i<gpolys.length;i++){ThisPolyCount=gpolys[i].getVertexCount();for(var j=0;j<ThisPolyCount;j++){g.push(gpolys[i].getVertex(j));h.push(gpolys[i].getVertex(j));c+=gpolys[i].getVertex(j).lat()+","+gpolys[i].getVertex(j).lng();if(i<ThisPolyCount-1){c+="a"}}c+="b"}var k=waypoints.length;EndMarker="";StartMarker="";for(var i=0;i<k;i++){if(i==0){StartMarkerLat=waypoints[i].getPoint().lat();StartMarkerLng=waypoints[i].getPoint().lng()}d+=waypoints[i].getPoint().lat()+","+waypoints[i].getPoint().lng()+"lm"+waypoints[i].LineDrawMode;if(i<k-1){d+="a"}if(i==k-1){EndMarkerLat=waypoints[i].getPoint().lat();EndMarkerLng=waypoints[i].getPoint().lng()}}verySmall=1*0.00001;Encode_zoomFactor=1*2;Encode_numLevels=1*18;var l=new PolylineEncoder(Encode_numLevels,Encode_zoomFactor,verySmall,true);var m=l.dpEncode(h);Encode_points=m.encodedPoints;debugBox("Encode_points:"+Encode_points);Encode_levels=m.encodedLevels;RouteTitle=document.formAddRoute.RouteTitle.value;RouteDescription=TidyUp(document.formAddRoute.RouteDescription.value);Nickname=document.formAddRoute.Nickname.value;editkey=document.formAddRoute.editkey.value;EmailAddress=document.formAddRoute.EmailAddress.value;routeid=document.formAddRoute.routeid.value;Distance=document.formAddRoute.TotalDistance.value;Distance=Distance.replace(/ km/,"");ReturnXMLStack="action=add&";ReturnXMLStack+="editkey="+editkey+"&";ReturnXMLStack+="routeid="+routeid+"&";ReturnXMLStack+="RouteTitle="+RouteTitle+"&";ReturnXMLStack+="RouteDescription="+RouteDescription+"&";ReturnXMLStack+="Nickname="+Nickname+"&";ReturnXMLStack+="EmailAddress="+EmailAddress+"&";ReturnXMLStack+="Encode_levels="+Encode_levels+"&";ReturnXMLStack+="Encode_points="+Encode_points+"&";ReturnXMLStack+="Encode_zoomFactor="+Encode_zoomFactor+"&";ReturnXMLStack+="Encode_numLevels="+Encode_numLevels+"&";ReturnXMLStack+="LongPolyline="+c+"&";ReturnXMLStack+="Waypoints="+d+"&";ReturnXMLStack+="Distance="+Distance+"&";ReturnXMLStack+="DistanceUnits=metric&";ReturnXMLStack+="StartMarkerLat="+StartMarkerLat+"&";ReturnXMLStack+="StartMarkerLng="+StartMarkerLng+"&";ReturnXMLStack+="EndMarkerLat="+EndMarkerLat+"&";ReturnXMLStack+="EndMarkerLng="+EndMarkerLng;return TidyUp(ReturnXMLStack)}}function SaveRoute(e){var v=GXmlHttp.create();v.open("POST","/dotnet/SaveRoute.aspx",true);v.setRequestHeader('Content-Type','application/x-www-form-urlencoded');v.onreadystatechange=function(){if(v.readyState==4){FullPageData=v.responseText;var a=GXml.parse(FullPageData);var b=a.documentElement.getElementsByTagName('routeid')[0].firstChild.nodeValue;var c=a.documentElement.getElementsByTagName('userid')[0].firstChild.nodeValue;var d=a.documentElement.getElementsByTagName('editkey')[0].firstChild.nodeValue;SavedMsg="<h2>Route saved</h2>";SavedMsg+="Your cycle route has been saved and can now be seen by other cyclists.<br /><br />";SavedMsg+="<a href=\"http://"+WhichServer+"/?rid="+b+"\">View your route on the map</a><br />";SavedMsg+="<a href=\"javascript:ReEditSavedRouteStraightAway('"+b+"','"+d+"');\">Edit your route now</a><br /><br />";SavedMsg+="If you would like to make changes to your cycle route in the future, you can <a class=\"favButton\" title=\"Your routes &#124; Transport for London &#124; Route:"+b+"\" href=\"http://"+WhichServer+"/?mode=add&action=edit&rid="+b+"&key="+d+"\">bookmark your route</a> and return to it later.<br /><br />";document.formAddRoute.routeid.value=b;document.formAddRoute.editkey.value=d;$('#cycleLeftCol_AddARouteHolderSaved').empty;$('#cycleLeftCol_AddARouteHolderSaved').append(SavedMsg);$('.favButton').jFav();ShowSaveForm(false);clearDown();ClearForm();SaveButtonDisabled(false);popUpBoxClose()}};v.send(e)};function ClearForm(){document.formAddRoute.RouteTitle.value="";document.formAddRoute.Nickname.value="";document.formAddRoute.EmailAddress.value="";document.formAddRoute.RouteDescription.value="";document.formAddRoute.editkey.value="";document.formAddRoute.routeid.value=""}function ShowSaveForm(a){if(a){$('#cycleLeftCol_AddARouteHolderSaved').fadeOut(500);$('#cycleLeftCol_AddARouteHolderSaved').empty();$('#cycleLeftCol_AddARouteHolder').fadeIn(500);SaveFormIsHidden=false}else{$('#cycleLeftCol_AddARouteHolder').fadeOut(500);$('#cycleLeftCol_AddARouteHolderSaved').fadeIn(500);SaveFormIsHidden=true}}function ReEditSavedRouteStraightAway(a,b){ShowSaveForm(true);LoadSavedRoute(a,b)}function fnAddWayPoint(c){if(c!=""){UserClickPoint=c}wCount=waypoints.length;gCount=gpolys.length;map.panTo(UserClickPoint);if(waypoints.length==0){map.clearOverlays();waypoints[wCount]=new GMarker(UserClickPoint,{icon:genDynMarker("d_bubble_icon_text_small","bicycle","Start","000000","20AF34"),draggable:false,dragCrossMove:false,bouncy:false,zIndexProcess:function(a,b){return 1}})}else{if(sLineDrawMode=="cycle"){GPolyCode=GDir1.getStatus().code;var d=GDir1.getPolyline();UserClickPoint=d.getVertex(d.getVertexCount()-1)}if(sLineDrawMode=="line"){d=new GPolyline([waypoints[wCount-1].getPoint(),UserClickPoint])}map.addOverlay(d);gpolys.push(d);gCount=gpolys.length;routeNodes=[];getProximity();waypoints[wCount]=new GMarker(UserClickPoint,{icon:genDynMarker("d_bubble_icon_text_small","bicycle",sDistanceText,"000000","20AF34"),draggable:false,dragCrossMove:false,bouncy:true,zIndexProcess:function(a,b){return 1}});waypoints[wCount].DistanceText=sDistanceText;waypoints[wCount].LineDrawMode=sLineDrawMode;if(wCount>1){map.removeOverlay(waypoints[wCount-1])}}waypoints[wCount].MyIndex=wCount;map.addOverlay(waypoints[wCount]);if(TotalWaypointsToLoadOntoMap>0){BulkAddMakerCount++;renderSavedLine()}}function renderSavedLine(){if(BulkAddMakerCount<TotalWaypointsToLoadOntoMap){loadOn();arUserWaypoint=arWaypoints[BulkAddMakerCount].split("lm");sLineDrawMode=arUserWaypoint[1];if(sLineDrawMode=="undefined"){sLineDrawMode="cycle"}controls_LineMode(sLineDrawMode);arrWaypointLatLng=arUserWaypoint[0].split(",");point=new GLatLng(arrWaypointLatLng[0],arrWaypointLatLng[1]);addPoint(point);map.setCenter(point,15);loadOff()}}function LoadSavedRoute(k,l){loadOn();gpolys=[];routeNodes=[];waypoints=[];var m="/dotnet/GetRoute_FullDetail.aspx?routeId="+k+"&editkey="+l;GDownloadUrl(m,function(a,b){if(b==200){var c=GXml.parse(a);var d=c.documentElement.getElementsByTagName('RouteId')[0].firstChild.nodeValue;if(d=="Error"){popUpBox("<b>Error:</b> invalid details to retrieve route id:"+k)}else{var e=c.documentElement.getElementsByTagName('WayPoints')[0].firstChild.nodeValue;var f=c.documentElement.getElementsByTagName('RouteTitle')[0].firstChild.nodeValue;var g=c.documentElement.getElementsByTagName('Nickname')[0].firstChild.nodeValue;var h=c.documentElement.getElementsByTagName('RouteDescription')[0].firstChild.nodeValue;var i=c.documentElement.getElementsByTagName('EmailAddress')[0].firstChild.nodeValue;document.formAddRoute.RouteTitle.value=f;document.formAddRoute.Nickname.value=g;document.formAddRoute.EmailAddress.value=i;document.formAddRoute.RouteDescription.value=h;document.formAddRoute.editkey.value=l;document.formAddRoute.routeid.value=k;arWaypoints=e.split("a");TotalWaypointsToLoadOntoMap=arWaypoints.length;var j="";renderSavedLine()}}});loadOff()}