fix bug: route view should shownext trip to finish not next to start
fix bug: route view should shownext trip to finish not next to start

--- a/include/common-template.inc.php
+++ b/include/common-template.inc.php
@@ -121,13 +121,13 @@
 		echo "<script>
 
 function success(position) {
-$('#error').val("Location now detected. Please wait for data to load.");
+$('#error').val('Location now detected. Please wait for data to load.');
 $('#geolocate').val(position.coords.latitude+','+position.coords.longitude);
 $.ajax({ url: \"include/common.inc.php?geolocate=yes&lat=\"+position.coords.latitude+\"&lon=\"+position.coords.longitude });
 location.reload(true);
 }
 function error(msg) {
-$('#error').val("Error: "+msg);
+$('#error').val('Error: '+msg);
 }
 
 function geolocate() {
@@ -169,7 +169,8 @@
     document.title = "' . $pageTitle . '";
 });
 </script>
-	<div data-role="header"> 
+	<div data-role="header" data-position="inline">
+	<a href="'.$_SERVER["HTTP_REFERER"].'" data-icon="arrow-l" data-rel="back">Back</a> 
 		<h1>' . $pageTitle . '</h1>
 	</div><!-- /header -->
         <a name="maincontent" id="maincontent"></a>

--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -5,7 +5,7 @@
 	"session",
 	"json",
 	"phperror",
-	"awsgtfs",
+	//"awsgtfs",
 	"awsotp",
 	//"squallotp",
 	//"vanilleotp",

--- a/schedule_viewer.py
+++ b/schedule_viewer.py
@@ -316,8 +316,19 @@
         except:
           print "Error for GetStartTime of trip #" + t.trip_id + sys.exc_info()[0]
         else:
-            result.append ( (starttime, t.trip_id) )
-    return sorted(result, key=lambda trip: trip[0])
+          cursor = t._schedule._connection.cursor()
+          cursor.execute(
+              'SELECT arrival_secs,departure_secs FROM stop_times WHERE '
+              'trip_id=? ORDER BY stop_sequence DESC LIMIT 1', (t.trip_id,))
+          (arrival_secs, departure_secs) = cursor.fetchone()
+          if arrival_secs != None:
+            endtime = arrival_secs
+          elif departure_secs != None:
+            endtime = departure_secs
+          else:
+            endtime =0
+          result.append ( (starttime, t.trip_id, endtime) )
+    return sorted(result, key=lambda trip: trip[2])
   
   def handle_json_GET_triprows(self, params):
     """Return a list of rows from the feed file that are related to this

file:a/trip.php -> file:b/trip.php
--- a/trip.php
+++ b/trip.php
@@ -9,7 +9,7 @@
 	$url = $APIurl . "/json/routetrips?route_id=" . $routeid;
 	$routetrips = json_decode(getPage($url));
 	foreach ($routetrips as $trip) {
-		if ($trip[0] > midnight_seconds()) {
+		if ($trip[2] > midnight_seconds()) {
 			$tripid = $trip[1];
 			break;
 		}