OpenLayers.Control.DeleteFeature = OpenLayers.Class(OpenLayers.Control, {

  /**
   * Constant: EVENT_TYPES
   *
   * Supported event types:
   *  - *afterdeletioncommit* Triggered when a feature deletion is commited.
   */
  EVENT_TYPES: ['afterdeletioncommit'],
  
  /**
   * Constructs this control.
   * @param {OpenLayers.Layer} layer The layer the control acts on.
   * @param {Boolean} commit <code>true</code> if the deletion has to be committed to the shape file or
   * database.
   * @param {Object} options Other constructor options.
   */
  initialize: function(layer, commit, options) {
    // concatenate events specific to vector with those from the base
    this.EVENT_TYPES = OpenLayers.Control.DeleteFeature.prototype.EVENT_TYPES.concat(OpenLayers.Control.prototype.EVENT_TYPES);
    OpenLayers.Control.prototype.initialize.apply(this, [options]);
    this.layer = layer;
    this.commit = commit;
    this.handler = new OpenLayers.Handler.Feature(this, layer, {
      click: this.clickFeature
    });
  },
  
  /**
   * Deletes the feature from the layer and, if the <code>commit</code> parameter is <code>true</code>, then asks
   * the user to confirm and uses the <code>Save</code> strategy (if present) to commit the transaction. After the
   * commit the <code>afterdeletioncommit</code> event is triggered.
   * @param {OpenLayers.Feature} feature The feature to delete.
   */
  clickFeature: function(feature) {
    if (this.commit) {
      //confirm popup
      Ext.MessageBox.confirm('Conferma', 'La cancellazione di <b>' + feature.attributes.topocomple + '</b> è una operazione irreversibile.<br/>Procedere comunque?', function(button) {
        if (button == 'yes') {
          this.deleteFeature(feature);
          var saveStrategy = undefined;
          var strategies = this.layer.strategies;
          for (var ii = 0; !saveStrategy && ii < strategies.length; ii++) {
            if (strategies[ii].CLASS_NAME == 'OpenLayers.Strategy.Save') {
              saveStrategy = strategies[ii];
            }
          }
          if (saveStrategy) {
            saveStrategy.save();
            this.events.triggerEvent('afterdeletioncommit');
          }
        }
      }
.bind(this));
    } else {
      this.deleteFeature(feature);
    }
  },
  
  /**
   * Deletes the feature from the layer.
   * @param {OpenLayers.Feature} feature The feature to delete.
   */
  deleteFeature: function(feature) {
    // if feature doesn't have a fid, destroy it
    if (feature.fid == undefined) {
      this.layer.destroyFeatures([feature]);
    } else {
      feature.state = OpenLayers.State.DELETE;
      this.layer.events.triggerEvent('afterfeaturemodified', {
        feature: feature
      });
      feature.renderIntent = 'select';
      this.layer.drawFeature(feature);
    }
  },
  
  /**
   * Sets the map this control acts on.
   * @param {OpenLayers.Map} map The map to set.
   */
  setMap: function(map) {
    this.handler.setMap(map);
    OpenLayers.Control.prototype.setMap.apply(this, arguments);
  },
  
  CLASS_NAME: 'OpenLayers.Control.DeleteFeature'
});
