function dir(ob) {
    var out = [];
    for (attr in ob){
        out.push(attr);
    }
    return out.sort();
}       

var tflLocationTypes = {
      stops: 'stop',
      addresses: 'address',
      'places of interest': 'poi',
      'post codes': 'locator'};
var tflDefaultLocationType = 'stop';


tflDescription = "Get directions and travel times for journies using public transport in London.";
CmdUtils.CreateCommand({
  author: {name: "Tom Wright", email: "tat.wright@googmail.com"},
  contributers: "Tom Parker",
  license: "BSD", 
  description: tflDescription,
  help: "",
  names: ["tfl-directions"],
  arguments: [{role: "goal",
               nountype: noun_arb_text,
               label: "there"},
              {role: "instrument",
               nountype: dir(tflLocationTypes),
               label: "way of specifying locations"},
              {role: "source",
               nountype: noun_arb_text,
               label: "here"}],
  preview: function( pblock, arguments ) {

    pblock.innerHTML = tflDescription;

    if (! arguments.goal.text){
         return;
    }      
    pblock.innerHTML = 'Get directions to <em>' + arguments.goal.text + "</em>";
    var source = arguments.source.text || Bin['last-source']();
    if (source){
        pblock.innerHTML += " from <em>" + source + "</em>";
    } else {
        pblock.innerHTML += " from (<b>missing</b>)"
    }

    var instrument = arguments.instrument.text
    if (instrument){
        pblock.innerHTML += " using " + instrument;
    }

    pblock.innerHTML += "<hr>\
<p>The <b>way of specifying</b> locations can be any of:\
<dl>\
<dt>stops (default)</dt>\
   <dd>All locations are bus, tube, or train stops</dd>\
<dt>post codes</dt>\
<dt>addresses</dt>\
<dt>places of interest</dt>\
</dl>\
<p> The tfl website will often ask for clarification of these locations </p>";

  },
  execute: function( arguments ) {
      var url = "http://journeyplanner.tfl.gov.uk/user/XSLT_TRIP_REQUEST2";
      var locationType = tflLocationTypes[arguments.instrument.text];
      if (!locationType){
          locationType = tflDefaultLocationType;
      }

      // if no source given use the last used source
      if (arguments.source.text){
          Bin['last-source'](arguments.source.text);
      }
      displayMessage(Bin['last-source']());


      url += Utils.paramsToString({
        name_origin: Bin['last-source'](),
        name_destination: arguments.goal.text,
        type_origin:locationType,
        type_destination:locationType,

        language:'en',
        sessionID:0,
        requestID:0,
        ptOptionsActive:1,
        itOptionsActive:1,
        imparedOptionsActive:1,
        ptAdvancedOptions:1,
        advOptActive_2:1,
        advOpt_2:1,
        execInst:'normal',
        command:'',
        itdLPxx_request:'',
        itdLPxx_view:'',
        itdLPxx_tubeMap:'',
        calculateDistance:1,
        nameState_origin:'notidentified',
        nameDefaultText_origin:'start',
        place_origin:'London',        
        nameState_destination: 'notidentified',
        nameDefaultText_destination:'end',
        place_destination: 'London',
        itdTripDateTimeDepArr:'dep',
        Submit:'Search',
        routeType:'LEASTTIME',
        name_via:'Enter+location+(optional)',
        nameState_via:'notidentified',
        nameDefaultText_via:'Enter+location+(optional)',
        type_via:locationType,
        place_via:'London',
        placeDefaultText_via:'London',
        includedMeans:'checkbox',
        inclMOT_11: 1,
        inclMOT_0:'on',
        inclMOT_1:'on',
        inclMOT_2:'on',
        inclMOT_4:'on',
        inclMOT_5:'on',
        inclMOT_7:'on',
        inclMOT_9:'on',
        trITMOTvalue101: 60,
        trITMOTvalue:20,
        trITMOT:100,});
      // I love server components that don't have sane default values - hurrah for tfl
        Utils.openUrlInBrowser(url);

  }
});


