function FromToOverlay(opt_options) {
  // Initialization
  this.setValues(opt_options);
};

FromToOverlay.prototype = new google.maps.OverlayView;

// Implement onAdd
FromToOverlay.prototype.onAdd = function() {
  this.from = new google.maps.Marker({
    map: this.getMap(),
    icon: new google.maps.MarkerImage( "/images/markers/departure.png",
      new google.maps.Size( 20, 10),
      new google.maps.Point(0,0),
      new google.maps.Point(0,5)),
    visible: false
  });
  this.to = new google.maps.Marker({
    map: this.getMap(),
    icon: new google.maps.MarkerImage( "/images/markers/arrival.png",
      new google.maps.Size( 20, 10),
      new google.maps.Point(0,0),
      new google.maps.Point(20,5)),
    visible: false
  });
 // Ensures the label is redrawn if the text or position is changed.
  var my_label = this.get("label");
  var from_marker = this.from;
  var to_marker = this.to;
  this.listeners_ = [
    google.maps.event.addListener(from_marker, 'mouseover',
      function() { my_label.set("marker", from_marker); }),
    google.maps.event.addListener(from_marker, 'mouseout',
      function() { my_label.hide(); }),
    google.maps.event.addListener(to_marker, 'mouseover',
      function() { my_label.set("marker", to_marker); }),
    google.maps.event.addListener(to_marker, 'mouseout',
      function() { my_label.hide(); })
  ];
};

// Implement onRemove
FromToOverlay.prototype.onRemove = function() {
 // Label is removed from the map, stop updating its position/text.
  for (var i = 0, I = this.listeners_.length; i < I; ++i) {
    maps.google.event.removeListener(this.listeners_[i]);
  }
};

FromToOverlay.prototype.fitSelection = function() {

  var fromOrToLatLngs = [];
  if ( this.from.getVisible()) {
    fromOrToLatLngs.push(this.from.getPosition());
  }
  if ( this.to.getVisible()) {
    fromOrToLatLngs.push(this.to.getPosition());
  }
  fromOrToLatLngs = $A(fromOrToLatLngs);

  if ( fromOrToLatLngs.size()==1) {
   // don't change zoom, just center map
    this.getMap().panTo( fromOrToLatLngs.first());
  } else if ( fromOrToLatLngs.size()==2) {
    var bounds = new google.maps.LatLngBounds();
    bounds.extend( fromOrToLatLngs.first());
    bounds.extend( fromOrToLatLngs.last());
    this.getMap().fitBounds( bounds);
  }
}

// Implement draw
FromToOverlay.prototype.draw = function() {
  var fromVisible = ( this.get("fromLat")!=null && this.get("fromLng")!=null);
  var toVisible   = ( this.get("toLat")!=null && this.get("toLng")!=null);
  var fromLatLng = new google.maps.LatLng(this.get("fromLat"), this.get("fromLng"));
  var toLatLng = new google.maps.LatLng(this.get("toLat"), this.get("toLng"));
  var fromString = "Départ";
  if ( this.get("fromName")!=undefined) {
    fromString += ": "+this.get("fromName");
  }
  var toString = "Arrivée";
  if ( this.get("toName")!=undefined) {
    toString += ": "+this.get("toName");
  }

  this.from.setOptions({
    text: fromString,
    visible: fromVisible,
    position: fromLatLng
  });
  this.to.setOptions({
    text: toString,
    visible: toVisible,
    position: toLatLng
  });
};

