OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                
 defaultHandlerOptions: {
                    'single': true,
                    'double': false,
                    'pixelTolerance': 0,
                    'stopSingle': false,
                    'stopDouble': false
                },


                initialize: function(options) {
                    this.handlerOptions = OpenLayers.Util.extend(
                        {}, this.defaultHandlerOptions
                    );
                    OpenLayers.Control.prototype.initialize.apply(
                      	 this, arguments
                    ); 
                    this.handler = new OpenLayers.Handler.Click(
                        this, {
                            'click': this.trigger
                        }, this.handlerOptions
                    );
                }, 

               trigger: function(e) {
					var x = e.xy.x;
					var y = e.xy.y;
					var currbounds = map.calculateBounds(map.getCenter(), map.getResolution());
					lonlat = map.getLonLatFromPixel(new OpenLayers.Pixel(x,y));
					var format;
					var srs = "EPSG:27572";
					format = "text/html";
					//var layer = 'communes_adherentes';
					var layer = map.getLayersByName('communes_adherentes')[0];
					var url =  layer.getFullRequestString({
										'REQUEST': "GetFeatureInfo",
										'EXCEPTIONS': "application/vnd.ogc.se_xml",
										'BBOX': currbounds.toBBOX(),
										'SRS': srs,
										'X': x,
										'Y': y,
										'RADIUS': 20,
										'INFO_FORMAT': format,
										'QUERY_LAYERS': layer.params.LAYERS ,
										'WIDTH': map.size.w,
										'HEIGHT': map.size.h});
					if(layer.getVisibility()==true) {
						OpenLayers.loadURL(url,{'nomLayer':'communes_adherentes'}, this, lancePopup);
					}
                }
           });



// different popup types
function zoomregion (){   
    if (map.getZoom()==0){
	var SldLayer2 = map.getLayersByName('dept')[0];			
	SldLayer2.params.SLD='http://sigore.observatoire-environnement.org/terresaine/SldDepartement.php?dept=$dept';
	SldLayer2.redraw();
	}
 }
function myCloseBoxCallback (e){   
    this.destroy(); 
    OpenLayers.Event.stop(e);
 }

        OpenLayers.Popup.FramedCloud.prototype.autoSize = true;
        AutoSizeFramedCloud = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
            'autoSize': true
        });
        AutoSizeFramedCloudMinSize = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
            'autoSize': true, 
            'minSize': new OpenLayers.Size(400,400)
        });
       AutoSizeFramedCloudMaxSize = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
            'autoSize': true, 
            'maxSize': new OpenLayers.Size(400,400)
        });

 var inseeI = null;
	function ajouteFeature(ll, popupClass, popupContentHTML, closeBox, overflow) {
		var layer = map.getLayersByName('communes_adherentes')[0];
		var feature = new OpenLayers.Feature(layer, ll); 
		feature.closeBox = closeBox;
		feature.popupClass = popupClass;
		feature.data.popupContentHTML = popupContentHTML;
		feature.data.overflow = (overflow) ? "auto" : "hidden";
		if (feature.popup == null) {
			feature.popup = feature.createPopup(feature.closeBox);
			map.addPopup(feature.popup);
			feature.popup.show();
			} else {feature.popup.toggle();}
			OpenLayers.Event.observe(feature.popup.closeDiv, "click", 
			OpenLayers.Function.bindAsEventListener(myCloseBoxCallback, feature.popup));
			var _insee_ajax = document.getElementById('ajax_code_insee'); 
			var codeInsee = _insee_ajax.innerHTML;
			inseeI =codeInsee ; 
			if (_insee_ajax != null) {
			// NBT : 30/08/2011
			// var SldLayer = map.getLayersByName('cosignataires')[0];			
			// SldLayer.setVisibility(true);
			// SldLayer.params.SLD="http://sigore.observatoire-environnement.org/terresaine/SldCosignataire.php?codeInsee="+codeInsee;
			// SldLayer.redraw();
			
			var CosLayer = map.getLayersByName('cosignataires')[0];			
			CosLayer.setVisibility(true);
			CosLayer.params.codeinsee=codeInsee;
			CosLayer.redraw();

			}
			var _divajax = document.getElementById('remplissage'); 
							if (_divajax != null) {
								eval(_divajax.innerHTML);
							}



      }

