var AnalyseSwitcherMultiple=Class.create();
AnalyseSwitcherMultiple.prototype={
    datasource:null,

		initialize:function(gestEvt,gestMap,datasource,initaOld){
		this.gestEvt=gestEvt;
		this.gestMap=gestMap;
			gestEvt.ajouteEcouteur('ASK_LAUNCH_ANALYSE',this.addAnalyse,this);

			gestMap.ajouteEcouteur('SETNEWSTATEANALYSE',this.isRemove,this);
			//gestMap.ajouteEcouteur('SETNEWSTATEANALYSE',this.isRemove,this);
			this.aOld=false;
			this.aNew=false;
			if(datasource!=undefined){
        this.datasource=datasource;
      }
      if(initaOld!=undefined && initaOld.ANALYSEID!=undefined) {
          this.aOld=initaOld;
          this.oldLayerVisibility=true;
      }

		},
		addAnalyse:function(analyse){
	   	
              var layer = mainCarte.layer(analyse.LAYERID);                     
              this.LayerVisibility = layer.visibility;
        
  			      // Demande de suppression de l'ancienne analyse
              if(this.aOld!=false){
                  
                  var strlayerAnalyse = this.aOld.ANALYSEID+'-'+this.aOld.LAYERID;
  
                  // Si la couche de l'analyse etait affiché avant on la conserve (sauf dans le cas d'une couche cercle ou camembert)
                  // Si la couche de l'analyse ne l'etait pas on l'efface avec l'analyse
                  if (this.oldLayerVisibility==true){
                      var strLayerLink = "";
                      if (this.aOld.LAYERID >= 1000){
                          strLayerLink = '-' + this.aOld.LAYERID;
                      }
                      strlayerAnalyse = this.aOld.ANALYSEID + strLayerLink;
                  }
                  this.gestMap.lanceEvenement('SETNEWSTATEANALYSE',[strlayerAnalyse,0]);
  			}
  			this.gestMap.lanceEvenement('SETNEWSTATEANALYSE',[analyse.ANALYSEID+'-'+analyse.LAYERID,1]);
  			
        
			  
			this.aOld=analyse;
			this.oldLayerVisibility = this.LayerVisibility;
			this._flagSelfDecision=false;
		},
		setViewClicked:function(idView, idAnalyse, val)
		{
		    //console.debug(idView+' '+idAnalyse+' '+val);
		    // this.setViewClicked = idDiv;
    },
		isRemove:function(descA,state){
		  desc = descA.split('-');
		  analyseId = desc[0];
		  layerId = desc[1];
			if(!state){
				if(this.aOld.ANALYSEID == analyseId ){
					this.aOld=false;
				}
			}
			//la je fais , enfonction d'un parametrage ? le job si mon analyse est presente(meme classe) je la change).
			//et en ce cas:
	    //optionsJsonViewer={"ANALYSEID":"40","testid":"40","LAYERID":"119","GRP":"20","TITLE":"Population%20active%20totale","LAYERPARENT":"119","TYPE":"2","typeAnalyse":"2","TYPEANALYSE":"1","id":"40","S_TITLE":"","DESCRIPTION":"","libelle":"Population%20active%20totale","idPere":"20","lPName":"Communes%20du%20d%C3%A9partement","MINSCALE":"6500","MAXSCALE":"9999999","PARAMETERS_MODIFICATION":"0","libelleGrp":"","STATE_INIT":"0"};
  
    
      if (this.datasource!=undefined && this.datasource.currentAnalyse )
      {
        if (this.datasource.tabHierarchie[2] != undefined)
        {
            var tabAnalyse = this.datasource.tabHierarchie[2];
            for(var i=0;i<tabAnalyse.length;i++)
            {
                // quelque fois le layerid n'est pas present
                if(analyseId == tabAnalyse[i].ANALYSEID){
                  layerId = tabAnalyse[i].LAYERID; 
                }
                state=tabAnalyse[i].STATE_INIT;
                if (this.datasource.MonTab[""+this.datasource.currentAnalyse.ANALYSEID]){
				          state = this.datasource.MonTab[""+this.datasource.currentAnalyse.ANALYSEID];
			          }
			          
			          canReplaceSameAnalyse=true;
			          myName = this.datasource.name;
			          for(var nameOnglet in synchronisationOnglet.currentAnalyseId)
		            {
				          if(nameOnglet!='extend' && nameOnglet!=myName )
				          {
				            if (synchronisationOnglet.currentAnalyseId[nameOnglet].ANALYSEID==analyseId)
				            {
				                canReplaceSameAnalyse = false;
                    }
				          }
                }
			          
                if( analyseId == tabAnalyse[i].ANALYSEID && this.datasource.currentAnalyse &&
                    this.datasource.currentAnalyse.ANALYSEID != analyseId && 
                    this.datasource.currentAnalyse.LAYERID==layerId && layerId<1000  && synchronisationOnglet.getPermission()==true &&
                    state=="1" && canReplaceSameAnalyse == true && tabAnalyse[i].onlyOnList == "1")
                {
                    //console.debug("passe apres normalement switcher");
                    this.gestEvt.triggerEvent('ANALYSE_SELECTED',[tabAnalyse[i]]);
                    synchronisationOnglet.currentAnalyseId[myName] = tabAnalyse[i];
                    
                }
      			}
  			}
  			
      }
      
      
			

		}
};
