var MapControlBase=Class.create({routes:[],marks:[],media:[],media_rels:[],selected_route:null,selected_point:null,selected_track:null,selected_mark:null,selected_trip_mark:null,_route_element:function(data){return{'points':data.points||[],'name':data.name,'comment':data.comment||'','tracks':data.tracks||[],'marks':data.marks||[]};},_point_element:function(data){return{'name':data.name,'position':data.position||this.map.getCenter(),'uid':data.uid||'p_'+Utils.rnd_key()};},_mark_element:function(data){return{'name':data.name,'position':data.position||this.map.getCenter(),'uid':data.uid||'m_'+Utils.rnd_key()};},_track_element:function(data){return{'name':data.name,'line':data.line||''};},initialize:function(map,cnf){this.load_state=new LoadState();this.map=map;this.cnf=cnf;GEvent.bind(this.map,'click',this,this.map_click);GEvent.bind(this.map,'infowindowbeforeclose',this,this.clear_events_in_info_window);},evaluate:function(data){this.load_state.start();if(data&&data.isJSON()){var json=data.evalJSON();if('map' in json&&'type' in json.map){var map_types={'h':G_HYBRID_MAP,'k':G_SATELLITE_MAP,'m':G_NORMAL_MAP,'p':G_PHYSICAL_MAP};if(json.map.type in map_types){this.map.setMapType(map_types[json.map.type]);}}
if('routes' in json){json.routes.each(function(route){route.points.each(function(point){point.position=new GLatLng(point.position[0],point.position[1]);});route.tracks.each(function(track){track.line=track.line.map(function(point){return new GLatLng(point[0],point[1]);});});if('marks' in route){route.marks.each(function(mark){mark.position=new GLatLng(mark.position[0],mark.position[1]);});}
this.add_new_route(route,true);}.bind(this));}
if('marks' in json){json.marks.each(function(mark){mark.position=new GLatLng(mark.position[0],mark.position[1]);this.add_new_trip_mark(mark,true);}.bind(this));}
if('media' in json){this.add_media(json.media,json.media_rels);}
if('routes' in json&&json.routes.size()){this.select_route(0);}}
this.load_state.stop();},add_new_route:function(route){this.routes.push(this._route_element(route));return this.add_route(this.routes.size()-1);},add_route:function(index){var el=$(this.CONST.container.routes).down('.route_list_element').cloneNode(true);$('routes_list').appendChild(el.show());this.routes[index].id=el.identify();this.register_route_click(index);this.update_route_attr(index,'name');return index;},update_route_attr:function(index,key){var route=this.routes[index];$(route.id).down('.route_'+key).update(route[key]);},register_route_click:function(index){$(this.routes[index].id).stopObserving('click').observe('click',function(index){this.select_route(index);}.bind(this,index));},select_route:function(index){this.load_state.start();Utils.select(this.routes[index].id,'route_list_element_selected');this.clear_points();this.clear_tracks();this.clear_marks();this.selected_route=index;this.show_points(index);this.show_tracks(index);this.show_marks(index);this.pan_to_route.bind(this).delay(1,this.routes[index]);this.load_state.stop();return true;},pan_to_route:function(route){return this.pan_to_line(route.line);},pan_to_line:function(line){if(line&&line.getVertexCount()>1){var bounds=line.getBounds();if(!this.map.getBounds().containsBounds(bounds)){this.map.setZoom(this.map.getBoundsZoomLevel(bounds));this.map.panTo(bounds.getCenter());return true;}}
return false;},clear_points:function(){Utils.stop_and_clear($('points_list'));this.selected_point=null;if(this.selected_route!==null){this.map.removeOverlay(this.routes[this.selected_route].line);this.routes[this.selected_route].points.each(function(item,index){this.map.removeOverlay(item.marker);if(index>0){this.map.removeOverlay(item.arrive_marker);}}.bind(this));}},show_points:function(index){var route=this.routes[index];route.line=new GPolyline([]);this.map.addOverlay(route.line);route.points.each(function(item,index){this.add_point(index);}.bind(this));this.check_vertex(route);},add_point:function(index){var el=$(this.CONST.container.points).down('.point_list_element').cloneNode(true);$('points_list').appendChild(el.show());this.routes[this.selected_route].points[index].id=el.identify();this.update_point_attr(index,'name');this.register_point_click(index);this.show_point_on_map(index);return index;},update_point_attr:function(index,key){var point=this.routes[this.selected_route].points[index];$(point.id).down('.point_'+key).update(point[key]);},register_map_point_events:function(route,index){var marker=route.points[index].marker;GEvent.clearListeners(marker);GEvent.addListener(marker,'click',this.select_point.bind(this,index));return marker;},register_point_click:function(index){$(this.routes[this.selected_route].points[index].id).stopObserving('click').observe('click',function(index){this.select_point(index);}.bind(this,index));},select_point:function(index){var point=this.routes[this.selected_route].points[index];Utils.select(point.id,'point_list_element_selected');this.update_point_marker(index,this.selected_point,true);this.selected_point=index;this.pan_to_latlng(point.position);return index;},pan_to_latlng:function(latlng){if(!this.map.getBounds().containsLatLng(latlng)){this.map.panTo(latlng);}},map_click:function(){return false;},show_point_on_map:function(index){var route=this.routes[this.selected_route];var point=route.points[index];this.create_point_marker(point,index);this.register_map_point_events(route,index);route.line.insertVertex(index,point.position);if(index>0){this.create_arrive_marker(route,point,index);}},check_vertex:function(route){if(route.points.size()>1){route.line.show();if(route.line.getVertexCount()>route.points.size()){$R(route.points.size(),route.line.getVertexCount(),true).each(function(index){this.deleteVertex(index);}.bind(route.line));}}else{route.line.hide();}},create_point_marker:function(point,index,with_star){point.marker=new GMarker(point.position,this.point_icon(index,with_star));this.map.addOverlay(point.marker);},update_point_marker:function(newbie,previous,selected){var route=this.routes[this.selected_route];var point;if(previous!==null&&previous<route.points.size()){point=route.points[previous];this.map.removeOverlay(point.marker);this.create_point_marker(point,previous);this.register_map_point_events(route,previous);}
if(newbie!==null){if(selected||newbie===0||newbie===(route.points.size()-1)){point=route.points[newbie];this.map.removeOverlay(point.marker);this.create_point_marker(point,newbie,selected);}
this.register_map_point_events(route,newbie);}},create_arrive_marker:function(route,point,index){point.arrive_marker=new GMarker(Utils.get_center(this.map,point.position,route.points[index-1].position),this.arrive_icon(point));this.map.addOverlay(point.arrive_marker);return point.arrive_marker;},point_icon:function(index,selected){var iconOptions={};var icon;if(index===0){icon=new GIcon(G_DEFAULT_ICON,'http://www.google.com/mapfiles/dd-start.png');}else if(index>0&&index===(this.routes[this.selected_route].points.size()-1)){icon=new GIcon(G_DEFAULT_ICON,'http://www.google.com/mapfiles/dd-end.png');}else{iconOptions.width=24;iconOptions.height=24;iconOptions.primaryColor="#0000FF";iconOptions.cornerColor="#FFFFFF";iconOptions.strokeColor="#000000";}
if(selected){iconOptions.width=32;iconOptions.height=32;iconOptions.primaryColor="#FFCC00";iconOptions.cornerColor="#FFFFFF";iconOptions.strokeColor="#000000";icon=null;}
return{icon:icon||MapIconMaker.createMarkerIcon(iconOptions),'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]:'unknown')+'.png';return{icon:icon};},clear_events_in_info_window:function(){this.map.getInfoWindow().getContentContainers().each(function(el){el.select('.has_event').invoke('stopObserving');});},show_tracks:function(index){this.routes[index].tracks.each(function(track,index){this.add_track(index);}.bind(this));},clear_tracks:function(){Utils.stop_and_clear($('tracks_list'));this.selected_track=null;if(this.selected_route!==null){this.routes[this.selected_route].tracks.each(function(track){if('polyline' in track){this.map.removeOverlay(track.polyline);}},this);}},add_track:function(index,id){var el=$(this.CONST.container.tracks).down('.track_list_element').cloneNode(true);$('tracks_list').appendChild(el.show());var route=this.routes[this.selected_route];route.tracks[index].id=el.identify();this.update_track_attr(index,'name');this.register_track_click(index);this.show_track_on_map(route,index);return index;},update_track_attr:function(index,key){var track=this.routes[this.selected_route].tracks[index];$(track.id).down('.track_'+key).update(track[key]);},show_track_on_map:function(route,index){var track=route.tracks[index];track.polyline=new GPolyline(track.line,this.CONST.track_color);this.map.addOverlay(track.polyline);return track;},register_track_click:function(index){$(this.routes[this.selected_route].tracks[index].id).stopObserving('click').observe('click',function(index){this.select_track(index);}.bind(this,index));},select_track:function(index){Utils.select(this.routes[this.selected_route].tracks[index].id,'track_list_element_selected');this.routes[this.selected_route].tracks[index].polyline.setStrokeStyle({'color':this.CONST.track_color_selected});if(this.selected_track!==null){this.routes[this.selected_route].tracks[this.selected_track].polyline.setStrokeStyle({'color':this.CONST.track_color});}
this.selected_track=index;this.pan_to_line(this.routes[this.selected_route].tracks[index].polyline);return index;},show_marks:function(index){this.routes[index].marks.each(function(mark,index){this.add_mark(index);}.bind(this));},clear_marks:function(){Utils.stop_and_clear($('marks_list'));this.selected_mark=null;if(this.selected_route!==null){this.routes[this.selected_route].marks.each(function(mark){if('marker' in mark){this.map.removeOverlay(mark.marker);}},this);}},add_mark:function(index,id){var el=$(this.CONST.container.marks).down('.mark_list_element').cloneNode(true);$('marks_list').appendChild(el.show());var route=this.routes[this.selected_route];route.marks[index].id=el.identify();this.update_mark_attr(index,'name');this.register_mark_click(index);this.show_mark_on_map(route,index);return index;},update_mark_attr:function(index,key){var mark=this.routes[this.selected_route].marks[index];$(mark.id).down('.mark_'+key).update(mark[key]);},show_mark_on_map:function(route,index){var mark=route.marks[index];this.create_mark_marker(mark,index);this.register_map_mark_events(route,index);},register_map_mark_events:function(route,index){var marker=route.marks[index].marker;GEvent.clearListeners(marker);GEvent.addListener(marker,'click',this.select_mark.bind(this,index));return marker;},create_mark_marker:function(mark,index,with_star){mark.marker=new GMarker(mark.position,this.mark_icon(index,with_star));this.map.addOverlay(mark.marker);},register_mark_click:function(index){$(this.routes[this.selected_route].marks[index].id).stopObserving('click').observe('click',function(index){this.select_mark(index);}.bind(this,index));},select_mark:function(index){var mark=this.routes[this.selected_route].marks[index];Utils.select(mark.id,'mark_list_element_selected');this.update_mark_marker(index,this.selected_mark,true);this.selected_mark=index;this.bounce(mark.marker);this.pan_to_latlng(mark.position);return index;},update_mark_marker:function(newbie,previous,selected){var route=this.routes[this.selected_route];var mark;if(previous!==null&&previous<route.marks.size()){mark=route.marks[previous];this.map.removeOverlay(mark.marker);this.create_mark_marker(mark,previous);this.register_map_mark_events(route,previous);}
if(newbie!==null){if(selected||newbie===0||newbie===(route.marks.size()-1)){mark=route.marks[newbie];this.map.removeOverlay(mark.marker);this.create_mark_marker(mark,newbie,selected);}
this.register_map_mark_events(route,newbie);}},mark_icon:function(index,selected){return this.common_mark_icon(new GIcon(G_DEFAULT_ICON),this.routes[this.selected_route].marks[index]);},common_mark_icon:function(icon,mark){if('ico' in mark){var ico=mark.ico;icon.image=ico.ii;icon.shadow=ico.si;icon.iconSize=GSize(ico.iw||32,ico.ih||32);icon.shadowSize=GSize(ico.sw||32,ico.sh||32);}
return{icon:icon,'draggable':false};},add_new_trip_mark:function(mark){this.marks.push(mark);return this.add_trip_mark(this.marks.size()-1);},add_trip_mark:function(index){var el=$(this.CONST.container.trip_marks).down('.trip_mark_list_element').cloneNode(true);$('trip_marks_list').appendChild(el.show());this.marks[index].id=el.identify();this.register_trip_mark_click(index);this.update_trip_mark_attr(index,'name');this.show_trip_mark_on_map(index);return index;},update_trip_mark_attr:function(index,key){var mark=this.marks[index];$(mark.id).down('.trip_mark_'+key).update(mark[key]);},register_trip_mark_click:function(index){$(this.marks[index].id).stopObserving('click').observe('click',function(index){this.select_trip_mark(index);}.bind(this,index));},show_trip_mark_on_map:function(index){var mark=this.marks[index];this.create_trip_mark_marker(mark,index);this.register_map_trip_mark_events(index);},register_map_trip_mark_events:function(index){var marker=this.marks[index].marker;GEvent.clearListeners(marker);GEvent.addListener(marker,'click',this.select_trip_mark.bind(this,index));return marker;},create_trip_mark_marker:function(mark,index,with_star){mark.marker=new GMarker(mark.position,this.trip_mark_icon(index,with_star));this.map.addOverlay(mark.marker);},select_trip_mark:function(index){var mark=this.marks[index];Utils.select(mark.id,'trip_mark_list_element_selected');this.update_trip_mark_marker(index,this.selected_trip_mark,true);this.selected_trip_mark=index;this.bounce(mark.marker);this.pan_to_latlng(mark.position);return true;},trip_mark_icon:function(index,selected){return this.common_mark_icon(new GIcon(G_DEFAULT_ICON,'http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png'),this.marks[index]);},update_trip_mark_marker:function(newbie,previous,selected){var mark;if(previous!==null&&previous<this.marks.size()){mark=this.marks[previous];this.map.removeOverlay(mark.marker);this.create_trip_mark_marker(mark,previous);this.register_map_trip_mark_events(previous);}
if(newbie!==null){if(selected||newbie===0||newbie===(this.marks.size()-1)){mark=this.marks[newbie];this.map.removeOverlay(mark.marker);this.create_trip_mark_marker(mark,newbie,selected);}
this.register_map_trip_mark_events(newbie);}},bounce:function(marker){this._blinks=10;return new PeriodicalExecuter(this.blink.bind(this,marker),0.05);},blink:function(marker,pe){this._blinks--;if(this._blinks%2===1){marker.show();}else{marker.hide();}
if(this._blinks<0){pe.stop();marker.show();}},add_media:function(entries,_rels){var initial_length=this.media.length;this.media=this.media.concat(entries);this.media_rels=_rels||[];return initial_length;},show_media:function(index,_media){return new MediaShow(_media||this.media,index);},relate_media:function(what,index,_route){if(what!=='key'&&what!=='route'){return this.media_rels.select(function(what,index,_route,s){return s[1]===_route&&s[2]===what&&s[3]===index;}.curry(what,index,_route));}else{var key=what=='key'?0:1;return this.media_rels.select(function(key,index,s){return s[key]===index;}.curry(key,index));}}});var MediaShow=Class.create({initialize:function(data,index){this.dialog=new Dialog({title:I18N.image_view,content:$H(data.inject({},function(size,acc,item,index){acc[(index+1)+'/'+size]=new Element('img',{'src':item.i,'alt':item.c||'','className':'media_view_item pointer'}).observe('click',this.next_step.bind(this));return acc;}.bind(this,data.length))),afterClose:function(){$$('img.media_view_item').invoke('stopObserving');this.dialog=null;}});this.dialog.open();if(index!==null){this.dialog.switch_step(index);this.dialog.setDimensions.bind(this.dialog).delay(1);}},next_step:function(){this.dialog.next_step();}});