var request = require( 'request' );
var async = require( 'async' );
var _ = require( 'underscore' );
var moment = require( 'moment' );
var parseString = require('xml2js').parseString;
var apiRequest = require( "./utils" ).apiRequest;
var convertXML = require( "./utils" ).convertXML;


module.exports = function ( apiKey ) {

    if ( !apiKey ) {
        throw new Error( "CTA Bustracker requires an API key" );
    }

    var apiUrl = "http://www.ctabustracker.com/bustime/api/v1";

    var baseOptions = {
        qs: {
            key: apiKey
        },
        method: "GET"
    };

    var requestFactory = function ( options ) {
        return function ( done ) {
            return apiRequest( options, done );
        };
    };

    return {
        getTime: function ( cb ) {
            var options = _.clone( baseOptions );
            options.url = apiUrl + "/gettime";

            var makeRequest = requestFactory( options );

            async.waterfall( [ makeRequest, convertXML ],
                function ( err, data ) {

                    if ( err ) {
                        return cb( err );
                    }
                    return cb( null, data );

                } );
        },
        getVehiclesById: function ( vehicleIds, cb ) {
            var options = _.clone( baseOptions );
            options.url = apiUrl + "/getvehicles";
            options.qs.vid = vehicleIds.join( "," );

            var makeRequest = requestFactory( options );

            async.waterfall( [ makeRequest, convertXML ],
                function ( err, data ) {

                    if ( err ) {
                        return cb( err );
                    }
                    return cb( null, data );

                } );
        },
        getVehiclesByRoute: function ( routeIds, cb ) {
            var options = _.clone( baseOptions );
            options.url = apiUrl + "/getvehicles";
            options.qs.rt = routeIds.join( "," );

            var makeRequest = requestFactory( options );

            async.waterfall( [ makeRequest, convertXML ],
                function ( err, data ) {

                    if ( err ) {
                        return cb( err );
                    }
                    return cb( null, data );

                } );
        },
        getRoutes: function ( cb ) {
            var options = _.clone( baseOptions );
            options.url = apiUrl + "/getroutes";

            var makeRequest = requestFactory( options );

            async.waterfall( [ makeRequest, convertXML ],
                function ( err, data ) {

                    if ( err ) {
                        return cb( err );
                    }
                    return cb( null, data );

                } );
        }
    };

};

@@ -755,12 +755,9 @@
     
-getT
+t
 ime:
@@ -1206,36 +1206,33 @@
      %7D,%0A        
-getV
+v
 ehiclesById: fun
@@ -1758,12 +1758,9 @@
     
-getV
+v
 ehic
@@ -2296,16 +2296,489 @@
     
-getRoute
+routes: function ( cb ) %7B%0A            var options = _.clone( baseOptions );%0A            options.url = apiUrl + %22/getroutes%22;%0A%0A            var makeRequest = requestFactory( options );%0A%0A            async.waterfall( %5B makeRequest, convertXML %5D,%0A                function ( err, data ) %7B%0A%0A                    if ( err ) %7B%0A                        return cb( err );%0A                    %7D%0A                    return cb( null, data );%0A%0A                %7D );%0A        %7D,%0A        routeDirection
 s: f
@@ -2778,32 +2778,41 @@
 ions: function (
+ routeId,
  cb ) %7B%0A        
@@ -2885,39 +2885,80 @@
 = apiUrl + %22/get
+directions%22;%0A            options.qs.rt = 
 route
-s%22
+Id
 ;%0A%0A            v

Add routeDirections