google.load("maps","2",{other_params:"sensor=false"});google.setOnLoadCallback(Setup);var mapElement=document.getElementById('map');var map;var stationIcon,transferIcon;var redTrainIcons=[];var greenTrainIcons=[];var blueTrainIcons=[];var yellowTrainIcons=[];var orangeTrainIcons=[];var allIcons=[];allIcons['red']=redTrainIcons;allIcons['green']=greenTrainIcons;allIcons['blue']=blueTrainIcons;allIcons['yellow']=yellowTrainIcons;allIcons['orange']=orangeTrainIcons;var stops,routeStops,segmentSecs;var countElement=document.getElementById('count');function Setup() {acme.Initialize();acme.maps.Initialize();if(!google.maps.BrowserIsCompatible()) {mapElement.innerHTML='Sorry, your browser is not compatible with Google Maps.';return;} map=new google.maps.Map2(mapElement,{draggableCursor:'default',backgroundColor:'#99cc99'});map.addControl(new google.maps.LargeMapControl());map.addControl(new google.maps.ScaleControl());map.setCenter(new google.maps.LatLng(37.80,-122.20),10);google.maps.Event.addListener(map,'infowindowclose',PopDown);var adUnit=new google.maps.AdsManager(map,"pub-5759188606642566",{channel:'0949180036',style:'adunit',maxAdsOnMap:1,position:new google.maps.ControlPosition(google.maps.ANCHOR_TOP_RIGHT,new google.maps.Size(7,7))});adUnit.enable();stationIcon=new google.maps.Icon();stationIcon.image='images/station.png';stationIcon.iconSize=new google.maps.Size(11,11);stationIcon.iconAnchor=new google.maps.Point(5,5);stationIcon.infoWindowAnchor=new google.maps.Point(3,7);transferIcon=new google.maps.Icon();transferIcon.image='images/transfer.png';transferIcon.iconSize=new google.maps.Size(15,15);transferIcon.iconAnchor=new google.maps.Point(7,7);transferIcon.infoWindowAnchor=new google.maps.Point(6,7);var size=new google.maps.Size(20,20);var anchor=new google.maps.Point(10,10);for(a=0;a<=360;a+=5) {redTrainIcons[a]=new google.maps.Icon();redTrainIcons[a].image='images/red_train_'+ZeroPad(a)+'.png';redTrainIcons[a].iconSize=size;redTrainIcons[a].iconAnchor=anchor;greenTrainIcons[a]=new google.maps.Icon();greenTrainIcons[a].image='images/green_train_'+ZeroPad(a)+'.png';greenTrainIcons[a].iconSize=size;greenTrainIcons[a].iconAnchor=anchor;blueTrainIcons[a]=new google.maps.Icon();blueTrainIcons[a].image='images/blue_train_'+ZeroPad(a)+'.png';blueTrainIcons[a].iconSize=size;blueTrainIcons[a].iconAnchor=anchor;yellowTrainIcons[a]=new google.maps.Icon();yellowTrainIcons[a].image='images/yellow_train_'+ZeroPad(a)+'.png';yellowTrainIcons[a].iconSize=size;yellowTrainIcons[a].iconAnchor=anchor;orangeTrainIcons[a]=new google.maps.Icon();orangeTrainIcons[a].image='images/orange_train_'+ZeroPad(a)+'.png';orangeTrainIcons[a].iconSize=size;orangeTrainIcons[a].iconAnchor=anchor;} GtfsGetFile('stops.txt',StopsFetched);} function Unload() {if(minuteTimeout!=null) {clearTimeout(minuteTimeout);minuteTimeout=null;} if(secondTimeout!=null) {clearTimeout(secondTimeout);secondTimeout=null;} google.maps.Unload();} function StopsFetched(_stops) {stops=_stops;stops=Associatize(stops,'stop_id');for(var i in stops) {var stop=stops[i];stop.stop_lat=+stop.stop_lat;stop.stop_lon=+stop.stop_lon;stop.latlng=new google.maps.LatLng(stop.stop_lat,stop.stop_lon);var xfer=(i=='BALB'||i=='BAYF'||i=='MCAR'||i=='12TH');stop.marker=new google.maps.Marker(stop.latlng,{icon:xfer?transferIcon:stationIcon,title:stop.stop_name,zIndexProcess:StopZIndex});stop.marker.name=stop.stop_name;map.addOverlay(stop.marker);google.maps.Event.addListener(stop.marker,'click',MakeCaller(PopUp,stop.stop_id));} GtfsGetFile('route_stops.txt',RouteStopsFetched);} function StopZIndex(a,b) {return 20;} function TrainZIndex(a,b) {return 10;} function RouteStopsFetched(_routeStops) {routeStops=_routeStops;routeStops=Associatize(routeStops,'route_id');for(var i in routeStops) {routeStop=routeStops[i];var stop_ids=Trim(routeStop.stop_ids).split(' ');routeStop.route=[];for(var j in stop_ids) routeStop.route.push({stop_id:stop_ids[j]});var latlngs=[];for(var j in routeStop.route) latlngs.push(stops[routeStop.route[j].stop_id].latlng);map.addOverlay(new google.maps.Polyline(latlngs,'#000000',8,1.0));} GtfsGetFile('segment_secs.txt',SegmentSecsFetched);} function SegmentSecsFetched(_segmentSecs) {segmentSecs=[];for(var i in _segmentSecs) {var segmentSec=_segmentSecs[i];segmentSec.secs=+segmentSec.secs;segmentSec.direction=acme.maps.Bearing(stops[segmentSec.from_stop_id].latlng,stops[segmentSec.to_stop_id].latlng);if(segmentSecs[segmentSec.from_stop_id]==null) segmentSecs[segmentSec.from_stop_id]=[];segmentSecs[segmentSec.from_stop_id][segmentSec.to_stop_id]=segmentSec;} for(var i in routeStops) {var routeStop=routeStops[i];routeStop.route[0].secs=0;for(var j=1;j=1000) {var remainderSecs=Math.floor(remainderMsecs/1000);remainderMsecs-=remainderSecs*1000;MoveTrains(remainderSecs);ShowTrains();}} prevMsecs=currMsecs;secondTimeout=setTimeout(EverySecond,900);} function FetchEtas() {HttpGet('get_etas.cgi',EtasRequestChecker);} function EtasRequestChecker(request) {for(var i in stops) stops[i].etas=[];var xmlDoc=request.responseXML.documentElement;var stationElements=xmlDoc.getElementsByTagName('station');for(var i=0;i=routeStop.secsSpots.length) secs=routeStop.secsSpots[routeStop.secsSpots.length-1]-secs;var secsSpot=routeStop.secsSpots[secs];train.latlng=secsSpot.latlng;train.direction=secsSpot.direction;} function ShowTrains() {for(var i in trains) {var train=trains[i];var icons=allIcons[train.color];var icon=icons[RoundFive(train.direction)];if(train.marker==null) {train.marker=new google.maps.Marker(train.latlng,{icon:icon,clickable:false,zIndexProcess:TrainZIndex});map.addOverlay(train.marker);} else {if(train.marker.icon==icon) train.marker.setLatLng(train.latlng) else {map.removeOverlay(train.marker);train.marker=new google.maps.Marker(train.latlng,{icon:icon,clickable:false,zIndexProcess:TrainZIndex});map.addOverlay(train.marker);}}}} var cookieName='bartStation';var lastStation=GetCookie(cookieName);function PopUp(stop_id) {var stop=stops[stop_id];var html;var name=stop.stop_name;html=''+name+'' html+='

';if(stop.etas.length==0) html+='No ETAs listed.
';else for(var i in stop.etas) {html+=stop.etas[i].destination+': ';for(j in stop.etas[i].times) {if(j!=0) html+=', ';var secs=stop.etas[i].times[j];if(secs==0) html+='Arrived';else html+=secs/60+' min';} html+='
';} map.closeInfoWindow();stop.marker.openInfoWindowHtml(html,{maxWidth:300});lastStation=stop_id;SaveCookie(cookieName,lastStation);} function RePop() {if(lastStation) PopUp(lastStation);} function PopDown() {lastStation=null;ClearCookie(cookieName);} function ZeroPad(n) {if(n<=9) return'00'+n;else if(n<=99) return'0'+n;else return n;} function RoundFive(a) {return Math.round(a/5)*5;} var gtfsCurrentCallback;function GtfsGetFile(url,callback) {gtfsCurrentCallback=callback;HttpGet(url,GtfsParseFile);} function GtfsParseFile(request) {var text=request.responseText;var lines=text.split('\n');var fieldNames;var table=[];for(var i in lines) {var line=lines[i];if(line.length>0) {var fields=line.split(',');for(var j in fields) fields[j]=GtfsDeQuote(fields[j]);if(i==0) fieldNames=fields;else {var row={};for(var j in fields) row[fieldNames[j]]=fields[j];table.push(row);}}} gtfsCurrentCallback(table);} function GtfsDeQuote(field) {return field;}