// Shows a bus route with the stops // Parameters: b - busno (eg. 301 or 2 L), s - start stopno (eg. 2 or 0 for start), // d - dest stopno (e.g. 25 or 0 for last stop) /* global $, document, gup */ var dBugLvl = 3; var stopBusLines = ''; var called_times = 0; var STOPNO = 0; var SEARCHSTR = 1; var NXTDIST = 2; var NXTTIME = 3; var LAT = 4; var LON = 5; var MAXROUTEDIST = 50; var MAXSTOPDIST = 2; var MAXRECENTJOURNEYS = 7; var geoPos; var gStartStop = {}; var gDestStop = {}; var gStartStopNo; var gStartSearchStr; var gDestStopNo; var gDestSearchStr; var total_dist = 0; var total_time = 0; var firstStop = 0; var lastStop = 0; var arBusStops = []; var colStart = ''; var colEnd = '
'; var dispSize; var wdth; var busNo, firstStop, lastStop; var addJourney = true; var trackJourney = true; // var bDir = 'u'; var sDir = 'u'; $(document).ready(function () { var dispSize, wdth, s, d, startStop, destStop; dispSize = getWinDim(); wdth = dispSize.wd * 0.98; if (dispSize.wd > 240) { // Screen too small to display columns colStart = ''; colEnd = ''; } busNo = gup('b'); // get URL params s = gup('s'); d = gup('d'); sDir = gup('dir'); if (s === "" && d === "") { busNo = getCookie('cBusNo'); s = getCookie('cStartStopNo'); d = getCookie('cDestStopNo'); } if(busNo === "") { alert('No Journey selected yet. Select one from the Find Bus tab or the Routes tab'); window.location.href = "index.html"; return; } startStop = parseInt(s, 10); destStop = parseInt(d, 10); if(startStop > 0 && destStop > 0) { if(startStop > destStop) sDir = 'd'; else sDir = 'u'; } if(startStop === 0 && destStop === 0) trackJourney = false; // User is just browsing a route // dBug('', 'b=' + busNo + ' s = ' + s + ', d = ' + d, 5); // showBBCookies(); startStop = startStop > 0 ? startStop : 1; if (startStop === "" && destStop === "") { document.write('Cannot find a valid start or destination'); return false; } gStartStop.stopno = startStop; gStartStop.searchstr = ''; gDestStop.stopno = destStop; gDestStop.searchstr = ''; firstStop = startStop > 0 ? startStop : 0; lastStop = destStop > 0 ? destStop : 0; dBug('ready: ', 'busno= ' + busNo + ', s ' + startStop + ', d=' + destStop, 5); document.getElementById("routeTitle").innerHTML = "Bus no.: " + busNo; // readRouteStopFile(busNo, startStop, destStop); arBusStops = getb(); dBug('arBusStops: ', arBusStops.length, 5); // Stop no.[Area] Stop name Next Stop
Dist. (Time)'; var ht = showRouteStops(arBusStops, 0, startStop, destStop); gStartStopNo = startStop; gDestStopNo = destStop; if(addJourney) { addRecentJourney({'busNo':busNo, 'firstStop':firstStop, 'firstStopName':gStartStop.searchstr, 'lastStop':lastStop, 'lastStopName':gDestStop.searchstr}); addJourney = false; } getLocation(); // document.getElementById("journey").innerHTML = "Total distance: " + distFormat(total_dist, MAXROUTEDIST) + " (" + total_time + " mins)"; document.getElementById("routeStops").innerHTML = ht; return; }); function initmodule() { var x; a = getb(); for (x=0;x' + a[x][0] + ", " + a[x][1] + ", " + a[x][2]); } // Show route stops in table function showRouteStops(stops, hlstop, sS, dS) { var sStop, dStop; sStop = parseInt(sS); dStop = parseInt(dS); var dir = 1; if(sDir === 'd') dir = -1; /* if(sStop > dStop && dStop) { bDir = 'd'; dir = -1; } */ if (sStop) sStop -=1; if(dStop) dStop -=1; if((dStop == 0 || dStop == -1) && dir != -1) { dStop = stops.length - 1; lastStop = dStop; } stopsHtml = ""; if(hlstop > 0) hlStyle = 'style="background-color:cyan; color:red;"'; else hlStyle = ''; var x = sStop; total_dist = 0; total_time = 0; if(sStop > dStop) { firstStop = dStop + 1; lastStop = sStop + 1; } else { firstStop = sStop + 1; lastStop = dStop + 1; } while(true) { // for(var x=0; x < stops.length; x++) { if(stops[x] != "") { ar = stops[x]; if(x == hlstop && hlstop != 0) stopsHtml = stopsHtml + ''; else stopsHtml = stopsHtml + ''; if(x+1 == gStartStop.stopno) gStartStop.searchstr = ar[SEARCHSTR]; if(x == dStop) gDestStop.searchstr = ar[SEARCHSTR]; mins = ""; // stopsHtml = stopsHtml + colStart + ar[STOPNO] + colEnd; stopsHtml = stopsHtml + colStart + ar[SEARCHSTR] + colEnd; met = typeof(ar[NXTDIST]) != "undefined" ? distFormat(parseInt(ar[NXTDIST]), MAXSTOPDIST) : ""; var missingLoc = true; if(typeof(ar[NXTTIME]) != "undefined" && ar[NXTDIST] < 2000) { missingLoc = false; mins = " (" + ar[NXTTIME] + " mins)"; } stopsHtml = stopsHtml + colStart + met + colEnd; stopsHtml = stopsHtml + colStart + mins + " "; stopsHtml = stopsHtml + ""; if(missingLoc) { // if no info, take average distance as 500 m and average time as 3 mins total_dist += 500; total_time += 3; } else { total_dist += typeof(ar[NXTDIST]) != "undefined" ? parseInt(ar[NXTDIST]) : 0; total_time += typeof(ar[NXTTIME]) != "undefined" ? parseInt(ar[NXTTIME]) : 0; } } x += dir; if(x < 0 || x > stops.length) break; if(dir < 0 && x < dStop) break; if(dir > 0 && x > dStop) break; } if(gDestStop.stopno == 0) { gDestStop.stopno = x-2; gDestStop.searchstr = ar[SEARCHSTR]; } var hdrHtml = '' hdrHtml = hdrHtml + ''; stopsHtml = hdrHtml + stopsHtml + "
[Area] Stop name Dist. to Next StopTime to Next Stop
"; return stopsHtml; } /* // Read the route from server and write to file function openBusRouteFile(busNo) { var xmlhttp; xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","init/busstoplist.php",false); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("busno=" + busNo + "&s=0&d=0"); return xmlhttp.responseText.split("\n"); } */ // Shows the current location and hilites the stop in the stop list function showLocation(pos) { dBug('showLocation: ', 'lat: ' + pos.coords.latitude + ', lon: ' + pos.coords.longitude, 5); // send location to server to identify bus route called_times++; var curStopNo = 0; // var bDir = 'd'; var idxnearstop = -1; if(arBusStops.length == 0 || pos == undefined) { document.getElementById("nearStop").innerHTML = 'N/A';; return; } nearStop = ""; locHtml = ""; prvdi = -1; for(var x=0; x < arBusStops.length; x++) { if(arBusStops[x] != "") { ar = arBusStops[x]; if(x == 1) { firstStop = ar[STOPNO]; firstStopName = ar[SEARCHSTR]; document.getElementById("nearStop").innerHTML = 'First Stop: ' + firstStop; } } // dBug('Stop: ' + x + ' of ' + stopLines.length + ' - ' + ar[SEARCHSTR]); } // document.getElementById("myLoc").innerHTML = 'First Stop: ' + firstStop; lastStop = lastStop > 0 ? lastStop :ar[STOPNO]; // document.getElementById("myLoc").innerHTML = 'Last Stop: ' + lastStop; // dBug('first: ' + firstStop + ', last: ' + lastStop); if(pos == undefined) return; diTillNearStop = 0; timeTillNearStop = 0; // Get nearest stop for(var x=0; x < arBusStops.length; x++) { // document.getElementById("myLoc").innerHTML = ''; if(arBusStops[x] == "") continue; ar = arBusStops[x]; // document.getElementById("myLoc").innerHTML = 'Stop: ' + x + ' of ' // + arBusStops.length + ' - ' + ar[SEARCHSTR]; dLat = ar[LAT]; dLon = ar[LON]; if(dLat !== 0 && dLon !== 0) { diNearStop = Math.abs(distanceLatLon(dLat, dLon, pos.coords.latitude, pos.coords.longitude)); if(diNearStop < prvdi || prvdi == -1) { prvdi = diNearStop; idxnearstop = x; arNearStop = ar; } diTillNearStop += typeof(ar[NXTDIST]) != "undefined" ? parseInt(ar[NXTDIST]) : 0; timeTillNearStop += typeof(ar[NXTTIME]) != "undefined" ? parseInt(ar[NXTTIME]) : 0; } } diNearStop = prvdi; var maxStop = firstStop; var minStop = lastStop; var locationFound = true; var nowMs = new Date(); var fixTime = new Date(pos.timestamp); var fixAge = nowMs - fixTime; // dBug('fixms' + pos.timestamp + ', time:' + fixTime + ' Now:' + nowMs); if ( typeof showLocation.called_times == 'undefined' ) { // first time, define static vars lat and lon called_times = 0; dLat = 0; dLon = 0; // return; } if(firstStop < lastStop) { // bDir = 'u'; minStop = firstStop; maxStop = lastStop; } var pastDestination = false; var beforeStart = false; var beforeMsg = "Journey hasn't started"; var pastMsg = "You have passed your destination, please get down when bus stops"; var atMsg = ""; var myLoc = ''; var htmLocInfo = "" + colStart + "(Location Accuracy - " + distFormat(pos.coords.accuracy, MAXROUTEDIST) + ", at " + timeStr(fixTime) + ") " + colEnd + ""; if(prvdi != -1) { // document.getElementById("myLoc").innerHTML = 'Found location ....'; try { // document.getElementById("myLoc").innerHTML = 'Processing location ....'; if(sDir == 'u') { if(arNearStop[STOPNO] < minStop || diNearStop > 2000) // proably far away 2 km from any stop beforeStart = true; if(arNearStop[STOPNO] > maxStop) pastDestination = true; } else { if(arNearStop[STOPNO] < minStop) pastDestination = true; if(arNearStop[STOPNO] > maxStop) beforeStart = true; } sDiNearStop = diNearStop != undefined ? distFormat(diNearStop, 5) : "N/A"; var elm = "Nearest stop: " + arNearStop[STOPNO] + ". " + arNearStop[SEARCHSTR] + " (" + sDiNearStop + ") " ; myLoc = myLoc + elm; myLoc = '' + myLoc + htmLocInfo + '
'; myLoc = myLoc + colEnd + ''; var stops_remaining = Math.abs(lastStop - arNearStop[STOPNO]); var bc = ''; // background color for remaining stops\ var almostthere = ''; if(stops_remaining <= 2) { bc = "id='neardest'" if(stops_remaining < 1 ) { almostthere = 'Please Alight'; myLoc = atMsg + myLoc; } else { almostthere = '(Almost There)'; } } var d = Math.abs(total_dist - diTillNearStop); remJourney = ''; rem_time = Math.abs(total_time - timeTillNearStop); nearStop = myLoc; if(pastDestination) { myLoc = pastMsg + myLoc; pastB4 = pastMsg; } else if(beforeStart) { myLoc = beforeMsg + myLoc; pastB4 = beforeMsg; } else if(stops_remaining > 0) { myLoc = myLoc + '' + colStart + 'Remaining ' + almostthere + colEnd + colStart + stops_remaining + ' Stops, ' + distFormat(d, MAXROUTEDIST) + ", " + rem_time + ' min. ' + colEnd + ''; remJourney = 'Remaining ' + almostthere + stops_remaining + ' Stops, ' + distFormat(d, MAXROUTEDIST) + ", " + rem_time + ' min. '; } } catch(e) { $("location").innerHTML = myLoc; return; } myLoc = '
' + myLoc + '
' // document.getElementById("myLoc").innerHTML = myLoc; } ht = showRouteStops(arBusStops, idxnearstop, gStartStopNo, gDestStopNo); document.getElementById("routeStops").innerHTML = ht; locAccur = "(Location Accuracy - " + distFormat(pos.coords.accuracy, MAXROUTEDIST) + ", at " + timeStr(fixTime) + ") " + colEnd + ""; nearStop = elm; // $('#pastB4').html(pastB4); $('#nearStop').html(nearStop); $('#locAccuracy').html(locAccur); // $('#remainingJourney').html(remJourney); /* + distFormat(pos.coords.accuracy, MAXROUTEDIST) + ", at " + timeStr(fixTime) + ") " + colEnd + ""; */ // send location to server to identify bus route if(trackJourney) sendBusInfo(busNo, pos, arNearStop, sDir); return; // dBug('In showLocation'); // showPosDetails(pos); } // eofn: showLocation(pos) function setGeoPos(pos) { geoPos = pos; } function include(arquivo){ //By Fabr�cio Magri e Micox //http://elmicox.blogspot.com/2006/12/include-em-javascript.html var novo = document.createElement('script'); novo.setAttribute('type', 'text/javascript'); novo.setAttribute('src', arquivo); document.getElementsByTagName('head')[0].appendChild(novo); //apos a linha acima o navegador inicia o carregamento do arquivo //portanto aguarde um pouco at� o navegador baix�-lo. :) } function showStopsMap() { loc = "http://mesn.org/bestbus/showstopsmap.php?busno=" + busNo + "&s=" + firstStop + "&d=" + lastStop; window.location.href = loc; return true; } // Send the location and bus info to server function sendBusInfo(busno, pos, arNearBus, dir) { var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { // document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } url = "http://mesn.org/bestbus/svr/sendbusinfo.php?b=" + busno + "&lat=" + pos.coords.latitude + "&lon=" + pos.coords.longitude + '&a=' + pos.coords.accuracy + '&s=' + arNearBus[STOPNO] + '&sn=' + arNearBus[SEARCHSTR] + '&d=' + dir; xmlhttp.open("GET", url, true); xmlhttp.send(); } // Add to the list of recent journeys function addRecentJourney(oJourney) { var rs = localStorage['recentJourneys']; var recentJourneysPrv; if(rs != null) recentJourneysPrv = JSON.parse(rs); recentJourneys = null; var recentJourneys = new Array; recentJourneys[0] = oJourney; if(recentJourneysPrv !== null && recentJourneysPrv != undefined) { for(var i = 0; i < recentJourneysPrv.length; i++) { if(recentJourneysPrv[i] === undefined || recentJourneysPrv[i] === null) continue; // check if already in array, else add alreadyThere = false; for(var j = 0; j < recentJourneys.length; j++) { if(recentJourneys[j].busNo === recentJourneysPrv[i].busNo && recentJourneys[j].firstStop === recentJourneysPrv[i].firstStop && recentJourneys[j].lastStop === recentJourneysPrv[i].lastStop) { alreadyThere = true; break; } } if(!alreadyThere) recentJourneys[recentJourneys.length] = recentJourneysPrv[i]; } } localStorage['recentJourneys']=JSON.stringify(recentJourneys); showRecentJourneys(); } function showRecentJourneys() { if(localStorage['recentJourneys'] === undefined || localStorage['recentJourneys'] === null) return; var recentJourneys = JSON.parse(localStorage['recentJourneys']); recentJourneysStr = ''; if(recentJourneys === null) return; if(recentJourneys.length) { // recentJourneysStr = '
'; for(var i = 0; i < recentJourneys.length && i < MAXRECENTJOURNEYS; i++) { if(recentJourneys[i] !== null && recentJourneys[i] !== undefined && typeof(recentJourneys[i]) === 'object') { elm = '"; recentJourneysStr = recentJourneysStr + elm; } } recentJourneysStr = recentJourneysStr + '
' + recentJourneys[i].busNo + '' + recentJourneys[i].firstStopName + '' + recentJourneys[i].lastStopName + "
'; document.getElementById("recentJourneys").innerHTML = recentJourneysStr; } }