MapToolkit.RouteEditor=function(h){function r(a){var b=g.i18n&&g.i18n[a]?g.i18n[a]:MapToolkit.i18n["editor_"+a];return b?b.replace(/\s/g,"&nbsp;"):a}var j=this,c=h.gmap,g=h.config.editor||{},f=null,d=[],e={data:{},div:null},l=function(){if(e.div==null){e.div=document.createElement("div");c.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(e.div);$ADD_CLASS(e.div,"mtk-tooltip");e.div.style.visibility="hidden";e.div.style.width="100%";e.div.style.position="relative";e.div.style.zIndex=7E5;$OPACITY(e.div,
0.8)}},s=function(a){if(a==null){for(var b=a=0;b<d.length;b++)a+=d[b].distance;a=parseInt(a)/1E3}g&&g.updateDistance&&g.updateDistance(a);GEvent.trigger(j,"updateDistance",a)},k=function(a){if(!f||f.type!=a||f.points.length>f.maxLength()){a=new MapToolkit.RoutePart(j,a);if(f){a.addVertex(f.points[f.points.length-1]);n(f,a)}d.push(a);f=a;GEvent.addListener(a,"polylineUpdated",s)}return f},n=function(a,b){GEvent.clearListeners(a,"lastVertexChanged");GEvent.clearListeners(b,"firstVertexChanged");GEvent.addListener(b,
"firstVertexChanged",function(){if(!a.removed){a.updateVertex(a.points.length-1,b.points[0]);a.draw()}});GEvent.addListener(a,"lastVertexChanged",function(){if(!b.removed){b.updateVertex(0,a.points[a.part1.length-1]);b.draw()}})},q=function(){if(d.length>1&&d[0].points.length<=1){d[0].remove();d.splice(0,1)}for(var a=0;a<d.length;a++){var b=false,m=false,i=false;if(a==0)m=true;if(a==d.length-1)i=true;if(d[a].isStart!=m)b=true;if(d[a].isEnd!=i)b=true;d[a].isStart=m;d[a].isEnd=i;b&&d[a].draw()}};j.map=
h;j.updateTooltip=function(a){if(!(g.tooltip!=null&&!g.tooltip)){$EXTEND(e.data,a);var b;if(e.data.onRouteMarker)b=r("editVertex");else if(e.data.onRoute)b=r("editRoute");else if(e.data.tool=="vertices"||e.data.tool=="waypoints")b=this.isEmpty()?r("beginRoute"):r("continueRoute");l();e.div.innerHTML=b}};j.showTooltip=function(){if(!(g.tooltip!=null&&!g.tooltip)){l();e.div.style.visibility="visible"}};j.hideTooltip=function(){if(!(g.tooltip!=null&&!g.tooltip)){l();e.div.style.visibility="hidden"}};
j.moveTooltip=function(a){l();a=c.fromLatLngToDivPixel(a);e.div.style.left=a.x+10+"px";e.div.style.top=a.y-10+"px"};j.addParts=function(a,b){function m(i){if(a[i]){if(a[i].points.length<2)i==0&&a.length>1?a[i].points.push(a[i+1].points[0]):a[i].points.unshift(a[i-1].points[a[i-1].points.length-1]);var o=k(a[i].type);$EACH(a[i].points,function(p){o.addVertex($P(p))});o.draw();window.setTimeout(function(){m(i+1)},100)}else{q();b&&b()}}m(0)};j.addVertex=function(a){var b=k("vertices");b.addVertex(a);
q();b.draw();GEvent.trigger(j,"addVertex",a,b)};j.addWaypoint=function(a){var b=k("waypoints");b.addVertex(a);q();b.draw();GEvent.trigger(j,"addWaypoint",a,b)};j.updateVertex=function(a,b,m){a.updateVertex(b,m);q();a.draw()};j.deleteVertex=function(a,b){a.deleteVertex(b);if(a.points.length==1){a.remove();d.length==1&&s(0);for(b=0;b<d.length;b++)if(d[b]==a){if(b&&b<d.length-1){n(d[b-1],d[b+1]);GEvent.trigger(d[b-1],"lastVertexChanged")}d.splice(b,1);f=d[d.length-1];q();return}}q();a.draw()};j.concatStartEnd=
function(){if(f){var a=d[0].points[0];this.addVertex($P(a.lat(),a.lng()))}};j.deleteLastPoint=function(){f&&this.deleteVertex(f,f.points.length-1)};j.getStartPoint=function(){try{return d[0].points[0]}catch(a){return null}};j.isEmpty=function(){return f?false:true};j.getFormValues=function(){for(var a=0,b=[],m=[],i=0;i<d.length;i++){a+=d[i].distance;var o=$MAP(d[i].getVertices(),function(p){return[p.lat(),p.lng()]});i&&o.shift();b=b.concat(o);o={type:d[i].type,points:$MAP(d[i].points,function(p){return[p.lat(),
p.lng()]})};i&&o.points.shift();m.push(o)}return{distance:a,points:$TO_JSON(b),parts:$TO_JSON(m)}}};
MapToolkit.RoutePart=function(h,r){function j(a){return f.editor.icon&&f.editor.icon[a]?MapToolkit.Icon.get(f.editor.icon[a]):G_DEFAULT_ICON}var c=this,g=h.map.gmap,f=h.map.config,d=null,e=[],l=f.editor.routingClass?(new MapToolkit.classForSting(f.editor.routingClass))(f.editor):new GDirections;GEvent.addListener(l,"load",function(a){for(var b=0;b<a.getNumGeocodes();b++)if(!a.getMarker(b).getLatLng().equals(c.points[b])){c.points[b]=a.getMarker(b).getLatLng();if(e[b])e[b].setLatLng(c.points[b]);else if(b==
0)GEvent.trigger(c,"firstVertexChanged");else b==e.length-1&&GEvent.trigger(c,"lastVertexChanged")}n(a.getPolyline())});var s=function(a){for(var b=0;b<c.points.length;b++)if(e[b]==a)return b},k=function(a,b){if(!e[a]||e[a].getIcon().image!=b.image){if(e[a]){GEvent.clearInstanceListeners(e[a]);g.removeOverlay(e[a])}var m=new GMarker(c.points[a],{icon:b,draggable:true,bouncy:false});GEvent.addListener(m,"mouseover",function(){h.updateTooltip({onRouteMarker:true})});GEvent.addListener(m,"mouseout",
function(){h.updateTooltip({onRouteMarker:false})});GEvent.addListener(m,"click",function(){var i=s(m);h.deleteVertex(c,i);h.updateTooltip({onRouteMarker:false,onRoute:false});if(i==0)GEvent.trigger(c,"firstVertexChanged");else i==e.length&&GEvent.trigger(c,"lastVertexChanged")});GEvent.addListener(m,"dragend",function(){var i=s(m);h.updateVertex(c,i,m.getPoint());if(i==0)GEvent.trigger(c,"firstVertexChanged");else i==e.length-1&&GEvent.trigger(c,"lastVertexChanged")});g.addOverlay(m);e[a]=m}},n=
function(a){d=a;g.addOverlay(a);GEvent.addListener(a,"mouseover",function(){h.updateTooltip({onRoute:true})});GEvent.addListener(a,"mouseout",function(){h.updateTooltip({onRoute:false})});GEvent.addListener(a,"click",function(b){for(var m=null,i=99999999,o=1;o<c.points.length;o++){var p=c.points[o-1],t=c.points[o],u=p.distanceFrom(t);p=p.distanceFrom(b);t=t.distanceFrom(b);if(p+t-u<i){m=o;i=p+t-u}}if(m!=null){c.addVertex(b,m);c.draw()}});c.distance=a.getLength();GEvent.trigger(c,"polylineUpdated")},
q=function(){if(d){g.removeOverlay(d);GEvent.clearInstanceListeners(d);d=null}};c.points=[];c.type=r;c.isStart=false;c.isEnd=false;c.removed=false;c.distance=0;c.maxLength=function(){return c.type=="waypoints"?5:20};c.addVertex=function(a,b){if(b){c.points.splice(b,0,a);e.splice(b,0,null)}else c.points.push(a)};c.updateVertex=function(a,b){c.points[a]=b;e[a]&&e[a].setLatLng(b)};c.deleteVertex=function(a){GEvent.clearInstanceListeners(e[a]);g.removeOverlay(e[a]);e.splice(a,1);c.points.splice(a,1)};
c.draw=function(){c.drawMarkers();q();if(!(c.points.length<2))if(c.type=="vertices")n(new GPolyline(c.points));else c.type=="waypoints"?l.loadFromWaypoints(c.points,{getPolyline:true,travelMode:f.editor.travelMode||G_TRAVEL_MODE_DRIVING,avoidHighways:f.avoidHighways!=null?f.avoidHighways:true}):alert("unknown RoutePart")};c.drawMarkers=function(){if(c.points.length){var a=c.points.length;c.isStart&&k(0,j("startVertex"));if(c.isEnd&&!(c.isStart&&a<2)){k(a-1,j("endVertex"));a--}for(var b=1;b<a;b++)k(b,
j("vertex"))}};c.remove=function(){GEvent.clearInstanceListeners(c);q();for(var a=0;a<e.length;a++)if(e[a]){GEvent.clearInstanceListeners(e[a]);g.removeOverlay(e[a])}c.removed=true};c.getVertices=function(){var a=[];if(d)for(var b=0;b<d.getVertexCount();b++)a.push(d.getVertex(b));return a}};MapToolkit.RouteEditor.Config={routingClass:null,tools:null,tooltip:null,travelMode:null,unit:null,avoidHighways:null,icon:{start:null,vertex:null,end:null},i18n:null,updateDistance:null};MapToolkit.RouteEditorControl=function(h){function r(k){return config.i18n&&config.i18n[k]?config.i18n[k]:MapToolkit.i18n["editor_"+k]}function j(k){return config.icon&&config.icon[k]?config.icon[k]:MapToolkit.host+"/static/images/editor/"+k+".png"}var c,g,f,d=[],e=this;e.initialize=function(k){config=h.config&&h.config.editor||{};var n=document.createElement("div");n.id="mtk-route-editor-control";g=document.createElement("img");f=document.createElement("img");g.src=j("verticesTool");g.title=r("verticesControl");
f.src=j("waypointsTool");f.title=r("waypointsControl");GEvent.addDomListener(g,"click",function(){e.verticesActive()});GEvent.addDomListener(f,"click",function(){e.waypointsActive()});var q={vertices:g,waypoints:f},a=config.tools||["waypoints","vertices"];$EACH(a,function(b){n.appendChild(q[b])});k.getContainer().appendChild(n);c=h.initEditor();if(config.showTooltip||config.showTooltip==null){GEvent.addListener(k,"mouseover",c.showTooltip);GEvent.addListener(k,"mousemove",function(b){c.moveTooltip(b)});
GEvent.addListener(k,"mouseout",c.hideTooltip)}a[0]=="vertices"?e.verticesActive():e.waypointsActive();return n};e.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(80,37))};var l=function(k){setTimeout(function(){$EL(h.div).firstChild.firstChild.style.cursor=k},10)},s=function(){for(var k=0;k<d.length;k++)GEvent.removeListener(d[k])};e.verticesActive=function(){c.updateTooltip({tool:"vertices"});g.src=j("verticesToolSelected");f.src=j("waypointsTool");s();h.gmap.disableDoubleClickZoom();
l("crosshair");d.push(GEvent.addListener(h.gmap,"click",function(k,n){c.updateTooltip({});k==null&&c.addVertex(n);l("crosshair")}));d.push(GEvent.addListener(h.gmap,"dragend",function(){l("crosshair")}))};e.waypointsActive=function(){c.updateTooltip({tool:"waypoints"});g.src=j("verticesTool");f.src=j("waypointsToolSelected");s();h.gmap.disableDoubleClickZoom();l("crosshair");d.push(GEvent.addListener(h.gmap,"click",function(k,n){c.updateTooltip({});k==null&&c.addWaypoint(n);l("crosshair")}));d.push(GEvent.addListener(h.gmap,
"dragend",function(){l("crosshair")}))}};MapToolkit.RouteEditorControl.prototype=new GControl;MapToolkit.UploadControl=function(h){function r(e){return d.i18n&&d.i18n[e]?d.i18n[e]:MapToolkit.i18n["upload_"+e]}function j(e){return d.icon&&d.icon[e]?d.icon[e]:MapToolkit.host+"/static/images/upload/"+e+".png"}var c=this,g,f,d=h.config.upload||{};h.upload=c;c.initialize=function(e){var l=document.createElement("div");f=document.createElement("img");f.src=j("tool");f.title=r("uploadControl");GEvent.addDomListener(f,"click",function(){g?c.close():c.open()});l.appendChild(f);e.getContainer().appendChild(l);
return l};c.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(148,37))};c.close=function(){f.src=j("tool");$EL(h.div).removeChild(g);h.editor&&h.editor.showTooltip();g=null};c.open=function(){f.src=j("toolSelected");var e="";for(var l in d.style){var s="";for(var k in d.style[l]){var n=k;n=n.replace(/([a-z])([A-Z])/g,function(q,a,b){return a+"-"+b.toLowerCase()});s+=n+":"+d.style[l][k]+";"}e+="#"+l+" {"+s+"}\n"}l=d.href||MapToolkit.host+"/upload";g=document.createElement("div");
g.onmouseover=function(){h.editor&&h.editor.hideTooltip()};g.onmouseout=function(){h.editor&&h.editor.showTooltip()};g.id="mtk-upload-container";g.style.width=(d.width||300)+"px";g.style.height=(d.height||200)+"px";g.innerHTML='<iframe name="mtk-upload" src="'+l+"?href="+encodeURIComponent(l)+"&div="+h.div+"&style="+encodeURIComponent(e)+"&height="+(d.height||200)+"&width="+(d.width||300)+"&i18n="+encodeURIComponent($TO_JSON(d.i18n||{}))+'" height="'+(d.height||200)+'" frameborder="0" width="'+(d.width||
300)+'" scrolling="no"></iframe><div class="mtk-infowindow-close'+(MapToolkit.isIE6()?"-ie6":"")+'" onclick="javascript:MapToolkit.getMap(\''+h.div+"').upload.close()\">&nbsp;</div>";$EL(h.div).appendChild(g)}};MapToolkit.UploadControl.prototype=new GControl;
