var LayerProperties = Class.create({
  initialize: function(zoomMin,zoomMax) {
    this.zoomMin = zoomMin;
    this.zoomMax = zoomMax;
    this.icons = {};
  },

  getZoomMin: function() {
    return this.zoomMin;
  },

  getZoomMax: function() {
    return this.zoomMax;
  },

  getImage: function(location) {},

  getTextForWindow: function() {
    return null;
  },
  getTextTemplate: function(from,to,location) {
    var str = I18n.t("itineraries.map.itinerary")+"&nbsp;:&nbsp;"
    // use inspect() to get JS escaped string
    str += "<a class=\"bubble_link\" href=\"#\" onclick=\"TripPlanner.formView.setExtremity('departure', '"+
                location.getTopoType()+"', "+location.getTopoId()+", "+location.getName().inspect()+
                ", '"+location.getLat()+"', '"+location.getLng()+
                "'); TripPlanner.controller.mapView.closeInfoWindow(); return false;\">"+from+"</a>&nbsp;-&nbsp;"
    str += "<a class=\"bubble_link\" href=\"#\" onclick=\"TripPlanner.formView.setExtremity('arrival',   '"+
                location.getTopoType()+"', "+location.getTopoId()+", "+location.getName().inspect()+
                ", '"+location.getLat()+"', '"+location.getLng()+
                "'); TripPlanner.controller.mapView.closeInfoWindow(); return false;\">"+to+"</a>"
    str += "&nbsp;"
    return "<div style=\"padding: 0; margin: 0; height: 60px; overflow: auto;\" class=\"info_window_contents\" ><h3>"+location.getName()+"</h3>"+str+"</div>";
  },
  markerCallBack: function (mapView,marker,location) {
    return null;
  }
});

var LayerStopProperties = Class.create(LayerProperties,{
  getImage: function(location) {
    return new google.maps.MarkerImage( iconByType[0],
                new google.maps.Size( 20, 20),
                new google.maps.Point(0,0),
                new google.maps.Point(10,10));
  },

//  getIconSize: function() {
//    return new GSize(20, 20);
//  },
//  getAnchor: function() {
//    return new GPoint(10, 10);
//  },
//  getShadowPath: function() {
//    return "/images/markers/shadow.png";
//  },
//  getShadowSize: function() {
//    return new GSize(24, 24);
//  },
  getTextForWindow: function(location) {
    var from = I18n.t("itineraries.map.from_stop");
    var to = I18n.t("itineraries.map.to_stop");
    return this.getTextTemplate(from,to,location);
  },
  markerCallBack: function (mapView,marker,location) {
    var text = this.getTextForWindow(location);
    google.maps.event.addListener(marker,"click", function() {
      mapView.infoWindow.setOptions({ position: marker.getPosition(),
                                      content: text });
      mapView.infoWindow.open(mapView.map, marker)
    });
  }

});

var LayerDefaultProperties = Class.create(LayerProperties,{
  getImage: function(location) {
    return location.icon;
  }

//  getIconSize: function() {
//    return new GSize(21, 32);
//  },
//  getAnchor: function() {
//    return new GPoint(11, 32);
//  },
});

var LayerPlaceProperties = Class.create(LayerProperties,{
  getImage: function(location) {
    return new google.maps.MarkerImage( iconByType[location.getTopoPlaceType()],
                new google.maps.Size( 20, 20),
                new google.maps.Point(0,0),
                new google.maps.Point(10,10));
  },

//  getIconSize: function() {
//    return new GSize(20, 20);
//  },
//  getAnchor: function() {
//    return new GPoint(10, 10);
//  },
  getTextForWindow: function(location) {
    var from = I18n.t("itineraries.map.from_place");
    var to = I18n.t("itineraries.map.to_place");
    return this.getTextTemplate(from,to,location);
  },
  markerCallBack: function (mapView,marker,location) {
    var text = this.getTextForWindow(location);
    google.maps.event.addListener(marker,"click", function() {
      mapView.infoWindow.setOptions({ position: marker.getPosition(),
                                      content: text });
      mapView.infoWindow.open(mapView.map, marker)
    }.bindAsEventListener(mapView));
  }
});

var LayerAddressProperties = Class.create(LayerProperties,{
  getTextForWindow: function(location) {
    var from = I18n.t("itineraries.map.from_address");
    var to = I18n.t("itineraries.map.to_address");
    return this.getTextTemplate(from,to,location);
  }
});

var HtmlContentProducer = Class.create( {
  initialize: function(properties) {
    this.properties = properties
  },

  get: function(key) {
    return this.properties[key];
  },

  produce: function() {
    var str = "<div style=\"padding: 0; margin: 0; height: 60px; overflow: auto;\" class=\"info_window_contents\" ><h3>"+this.get('name')+"</h3>";
    str += I18n.t("itineraries.map.itinerary")+"&nbsp;:&nbsp;"
    str += this.extremity_link( 'departure', this.get('from'));
    str += "-&nbsp;"
    str += this.extremity_link( 'arrival', this.get('to'));
//    str += "<p><a class=\"bubble_link\" href=\"#\" onclick=";
//    str += "\"TripPlanner.controller.address_overlay.hide();\" "
//    str += ">Retirer ce marqueur d'adresse</a></p>"
    return str;
  },

  extremity_link: function(depOrArr, fromOrTo) {
    var str = "<a class=\"bubble_link\" href=\"#\" onclick=\"TripPlanner.formView.setExtremity('";
    str += depOrArr;
    str += "', '";
    str += this.get("topoType");
    str += "', '";
    str += this.get("topoId");
    str +=  "', '";
    str += this.get("name");
    str +=  "', ";
    str += this.get("lat");
    str +=  ", ";
    str += this.get("lng");
    str += "); TripPlanner.mapView.closeInfoWindow(); return false;\">";
    str += fromOrTo;
    str += "</a>&nbsp;"
    return str;
  }
});
