OpenLayers.Popup.AutoSizedFramedCloud = OpenLayers.Class(
    OpenLayers.Popup.FramedCloud, {
        'autoSize': true,
        'fixedRelativePosition': true, // in order to be able to clic on the icons ...
        'relativePosition': 'tr', // idem
        'minSize': new OpenLayers.Size(150, 75),
        'maxSize': new OpenLayers.Size(600, 660)
});

/* mise en place d'un controle gérant click et hover sur les POI.
l'action hover déclenche la pop-up
l'action click déclenche la sélection du POI
et l'affichage des infos dans le panneau */

OpenLayers.Control.Hover = OpenLayers.Class(OpenLayers.Control, {
    layer: null,
    selectStyle: "select",
    selectedFeature: null,
    overFeature: function(feature) {},
    outFeature: function(feature) {},
    clickFeature: function(feature) {},
    clickoutFeature: function(feature) {},
    
    initialize: function(layer, options) {
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
        this.layer = layer;
        var callbacks = {
            over : this.overFeature,
            out: this.outFeature,
            click: this.onFeatureClick,
            clickout: this.onFeatureClickOut
        };
        
        this.handler = new OpenLayers.Handler.Feature(
            this, layer, callbacks, {geometryTypes: this.geometryTypes} // FIXME : geometryTypes not defined ?
        );
    },
    
    onFeatureClick: function(feature) {
        if (this.selectedFeature) {
            this.layer.drawFeature(this.selectedFeature, "default");
        }
        this.layer.drawFeature(feature, this.selectStyle);
        this.selectedFeature = feature;
        this.clickFeature(feature);
    },
    
    onFeatureClickOut: function(feature) {
        this.clickoutFeature(feature);
        this.selectedFeature = null;
    },

    /**
     * APIMethod: destroy
     * Take care of things that are not handled in superclass
     */
    destroy: function() {
        this.layer = null;
        this.selectedFeature = null;
        OpenLayers.Control.prototype.destroy.apply(this, []);
    }
});


/* 
 * Loading indicator ... this code from bartvde should become an addin 
 * I slightly modified it to display a "waiting" mouse cursor
 * See http://trac.openlayers.org/ticket/102
 */
OpenLayers.Control.LoadingPanel = OpenLayers.Class(OpenLayers.Control, {
     /**
      * Property: counter
      * {Integer} A counter for the number of layers loading.
      */
    counter: 0,

     /**
      * Property: maximized
      * {Boolean} A boolean indicating whether or not the control is maximized
     */
    maximized: false,

     /**
      * Property: layer
      * {OpenLayers.Layer}
     */
    layer: null,

     /**
      * Constructor: OpenLayers.Control.LoadingPanel
      * Display a panel across the map that says 'loading'.
      *
      * Parameters:
      * layer - {<OpenLayers.Layer.Grid>} Some layer which implements the loadstart and loadend
      *    properties. At the moment, this is only subclasses of the Grid layer.
      * options - {Object} additional options.
      */
    initialize: function(layer, options) {
        OpenLayers.Control.prototype.initialize.apply(this, [options]);

        if (!layer) {
            this.map.events.register('addlayer', this, this.addLayer);
            for (var i = 0; i < this.map.layers.length; i++) {
                var layer = this.map.layers[i];
                layer.events.register('loadstart', this, this.increaseCounter);
                layer.events.register('loadend', this, this.decreaseCounter);
            }
        } else {
            this.layer = layer;
            layer.events.register('loadstart', this, this.maximizeControl);
            layer.events.register('loadend', this, this.minimizeControl);
        }
    },

     /**
      * Method: addLayer
      * Attach event handlers when new layer gets added to the map
     */
    addLayer: function(evt) {
        var layer = this.map.layers[this.map.layers.length-1];
        layer.events.register('loadstart', this, this.increaseCounter);
        layer.events.register('loadend', this, this.decreaseCounter);
    },

     /**
      * Method: increaseCounter
      * Increase the counter and show control
     */
    increaseCounter: function() {
        this.counter++;
        if (this.counter > 0) {
            if (!this.maximized) {
                this.maximizeControl();
            }
        }
    },

     /**
      * Method: decreaseCounter
      * Decrease the counter and hide the control if finished
     */
    decreaseCounter: function() {
        if (this.counter > 0) {
            this.counter--;
        }
        if (this.counter === 0) {
            if (this.maximized) {
                this.minimizeControl();
            }
        }
    },

     /**
      * Method: draw
      * Create and return the element to be splashed over the map.
      */
    draw: function () {
        return null;
    },

     /**
      * Method: minimizeControl
      * Set the display properties of the control to make it disappear.
      */
    minimizeControl: function(e) {
        this.maximized = false;
        document.body.style.cursor = 'auto';
        if (e != null) {
            OpenLayers.Event.stop(e);
        }
    },

     /**
      * Method: maximizeControl
      * Make the control visible.
      */
    maximizeControl: function(e) {
        document.body.style.cursor = 'wait';
        this.maximized = true;
        if (e != null) {
            OpenLayers.Event.stop(e);
        }
    },

     /**
      * Method: destroy
      * Destroy control.
      */
    destroy: function() {
        if (this.layer) {
            this.layer.events.unregister('loadstart', this, this.maximizeControl);
            this.layer.events.unregister('loadend', this, this.minimizeControl);
            this.layer = null;
        } else {
            if (this.map) {
                this.map.events.unregister('addlayer', this, this.addLayer);
                if (this.map.layers) {
                    for (var i = 0; i < this.map.layers.length; i++) {
                        var layer = this.map.layers[i];
                        layer.events.unregister('loadstart', this, this.increaseCounter);
                        layer.events.unregister('loadend', this, this.decreaseCounter);
                    }
                }
            }
        }
        OpenLayers.Control.prototype.destroy.apply(this, arguments);
    },
    CLASS_NAME: "OpenLayers.Control.LoadingPanel"
});

