function init_map(cnf){var map=new GMap2($('public_map'),{googleBarOptions:{style:"new"}});map.setUIToDefault();map.enableGoogleBar();init_cluster();if(google.loader.ClientLocation){map.setCenter(new GLatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude),8);}else{map.setCenter(40,70,3);}
var public_map=new PublicMap(map,cnf);var image_preloader=new ImagePreloader();image_preloader.cache_images(new Hash(cnf.move_types).values().map(function(s){return'/images/ico/move_type/'+s[1]+'.png'}));Utils.inline_items($('latest_media'));}
var PublicMap=Class.create({path_to_move_images:'http://static.'+domain+'/images/ico/move_type/',trips:{},routes:{},tracks:{},marks:{},colors:{track:'#008000',route:'#0000FF',line_over:'#5A1EA8'},current_bounds:null,zoom_dir:0,initialize:function(map,cnf){this.cnf=cnf;this.map=map;this.cluster=new Clusterer(map);Utils.add_open_street(this.map);this.zoom_level==this.map.getZoom();GEvent.addListener(map,"moveend",function(){(function(){if(this.zoom_level==this.map.getZoom()){this.refresh();this.zoom_dir=0;}}.bind(this)).delay(this.zoom_dir>=0?0.2:0.5);}.bind(this));GEvent.addListener(map,"zoomend",function(old_level,new_level){this.zoom_level=new_level;this.zoom_dir=new_level-old_level;}.bind(this));if(!map.isLoaded()){GEvent.addListener(map,"load",this.refresh.bind(this));}else{this.refresh.bind(this).defer();}
GEvent.addListener(this.map,'infowindowbeforeclose',function(){this.map.getInfoWindow().getContentContainers().each(function(el){el.select('.has_event').invoke('stopObserving');});delete this.selected_line;}.bind(this));$('public_map').insert({'after':Builder.node('div',{'id':'show_restrictions','align':'right'},[new Element('span',{'class':'quiet'}).update(I18N.show+': '),new Element('input',{'id':'show_routes','class':'routes','type':'checkbox','checked':'checked'}),new Element('span').update(I18N.routes),new Element('input',{'id':'show_tracks','class':'tracks','type':'checkbox','checked':'checked'}),new Element('span').update(I18N.tracks),new Element('input',{'id':'show_marks','class':'marks','type':'checkbox'}),new Element('span').update(I18N.marks),new Element('span',{'class':'quiet'}).update(' &middot;&middot;&middot; '),new Element('input',{'id':'show_panoramio','type':'checkbox'}),new Element('span').update('Panoramio'),new Element('input',{'id':'show_wikipedia','type':'checkbox'}),new Element('span').update('Wikipedia')])});$('show_restrictions').observe('click',function(e){var el=e.element();if(el.tagName=='INPUT'){if(el.className){this.process_cached(el.className);}else{switch(el.id){case'show_wikipedia':if(!('wiki' in this)){this.wiki=new GLayer('org.wikipedia.'+language);this.map.addOverlay(this.wiki);}
this.wiki[el.checked?'show':'hide']();break;case'show_panoramio':if(!('panoramio' in this)){this.panoramio=new GLayer('com.panoramio.all');this.map.addOverlay(this.panoramio);}
this.panoramio[el.checked?'show':'hide']();break;}}}
return true;}.bindAsEventListener(this));},refresh:function(){var bounds=this.map.getBounds();if(!this.current_bounds||!this.current_bounds.containsBounds(bounds)){var sw=bounds.getSouthWest();var ne=bounds.getNorthEast();Utils.fetch('/api/bounds/'+sw.lat()+'/'+sw.lng()+'/'+ne.lat()+'/'+ne.lng(),this.process.bind(this,bounds));}else{this.process_cached();}},process_cached:function(type){switch(type){case'routes':var display=$('show_routes').checked;for(var id in this.routes){this.line_on_map(this.routes[id],'route',display);}
break;case'tracks':var display=$('show_tracks').checked;for(var id in this.tracks){this.line_on_map(this.tracks[id],'track',display);}
break;case'marks':var display=$('show_marks').checked;for(var id in this.marks){this.add_mark(display,this.marks[id]);}
break;default:var show_routes=$('show_routes').checked;var show_tracks=$('show_tracks').checked;for(var id in this.routes){this.line_on_map(this.routes[id],'route',show_routes);}
for(var id in this.tracks){this.line_on_map(this.tracks[id],'track',show_tracks);}}},process:function(bounds,json){if('ok' in json){json.routes.each(this.add_line.bind(this,$('show_routes').checked,'route',this.routes));json.tracks.each(this.add_line.bind(this,$('show_tracks').checked,'track',this.tracks));json.marks.each(this.add_mark.bind(this,$('show_marks').checked));}
if(!('oversized' in json)){this.current_bounds=bounds;}},add_line:function(is_visible,type,container,item){if(!(item.id in container)){container[item.id]=item;item.bounds=new GLatLngBounds(new GLatLng(item.sw_lat,item.sw_lng),new GLatLng(item.ne_lat,item.ne_lng));}
if(is_visible){this.line_on_map(container[item.id],type,true);}},line_on_map:function(item,type,is_visible){if(!is_visible){if(('center_marker_on_map' in item)){item.center_marker.hide();}
if('line_on_map' in item){item.polyline.hide();}}else{var zoom_diff=this.map.getBoundsZoomLevel(item.bounds)-this.map.getZoom();if(zoom_diff<=1){if(!('d' in item)){item.d=item.data.evalJSON();}
if(('center_marker_on_map' in item)){this.cluster.RemoveMarker(item.center_marker);delete item.center_marker_on_map;}
if(!('polyline' in item)){switch(type){case'route':item.polyline=new GPolyline(item.d.points.map(function(point){return new GLatLng(point.position[0],point.position[1])}),this.colors[type]);break;case'track':item.polyline=new GPolyline(item.d.line.map(function(point){return new GLatLng(point[0],point[1])}),this.colors[type]);break;default:break;}}
if(!('line_on_map' in item)&&this.map.getBounds().intersects(item.bounds)){this.map.addOverlay(item.polyline);GEvent.addListener(item.polyline,'mouseover',this.line_focus.bind(this,type,item));GEvent.addListener(item.polyline,'mouseout',this.line_blur.bind(this,type,item));GEvent.addListener(item.polyline,'click',this.select_line.bind(this,type,item));item.line_on_map=true;}
item.polyline.show();}else{if('line_on_map' in item){this.map.removeOverlay(item.polyline);delete item.line_on_map;}
if(!('center_marker' in item)){item.center_marker=new GMarker(item.bounds.getCenter(),this.icon(type));GEvent.addListener(item.center_marker,'click',this.select_line.bind(this,type,item,item.center_marker.getLatLng()));}
if(!('center_marker_on_map' in item)&&this.map.getBounds().containsLatLng(item.center_marker.getLatLng())){this.cluster.AddMarker(item.center_marker);item.center_marker_on_map=true;}
item.center_marker.show();}}},line_focus:function(type,item){if('block_over_behavior' in item){return;}
if(!('start_marker' in item)){item.stop_marker=new GMarker(item.polyline.getVertex(item.polyline.getVertexCount()-1),this.icon('stop'));item.start_marker=new GMarker(item.polyline.getVertex(0),this.icon('start'));GEvent.addListener(item.start_marker,'click',this.select_line.bind(this,type,item));GEvent.addListener(item.stop_marker,'click',this.select_line.bind(this,type,item));}
this.map.addOverlay(item.stop_marker);this.map.addOverlay(item.start_marker);switch(type){case'track':if(!('move_type_marker' in item)){item.move_type_marker=this.create_marker(Utils.line_center(this.map,item.polyline),'move',item.d);}else{this.map.addOverlay(item.move_type_marker);}
break;case'route':var points=item.d.points;var polyline=item.polyline;if(!('has_arrive_markers' in item)){for(var i=1,len=points.length;i<len;i++){var point=points[i];point.arrive_marker=this.create_marker(Utils.get_center(this.map,polyline.getVertex(i),polyline.getVertex(i-1)),'arrive',point);}
item.has_arrive_markers=true;}else{for(var i=1,len=points.length;i<len;i++){this.map.addOverlay(points[i].arrive_marker);}}
break;}
this.line_stroke(item.polyline,this.colors.line_over,1);item.has_focus=true;},line_blur:function(type,item){if('block_over_behavior' in item){return;}
this.map.removeOverlay(item.start_marker);this.map.removeOverlay(item.stop_marker);switch(type){case'track':this.map.removeOverlay(item.move_type_marker);break;case'route':var points=item.d.points;for(var i=1,len=points.length;i<len;i++){this.map.removeOverlay(points[i].arrive_marker);}
break;}
this.line_stroke(item.polyline,this.colors[type],0.5);delete(item.has_focus);},line_stroke:function(line,color,opacity){line.setStrokeStyle({'color':color,'opacity':opacity});},select_line:function(type,item,latlng){if('selected_line' in this&&this.selected_line==(type+'_'+item.id)&&!this.map.getInfoWindow().isHidden()){Utils.reset_info_window(this.map,latlng);}else{this.map.closeInfoWindow();var node=$('line_info').cloneNode(true);this.map.openInfoWindow(latlng,node.show());this.selected_line=type+'_'+item.id;node.down('.type').down().update(I18N[type]).next().update(item.name);if('polyline' in item){node.down('.length').down().update(type=='route'?I18N.distance:I18N.Length).next().update(Math.round(item.polyline.getLength())+' '+I18N.meters);}
node.down('.move_type').down().appendChild(this.collect_line_move_types(type,item));if(!('line_on_map' in item)){node.down('.zoom').appendChild(new Element('a',{'class':'has_event','href':'javascript:void(0)'}).update(' '+I18N.zoom));node.down('.zoom').down().observe('click',function(item,node,type){var current_bounds=this.map.getBounds();this.pan_to_bounds(item.bounds);if(!('polyline' in item)){item._listener=GEvent.addListener(this.map,'addoverlay',function(item,type,overlay){if('polyline' in item){GEvent.removeListener(item._listener);this.focus_line_by_select(type,item);$(Utils.reset_info_window(this.map,item.polyline.getVertex(0)).getContentContainers()[0]).down('.length').down().update(type=='route'?I18N.distance:I18N.Length).next().update(Math.round(item.polyline.getLength())+' '+I18N.meters);}}.bind(this,item,type));}else{this.focus_line_by_select(type,item);Utils.reset_info_window(this.map,item.polyline.getVertex(0));}
node.stopObserving();node.update(' '+I18N.zoom_back).observe('click',function(current_bounds){this.pan_to_bounds(current_bounds);this.map.closeInfoWindow();}.bind(this,current_bounds));}.bind(this,item,node.down('.zoom').down(),type));}else{this.focus_line_by_select(type,item);}
this.fill_trip_info(node,item);}},focus_line_by_select:function(type,item,already){if(!('has_focus' in item)){this.line_focus(type,item);}
item.block_over_behavior=true;item._window_event=GEvent.addListener(this.map,'infowindowbeforeclose',function(type,item){delete item.block_over_behavior;this.line_blur(type,item);GEvent.removeListener(item._window_event);}.bind(this,type,item));},collect_line_move_types:function(type,item){if(!('d' in item)){item.d=item.data.evalJSON();}
switch(type){case'route':return Builder.node('span',[$H(item.d.points.inject({},function(acc,s){if('arrive_type' in s){acc[s.arrive_type]=1;}
return acc;})).keys().map(function(s){return new Element('img',{'src':'http://static.'+domain+'/images/ico/move_type/'+this.cnf.move_types[s][1]+'.png'})}.bind(this))]);break;case'track':if('move_type' in item.d){return Builder.node('span',[new Element('img',{'src':'http://static.'+domain+'/images/ico/move_type/'+this.cnf.move_types[item.d.move_type][1]+'.png'})]);}
break;}
return new Element('span');},pan_to_bounds:function(bounds){this.map.setZoom(this.map.getBoundsZoomLevel(bounds));this.map.panTo(bounds.getCenter());},fill_trip_info:function(node,item){node.appendChild($('trip_info').cloneNode(true).show());if(!(item.trip in this.trips)){Utils.fetch('/api/trip/'+item.trip,function(node,json){if('trip' in json){this.trips[json.trip.id]=json.trip;this.update_trip_info(node,json.trip);}}.bind(this,node.down('.trip')));}else{this.update_trip_info(node,this.trips[item.trip]);}},update_trip_info:function(node,trip){node.down('.trip_name').update(trip.name).writeAttribute('href',trip.url).previous().update(I18N.trip);node.down('.trip_author').down().update(I18N.author).next().update(trip.author).writeAttribute('href',trip.site_url);},create_marker:function(latlng,type,options){var marker=new GMarker(latlng,this.icon(type,options));this.map.addOverlay(marker);return marker;},icon:function(type,options){var icon;switch(type){case'start':icon=new GIcon(G_DEFAULT_ICON,'http://www.google.com/mapfiles/dd-start.png');break;case'stop':icon=new GIcon(G_DEFAULT_ICON,'http://www.google.com/mapfiles/dd-end.png');break;case'route':icon=MapIconMaker.createFlatIcon({width:16,height:16,primaryColor:"#0000FF"});break;case'track':icon=MapIconMaker.createFlatIcon({width:16,height:16,primaryColor:"#00EE00"});break;case'mark':if(options){icon=new GIcon(G_DEFAULT_ICON);icon.image=options.ii;icon.shadow=options.si;icon.iconSize=GSize(options.iw||32,options.ih||32);icon.shadowSize=GSize(options.sw||32,options.sh||32);}else{icon=MapIconMaker.createFlatIcon({width:16,height:16,primaryColor:"#FFFF00"});}
break;case'arrive':icon=MapIconMaker.createFlatIcon({width:30,height:30,shape:'circle'});icon.image=this.path_to_move_images+('arrive_type' in options?this.cnf.move_types[options.arrive_type][1]:'empty')+'.png';break;case'move':icon=MapIconMaker.createFlatIcon({width:30,height:30,shape:'circle'});icon.image=this.path_to_move_images+('move_type' in options?this.cnf.move_types[options.move_type][1]:'empty')+'.png';break;default:icon=MapIconMaker.createFlatIcon({width:32,height:32,primaryColor:"#00AAAA"});break;}
return{icon:icon,'draggable':false};},add_mark:function(is_visible,item){if(!(item.id in this.marks)){this.marks[item.id]=item;item.position=new GLatLng(item.latitude,item.longitude);}
this.mark_on_map(item,(is_visible&&this.map.getBounds().containsLatLng(item.position)));},mark_on_map:function(item,is_visible){if(!is_visible){if(('marker_on_map' in item)){item.marker.hide();}}else{if(!('d' in item)){item.d=item.data.evalJSON();}
if(!('marker' in item)){item.marker=new GMarker(item.position,this.icon('mark',item.d.ico));GEvent.addListener(item.marker,'click',this.select_mark.bind(this,item));}
if(!('marker_on_map' in item)&&this.map.getBounds().containsLatLng(item.position)){this.cluster.AddMarker(item.marker);item.marker_on_map=true;}
item.marker.show();}},select_mark:function(item,latlng){this.map.closeInfoWindow();var node=$('mark_info').cloneNode(true);this.map.openInfoWindow(latlng,node.show());node.down('.name').down().update(I18N.mark).next().update(item.name);node.down('.position').down().update(I18N.coords).next().update(item.latitude.toFixed(3)+', '+item.longitude.toFixed(3));this.fill_trip_info(node,item);}});