

/**
 * Class: OpenLayers.Control.CustomNavToolbar
 * 
 * Inherits from:
 *  - <OpenLayers.Control.NavToolbar>
 */
OpenLayers.Control.CustomNavToolbar = OpenLayers.Class(OpenLayers.Control.NavToolbar, {

    /**
     * Constructor: OpenLayers.Control.CustomNavToolbar 
     * Add our two mousedefaults controls.
     *
     * Parameters:
     * options - {Object} An optional object whose properties will be used
     *     to extend the control.
     */
    initialize: function(options) {
        OpenLayers.Control.Panel.prototype.initialize.apply(this, [options]);
        this.addControls([
          new OpenLayers.Control.Navigation({
            id: 'navigationbar'
            ,title: 'Déplacer la carte'
          })
          ,new OpenLayers.Control.QueryLayerBox({
            id: 'query-layer-box'
            ,title: 'Afficher les infos'
          })
          ,new OpenLayers.Control.ZoomInBox({title: 'Zoom + prédéfini' })
          ,new OpenLayers.Control.ZoomOutBox({title: 'Zoom - prédéfini' })
          ,new OpenLayers.Control.ZoomZeroBox({title: 'Afficher toute la couche' })
          ,new OpenLayers.Control.ZoomBox({title: 'Zoom manuel' })
        ]);
    },

    CLASS_NAME: "OpenLayers.Control.CustomNavToolbar"
});


// custom layer switcher
OpenLayers.Control.CustomLayerSwitcher = OpenLayers.Class( OpenLayers.Control, {
    /**
     * Constructor: OpenLayers.Control.CustomLayerSwitcher
     * 
     * Parameters:
     * options - {Object}
     */
        
    CLASS_NAME: 'OpenLayers.Control.CustomLayerSwitcher',
        
    initialize: function(options) {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
        this.setMap(map);
        $$('.layer_checkbox').each(function(el) {
            el.observe( 'click', this.setLayer.bindAsEventListener(this));
            el.observe('layer:activate', this.setLayer.bindAsEventListener(this));
            el.fire('layer:activate');
        }.bind(this));
    },
        
    setLayer: function(event) {
        this.map.layers.each(function(layer) {
            if(event.element().readAttribute('rel') == layer.options.name ) {
                if(layer.options.controllable) {
                    layer.setVisibility(event.element().checked);
                    /*if( event.eventName == 'layer:activate' && event.element().checked) {
                        $('layer-legend-'+layer.options.name).toggle();
                    }
                    else if( event.eventName != 'layer:activate') {
                        $('layer-legend-'+layer.options.name).toggle();
                    }*/
                }
            }
        });
    }
});

// custom query layer switcher
OpenLayers.Control.CustomQueryLayerSwitcher = OpenLayers.Class( OpenLayers.Control, {
    /**
     * Constructor: OpenLayers.Control.CustomQueryLayerSwitcher
     * 
     * Parameters:
     * options - {Object}
     */
        
    CLASS_NAME: 'OpenLayers.Control.CustomQueryLayerSwitcher',
        
    initialize: function(options) {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
        this.setMap(map);
        $$('.query_layer_radio').each(function(el) {
            el.observe('click', this.setQueryLayer.bindAsEventListener(this));
            el.observe('querylayer:activate', this.setQueryLayer.bindAsEventListener(this));
            el.fire('querylayer:activate');
        }.bind(this));
        
        $$('.legend_layer_radio').each(function(el) {
            el.observe('click', this.setLegendLayer.bindAsEventListener(this));
        }.bind(this));
    },
        
    setQueryLayer: function(event) {
        this.map.layers.each(function(layer) {
            if(layer.options.controllable && event.element().checked) {
                layer.options.current_query = event.element().readAttribute('rel') == layer.options.name;
            }
        });
    }
    ,setLegendLayer: function(event) {
        this.map.layers.each(function(layer) {
            if(event.element().readAttribute('rel') == layer.options.name ) {
                $('global-layer-legend-content').update($('layer-legend-'+layer.options.name).innerHTML);
                $('global-layer-legend').show();
                $('global-layer-legend-content').show();
            }
        });
    }
    ,getCurrentQueryLayer: function() {
        var _layer = null;
        this.map.layers.each(function(layer) {
            if(layer.options.current_query) {
                _layer = layer;
            }
        });
        return _layer;
    }
});

/**
 * @requires OpenLayers/Control.js
 * @requires OpenLayers/Handler/Box.js
 */

/**
 * Class: OpenLayers.Control.QueryLayerBox
 *
 * Inherits from:
 *  - <OpenLayers.Control>
 */
OpenLayers.Control.QueryLayerBox = OpenLayers.Class(OpenLayers.Control, {
    
    initialize: function(options) {
        this.handlers = {};
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
    },

    /**
     * Method: destroy
     * The destroy method is used to perform any clean up before the control
     * is dereferenced.  Typically this is where event listeners are removed
     * to prevent memory leaks.
     */
    destroy: function() {
        this.deactivate();
        OpenLayers.Control.prototype.destroy.apply(this,arguments);
    },
    
    /**
     * Method: deactivate
     */
    deactivate: function() {
        var state = OpenLayers.Control.prototype.deactivate.apply(this,arguments);
        $(this.div).fire('queryLayerBox:deactivate');
        return state;
    },
    
    /**
     * Method: draw
     */
    draw: function() {
        var div = OpenLayers.Control.prototype.draw.apply(this, arguments);
        // disable right mouse context menu for support of right click events
        if (this.handleRightClicks) {
            this.map.div.oncontextmenu = function () { return false;};
        }
        
        this.activate();
        return div;
    },
    
    CLASS_NAME: "OpenLayers.Control.QueryLayerBox"
});


OpenLayers.Control.ZoomInBox = OpenLayers.Class(OpenLayers.Control, {
    
    initialize: function(options) {
        this.handlers = {};
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
    },

    activate: function() {
        OpenLayers.Control.prototype.activate.apply(this, arguments);
        this.map.zoomIn();
        this.deactivate.bind(this).delay(1);
        this.map.getControl('navigationbar').activate.bind(this.map.getControl('navigationbar')).delay(1);
        return true;
    },
    
    CLASS_NAME: "OpenLayers.Control.ZoomInBox"
});

OpenLayers.Control.ZoomOutBox = OpenLayers.Class(OpenLayers.Control, {
    
    initialize: function(options) {
        this.handlers = {};
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
    },

    activate: function() {
        OpenLayers.Control.prototype.activate.apply(this, arguments);
        this.map.zoomOut();
        this.deactivate.bind(this).delay(1);
        this.map.getControl('navigationbar').activate.bind(this.map.getControl('navigationbar')).delay(1);
        return true;
    },
    
    CLASS_NAME: "OpenLayers.Control.ZoomOutBox"
});

OpenLayers.Control.ZoomZeroBox = OpenLayers.Class(OpenLayers.Control, {
    
    initialize: function(options) {
        this.handlers = {};
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
    },

    activate: function() {
        OpenLayers.Control.prototype.activate.apply(this, arguments);
        this.map.zoomTo(0);
        this.deactivate.bind(this).delay(1);
        this.map.getControl('navigationbar').activate.bind(this.map.getControl('navigationbar')).delay(1);
        return true;
    },
    
    CLASS_NAME: "OpenLayers.Control.ZoomZeroBox"
});

