 $(() ->
-
   map = new OpenLayers.Map("map");
   osmLayer = new OpenLayers.Layer.OSM();
   antNodes = null

  displayDirections = (directions) ->
    $("#directions").html("<ol><li>" + directions.join("</li><li>") + "</li></ol>")

  initMap = () ->

  map.events.on({
    "changelayer": (event) ->
      if shouldRetrieveNodes(event, "Ant nodes", antNodes)
        antNodes = retrieveNodes("/antnodes", antNodes, memorizeAntNodesAndAddAntNodeMarkers)
      else if shouldRetrieveNodes(event, "OSM nodes", osmNodes)
        retrieveNodes("/osmnodes", osmNodes, memorizeOsmNodesAndAddOsmNodeMarkers)
  })

    addMarkers("marker-green", size, offset, osmNodes, osmNodesLayer)

  retrieveNodes = (url, nodes, successFunction) ->
    $.get  url,
      success: (ns) ->
        successFunction(ns)

  shouldRetrieveNodes = (event, layerName, nodes) ->
    event.layer.name is layerName and event.property is "visibility" and event.layer.visibility and not nodes?

  retrieveDirections = () ->
    destination = $("#destination").val()
    source = $("#source").val()
    $.get "/directions", {
      destination
      source
      },
      (directions) ->
        displayDirections(directions)

  $("#retrieveDirections").click(retrieveDirections)
)
