 function KmlLoader(map,tab,baseUrl,mode,newRegion){
		
		this.map=map;
		
		this.tabUrl=tab;
		this.baseUrl=(baseUrl==undefined)?"":baseUrl;
		if(newRegion==undefined)this.newRegion=false;
		else this.newRegion=newRegion;
		
		this.mode=(newRegion)?"newregion":mode;

		this.polygonGroup = new L.FeatureGroup();
		this.map.addLayer(this.polygonGroup);
		
		KmlLoader.prototype.onFileReady=function(file){
			//console.log("OK",this,file.url);
			this.count++;
			if(this.count==this.tabUrl.length){
				if(this.callback!==undefined){
					if(this.observer!==undefined) this.callback.call(this.observer,this);
					else this.callback.call(this);
				}
				
			}
			//this.map.fitBounds(this.polygonGroup.getBounds());
		}
		KmlLoader.prototype.addKml=function(url){
			//console.log("addKml('"+url+"')");
			var url=this.baseUrl+url;
			var k=new KmlFile(url,"other");
			k.init=false;
			var l=k.load(this.onFileReady,this);
			l.on('click', function() { 
				//console.log(this.kmlfile.id);
				onClickPolygon.call(this.kmlfile);
			})
			l.id=k.id;
			l.addTo(this.polygonGroup);

		}
		KmlLoader.prototype.load=function(callback,obj){

			this.callback=callback;
			this.observer=obj;
			this.count=0;
			for(var i=0;i<this.tabUrl.length;i++){
				var url=this.baseUrl+this.tabUrl[i];
				var k=new KmlFile(url,this.mode);
				
				var l=k.load(this.onFileReady,this);
				//console.log("this.newRegion : ",this.newRegion)
				if(!this.newRegion){
					//console.log(l);
					l.on('click', function() { 
						//console.log(this.kmlfile.id);
						onClickPolygon.call(this.kmlfile);
					})
				    .on('mouseover',function(event){
				    	var l=event.layer;
				    	//console.log(l);
						//l.setStyle({fillOpacity:0.7})
				    	onMouseOverPolygon.call(this.kmlfile);
				    })
				    .on('mouseout',function(event){
				    	var l=event.layer;
				    	//console.log(l);
						//l.setStyle({fillOpacity:0.2})
				    	onMouseOutPolygon.call(this.kmlfile);
				    });
				}

				l.id=k.id;
			    l.addTo(this.polygonGroup);

			}
		}
	}
	
	function KmlFile(url,mode){
		this.init=true;
		this.url=url;
		var kml=url.split("/").pop();
		this.id=kml.split(".").shift();
		this.mode=(mode!==undefined)?mode:"region";
		//console.log("new KmlFile : ",this);
		KmlFile.prototype.onReady=function(){
			//console.log(arguments);
			//console.log("onReady : ",this);
			this.callback.call(this.observer,this);
		}
		this.customLayer=new Object();

		var transColor = '#bd1442'; // Rose Transentreprise - Couleur de base
		var partColor = '#642470'; // Noir Partenaire
		
		this.customLayer[this.mode] = L.geoJson(null, {
		    // http://leafletjs.com/reference.html#geojson-style
		    style: function(feature) {

	

				let color = transColor;
				if (feature.properties.description == 'PARTENAIRE'){
					color = partColor;
				}  

		        return {
		        	fillColor: color,
					color: color,  //Outline color
			        weight: 0.3,
			        fillOpacity: 0.2
		        };
		    }
		});
		this.customLayer["newregion"] = L.geoJson(null, {
		    // http://leafletjs.com/reference.html#geojson-style
		    style: function(feature) {

				let color = transColor;
				if (feature.properties.description == 'PARTENAIRE')  color = partColor;

		        return {
		        	fillColor: color,
					color: color,  //Outline color
			        weight: 2,
			        fillOpacity: 0.2
		        };
		    }
		});
		this.customLayer["other"] = L.geoJson(null, {
		    // http://leafletjs.com/reference.html#geojson-style
	
		    style: function(feature) {

				let color = transColor;
				if (feature.properties.description == 'PARTENAIRE')  color = partColor;

		        return {
		        	fillColor: color,
					color: color,  //Outline color
		        	weight: 0,
			        strokeOpacity: 0,
			        fillOpacity: 0.7
		        };
		    }
		});
		this.customLayer[this.mode].kmlfile=this;
		
		KmlFile.prototype.load=function(callback,obj){
			this.callback=callback;
			this.observer=obj;
			this.polygonLayer = omnivore.kml(this.url,null,this.customLayer[this.mode]).on('ready',function(){this.kmlfile.onReady()});
			return this.polygonLayer;
		}
		
		
	}
	function onMouseOverPolygon(){
		//console.log("over",this);
		var id=this.id;
		var z=$("#geo_"+id);
		if(!z.hasClass("selected")) this.polygonLayer.setStyle({fillOpacity:0.7});
	}
	function onMouseOutPolygon(){
		//console.log("out",this);
		var id=this.id;
		var z=$("#geo_"+id);
		if(!z.hasClass("selected")) this.polygonLayer.setStyle({fillOpacity:0.2})
	}
	function onClickPolygon(){
		var id=this.id;
		
		if($("#geo_"+id).length){
			$("#geo_"+id).trigger("click");
		}else{
			//recherche dans les token
			var item=null;
			var liste=$("#int-localisations").tokenInput("get");
			
			for(var i=0;i<liste.length;i++){
				
				if(id==liste[i].id){
					item=liste[i];
					break;
				}
			}
			if(null!==item) $("#int-localisations").tokenInput("remove",item);
				
			
		}
		
		
			
			
			
		//}
		
	}