function lancePopup(response) {
	var Ow = new OpenLayers.Format.XML(); 
	var t1 = Ow.read(response.responseText);
	ogmlw = new OpenLayers.Format.GML(); 
	var t = Ow.getChildValue(t1.getElementsByTagName("gml:coordinates")[0]);
	var c = t.split(",");
	zoomNiveau = new OpenLayers.Bounds();
	if(response.responseText.length > 1) {
	contenuHtml = response.responseText;
	ajouteFeature(lonlat, AutoSizeFramedCloud,contenuHtml,true, false ,"");
	}
}

function unloadCosignataire() {    
          var CosignataireLayer = map.getLayersByName('cosignataires')[0];	
			CosignataireLayer.setVisibility(false);
 }



				function visibilityChanged_handler (e) {
				if (this.name =="communes_adherentes") {
						document.images["legImg"].src = "carto/images/legende_papillons.jpg";
						} else 
					{
						document.images["legImg"].src = "carto/images/legende_commune.jpg";
					}
					
				 
					
					
		
				};

// making this a global variable so that it is accessible for

      // debugging/inspecting in Firebug
	  
	  

 var map = null;
  function init(){
	var options = {'projection': new OpenLayers.Projection("EPSG:27572"),  'unit':"m", 'maxExtent': new OpenLayers.Bounds(299410.05,2012898.05,513664.9,2244364.9), 'resolutions': [399.0807758620687,250,100,50,25]};
	        map = new OpenLayers.Map('map', options);	
			var communes_adherentes = new OpenLayers.Layer.WMS( 
				"communes_adherentes", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'communes_adherentes', format: 'image/png'},
				{isBaseLayer:true,singleTile: true }
			);
			communes_adherentes.events.register('visibilitychanged',communes_adherentes,visibilityChanged_handler);			
			
			var communes_papillons = new OpenLayers.Layer.WMS( 
				"communes_papillons", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'communes_papillons', format: 'image/png'},
				{isBaseLayer:true,singleTile: true }
			);
			communes_papillons.events.register('visibilitychanged',communes_papillons,visibilityChanged_handler);
			
			var papillon1 = new OpenLayers.Layer.WMS( 
				"Communes &agrave; 1 papillon", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'papillon1', format: 'image/png', transparent:true},
				{isBaseLayer:true,singleTile: true}
			);
			var papillon2 = new OpenLayers.Layer.WMS( 
				"Communes &agrave; 2 papillons", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'papillons2', format: 'image/png'},
				{isBaseLayer:true,singleTile: true }

			);
			var papillon3 = new OpenLayers.Layer.WMS( 
				"Communes &agrave; 3 papillons", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'papillons3', format: 'image/png'},
				{isBaseLayer:true,singleTile: true}
			);

			var papillon4 = new OpenLayers.Layer.WMS( 
				"Communes &agrave; 4 papillons", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'papillons4', format: 'image/png'},
				{isBaseLayer:true,singleTile: true}
			);

			var cosignataires = new OpenLayers.Layer.WMS( 
				"cosignataires", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'cosignataires', format: 'image/png', transparent:true},
				{isBaseLayer:false,singleTile: true , displayInLayerSwitcher:false, visibility : false,opacity:0.5}
			);
			
			var fond = new OpenLayers.Layer.WMS( 
				"fond", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'fond', format: 'image/png', transparent:true},
				{isBaseLayer:false,singleTile: true , displayInLayerSwitcher:false, visibility : true}
			);
			var dept = new OpenLayers.Layer.WMS( 
				"dept", 
				"/cgi-bin/mapserv5?map=/var/www/html/terresaine/okmysql.map",
				{layers: 'dept', format: 'image/png', transparent:true},
				{isBaseLayer:false,singleTile: true , displayInLayerSwitcher:false, visibility : true}
			);

			
          map.addLayers([communes_adherentes,communes_papillons, fond,dept,cosignataires]);
		//	map.addLayers([communes_adherentes, fond,dept,cosignataires]);

            map.addControl(new OpenLayers.Control.LayerSwitcher());			
			map.setCenter(new OpenLayers.LonLat(411398.08,2128721.16));
			//map.zoomToMaxExtent;
			map.zoomToMaxExtent;
                var click = new OpenLayers.Control.Click();
                map.addControl(click);
                click.activate();
			map.events.register('zoomend',this,zoomregion);
		communes_adherentes.events.register("visibilitychanged", communes_adherentes, unloadCosignataire);



} // fin du init 

