var TripView=Class.create(MapControlBase,{CONST:{track_color:'#008000',track_color_selected:'#00FF80',container:{routes:'routes',points:'points_in_route',tracks:'tracks_in_route',marks:'marks_in_route',trip_marks:'marks_in_trip'}},cv:function(s,i){return s.size()===0?$(i).hide():$(i).show();},evaluate:function($super,data){$super(Base64.decode(data.replace(/:/g,"\n")));this.cv(this.routes,'routes');this.cv(this.marks,'marks_in_trip');},initialize:function($super,map,cnf){$super(map,cnf);for(var key in this.CONST.container){$(this.CONST.container[key]).show();}
$('media').observe('click',this.media_click.bindAsEventListener(this));$('text').select('span.mceNonEditable').each(function(el){el.observe('click',this.text_click.bindAsEventListener(this));}.bind(this));$('download_for_device').observe('click',this.show_download_formats.bind(this,$('download_for_device')));Utils.register_targets($('marks_in_trip').up());},text_click:function(e){var el=e.element().hasClassName('mceNonEditable')?e.element():e.element().up('span.mceNonEditable');if(el){var re_type=new RegExp("text_(\\w+)");var re_id=new RegExp("id(\\d+)_?(\\d+)?");var r_type,r_id;if((r_type=el.className.match(re_type))!==null&&(r_id=el.className.match(re_id))!==null){switch(r_type[1]){case'media':this.show_media(Number(r_id[1]));break;case'route':this.select_route(Number(r_id[1]));break;case'trip_mark':this.select_trip_mark(Number(r_id[1]));break;default:this.select_route(Number(r_id[1]));this['select_'+r_type[1]](Number(r_id[2]));}
if(r_type[1]!=='media'){$('map').scrollTo();}}}
e.stop();},media_click:function(e){var el=e.element();var id;if((el.tagName==='IMG'||el.tagName==='SPAN')&&(id=Utils.extract_number(el.className))!==null){this.show_media(id);}
e.stop();},select_route:function($super,index){this.map.closeInfoWindow();$super(index);var route=this.routes[index];if(route.line){route._visible_event=GEvent.addListener(route.line,'lineupdated',function(route,index){GEvent.removeListener(route._visible_event);this.show_route_data(route,index);}.bind(this,route,index));}else{this.show_route_data(route,index);}
['points','marks','tracks'].each(function(route,s){this.cv(route[s],s+'_in_route');}.bind(this,route));},pan_to_route:function($super,route){if(!$super(route)){if(route.tracks.size()>0){this.pan_to_line(route.tracks[0].polyline);}else if(route.points.size()>0){this.map.panTo(route.points[0].position);}else if(route.marks.size()>0){this.map.panTo(route.marks[0].position);}else if(this.marks.size()>0){this.map.panTo(this.marks[0].position);}}},show_route_data:function(route,index){this.set_data(Builder.node('div',[new Element('strong').update(route.name),new Element('div',{className:'fancy'}).update(I18N.distance+': '+(route.line?route.line.getLength().toFixed():'0')+' '+I18N.meters),new Element('div').update(route.comment)]));},select_point:function($super,index){$super(index);var point=this.routes[this.selected_route].points[index];this.set_data(Builder.node('div',[new Element('strong').update(point.name),point.comment?new Element('br'):'',new Element('div').update(point.comment),(point.stay_type?Builder.node('div',[new Element('br'),new Element('span',{className:'quiet'}).update(I18N.stay+': '),new Element('span').update(this.cnf.stay_types[point.stay_type][2])]):''),((point.stop_time||point.start_time)?new Element('div',{className:'fancy'}).update((point.stop_time?Utils.human_date(point.stop_time):'?')+' - '+(point.start_time?Utils.human_date(point.start_time):'?')):''),Builder.node('div',[new Element('br'),new Element('span',{className:'quiet'}).update(I18N.coords+': '),new Element('span').update(point.position.lat().toFixed(3)+', '+point.position.lng().toFixed(3))])]),point.position);GEvent.addListener(this.map,'infowindowopen',function(e){this.in_place_media_rels('point',this.selected_point,this.selected_route,true);GEvent.clearListeners(this.map,'infowindowopen');}.bindAsEventListener(this));},select_track:function($super,index){$super(index);var track=this.routes[this.selected_route].tracks[index];this.set_data(Builder.node('div',[new Element('strong').update(track.name),track.comment?new Element('div').update(track.comment):'',new Element('div',{className:'fancy'}).update(I18N.Length+': '+track.polyline.getLength().toFixed()+' '+I18N.meters),new Element('a',{'href':'javascript:void(0)','id':'download_in_place','class':'has_event'}).update(I18N.download)]),track.polyline.getBounds().getCenter());GEvent.addListener(this.map,'infowindowopen',function(e){this.in_place_media_rels('track',this.selected_track,this.selected_route,true);$('download_in_place').observe('click',function(){this.show_download_formats($('download_in_place'),true);}.bind(this));GEvent.clearListeners(this.map,'infowindowopen');}.bindAsEventListener(this));},select_mark:function($super,index){$super(index);this.set_mark_data(this.routes[this.selected_route].marks[index],index,this.selected_route);},select_trip_mark:function($super,index){$super(index);this.set_mark_data(this.marks[index],index,-1);},set_mark_data:function(mark,index,_route){this.set_data(Builder.node('div',[new Element('strong').update(mark.name),mark.comment?new Element('div').update(mark.comment):'',new Element('div',{className:'fancy'}).update(I18N.coords+': '+mark.position.lat().toFixed(3)+', '+mark.position.lng().toFixed(3))]),mark.position);GEvent.addListener(this.map,'infowindowopen',function(e,index,_route){this.in_place_media_rels('mark',index,_route,true);GEvent.clearListeners(this.map,'infowindowopen');}.bindAsEventListener(this,index,_route));},in_place_media_rels:function(what,index,_route,in_window){var els=this.relate_media(what,index,_route).map(function(s){return new Element('span',{'className':'action id'+s[0]}).update(this.media[s[0]].c);}.bind(this));if(els.length>0){var el=Builder.node('div',{'className':'has_event'},els);if(in_window&&this.map.getInfoWindow()){var wnd=this.map.getInfoWindow().getContentContainers();if(wnd.length>0){wnd[0].appendChild(el);}}else{$('select_data').appendChild(el);}
el.observe('click',this.media_click.bindAsEventListener(this));}},set_data:function(el,ll){if(ll){this.map.openInfoWindow(ll,el);}else{$('select_data').select('.has_event').invoke('stopObserving');$('select_data').update().appendChild(el);}},show_download_formats:function(from,in_place){var format_id='select_format'+from.identify();from.insert({'after':Builder.node('span',[new Element('select',{'id':format_id}),new Element('input',{type:'button','id':format_id+'_do',value:I18N.download})])});if(!('formats' in this)){Utils.fetch('/gps/babel/formats/filtered',this.fill_format.bind(this,from,format_id,in_place));}else{this.fill_format(from,format_id,in_place,{formats:this.formats});}},fill_format:function(from,format_id,in_place,json){if('formats' in json){$H(json.formats).keys().sort(function(a,b){return this[a].name.localeCompare(this[b].name);}.bind(json.formats)).each(function(format_id,formats,key){var value=formats[key];if(!value.name.match(/output/i)){$(format_id).options[$(format_id).options.length]=new Option(value.name,key,false,false);}}.bind(this,format_id,json.formats));this.formats=json.formats;}
from.hide();if(in_place){Utils.reset_info_window.defer(this.map,this.map.getInfoWindow().getPoint(),330);}
$(format_id+'_do').observe('click',function(format_id,in_place){window.location.pathname+='/get/'+$(format_id).getValue()+(in_place?'?route='+this.selected_route+'&track='+this.selected_track:'');}.bind(this,format_id,in_place));},show_track_on_map:function($super,route,index){var track=$super(route,index);if(track.line.size()>1){if(!('has_map_events' in track)){GEvent.addListener(track.polyline,'mouseover',this.focus_track.bind(this,track));GEvent.addListener(track.polyline,'mouseout',this.blur_track.bind(this,track));GEvent.addListener(track.polyline,'click',this.select_track.bind(this,index));track.has_map_events=true;}
if(!('start_marker' in track)){track.stop_marker=new GMarker(track.polyline.getVertex(track.polyline.getVertexCount()-1),this.icon('stop'));track.start_marker=new GMarker(track.polyline.getVertex(0),this.icon('start'));GEvent.addListener(track.start_marker,'click',this.select_track.bind(this,index));GEvent.addListener(track.stop_marker,'click',this.select_track.bind(this,index));if(('move_type' in track)&&track.move_type!==''){track.move_type_marker=new GMarker(Utils.line_center(this.map,track.polyline),this.icon('move',track));}}
this.map.addOverlay(track.stop_marker);this.map.addOverlay(track.start_marker);if('move_type_marker' in track){this.map.addOverlay(track.move_type_marker);}}
return track;},clear_tracks:function($super){if(this.selected_route!==null){this.routes[this.selected_route].tracks.each(function(track){if(track.line.size()>1){this.map.removeOverlay(track.start_marker);this.map.removeOverlay(track.stop_marker);if('move_type_marker' in track){this.map.removeOverlay(track.move_type_marker);}}}.bind(this));}
$super();},focus_track:function(track){track.polyline.setStrokeStyle({'color':this.CONST.track_color_selected});},blur_track:function(track){track.polyline.setStrokeStyle({'color':this.CONST.track_color});},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'move':icon=MapIconMaker.createFlatIcon({width:30,height:30,shape:'circle'});icon.image='http://static.imesto.me/images/ico/move_type/'+('move_type' in options?this.cnf.move_types[options.move_type][1]:'unknown')+'.png';break;default:icon=MapIconMaker.createFlatIcon({width:32,height:32,primaryColor:"#00AAAA"});break;}
return{icon:icon,'draggable':false};},arrive_icon:function(point){var icon=MapIconMaker.createFlatIcon({width:30,height:30,shape:'circle'});icon.image='http://static.imesto.me/images/ico/move_type/'+(point.arrive_type?this.cnf.move_types[point.arrive_type][1]:'empty')+'.png';return{icon:icon};}});