
var CLegende=Class.create();
CLegende.prototype={
	initialize: function(GestEvtDyn) {
		this.gE=GestEvtDyn;
		//this.okModif=1;
		//tableau pour connaitre la hierarchie groupe Layer
		this.tabLayersGroupe=new Array();
		this.tabAnalysesGroupe=new Array();
		//tableau d'etat des layers
		this.tabEtat=new Array();
		this.tabEtatAnalyse=new Array();
		this.tabmaxScale=new Array();
		this.tabminScale=new Array();


		this.gE.ajouteEcouteur('FILTREELEMENT',this.AddFiltreElem,this);
		
		this.gE.ajouteEcouteur('RMFILTREELEMENT',this.RMFiltreElem,this);
		this.gE.ajouteEcouteur('NEW_STATE_LAYER_DONE',this.cocheLayerU,this);
		this.gE.ajouteEcouteur('SETNEWSTATELAYER',this.cocheLayerU,this);
		this.gE.ajouteEcouteur('SETNEWSTATEANALYSE',this.cocheAnalyseU,this);
		this.gE.ajouteEcouteur('ANALYSEMODIFIED',this.refreshAnalyse,this);
		this.gE.ajouteEcouteur('SETNEWSTATEGROUPE',this.BtncocheGroupe,this);
		this.gE.ajouteEcouteur('MAPMOVED',this.onMapMoved,this);
		this.gE.ajouteEcouteur('STATE_ANALYSE_UPDATED',this.updateStateA,this);
		this.gE.ajouteEcouteur('SETNEWSTATEETIQUETTES',this.cocheLayerEtiquettesU,this);
	},
	AddFiltreElem:function(idcouche)
	{
		this.RMFiltreElem(idcouche);
		Element.show("UnFiltreCouche"+parseInt(idcouche));
		Element.hide("FiltreCouche"+parseInt(idcouche));

	},
	RMFiltreElem:function(idcouche)
	{
		try{
			Element.show("FiltreCouche"+parseInt(idcouche));
			Element.hide("UnFiltreCouche"+parseInt(idcouche));
		}catch(e){

		}
	},
	setTitle:function(idLayer){
		//recuperation des zooms
		var TrLayer=$('trLay'+idLayer);
		try
		{
			var minScale=TrLayer.getAttribute('minscale');
			var maxScale=TrLayer.getAttribute('maxscale');
			if(minScale>parseFloat(mainCarte.dx)||maxScale<parseFloat(mainCarte.dx)){
				var texte='Couche disponible à la visualisation entre '+minScale+' et '+maxScale+' mètres';
				TrLayer.title=texte;
			}
		}catch(e)
		{
		}
	},
	cocheLayer:function(idLayer,etat){
		if(etat=="0"){
			Element.hide('igBtnCocheL'+idLayer);
			Element.show('igBtnDecocheL'+idLayer);

		}else{
			Element.hide('igBtnDecocheL'+idLayer);
			Element.show('igBtnCocheL'+idLayer);
		}
	},
	cocheLayerU:function(idLayer,etat){
		try{
			this.cocheLayer(idLayer,etat);
			this.tabEtat[parseInt(idLayer)]=etat;
			var TrLayer=$('trLay'+idLayer);
			var Groupe=TrLayer.getAttribute("groupe");
			var etat=this.verifieEtatGroupe(Groupe);
			mainCarte.tabLayers=this.tabEtat;
			if(Groupe!="-1"){
				this.cocheGroupe(Groupe,etat);
			}
		}catch(e){
		}
	},
	cocheLayerEtiquettes:function(idLayer,etat){
		if($('EtiquetteCouche'+idLayer))
		{
			if(etat=="0"){
				Element.hide('EtiquetteCouche'+idLayer);
				Element.show('EtiquetteCoucheDisabled'+idLayer);
			}else{
				Element.hide('EtiquetteCoucheDisabled'+idLayer);
				Element.show('EtiquetteCouche'+idLayer);
			}
		}
	},
	cocheLayerEtiquettesU:function(idLayer,etat){

		this.cocheLayerEtiquettes(idLayer,etat);
		//mainCarte.chgStateLayerEtiquettes(idLayer,etat);
	},
	cocheAnalyse:function(idAnalyse,etat){
		if(etat=="0"){
			Element.hide('igBtnCocheA'+idAnalyse);
			Element.hide('legende'+idAnalyse);
			Element.show('igBtnDecocheA'+idAnalyse);

		}else{
			Element.hide('igBtnDecocheA'+idAnalyse);
			Element.show('igBtnCocheA'+idAnalyse);
			Element.show('legende'+idAnalyse);
		}
	},
	updateStateA:function(evt){
		if(evt.state){
			state='1';
		}else{
			state=0;
		}
		this.cocheAnalyse(evt.id,state);
	},
	refreshAnalyse:function(idAnalyse){
		if(idAnalyse.indexOf('-'!=-1)){
			tabA=idAnalyse.split('-');
			idAnalyse=tabA[0];
		}
		var oldim=$('legende'+idAnalyse);
		var srco=oldim.src;
		oldim.src="/dynmap/images/wait.gif";
		srcot=	srco.split('&refresh');
		srco=srcot[0]+'&refresh='+Math.random();
		oldim.src=srco;
		///dynmap/getLegend.php?A=20&amp;path_application=/cartes/carte_demo_jl/&amp;B=FFFFFF&amp;title=no&amp;typelegende=PNG&amp;wImg=100
	},
	cocheAnalyseU:function(idAnalyse,etat){
		if(idAnalyse.indexOf('-'!=-1)){
			tabA=idAnalyse.split('-');
			idAnalyse=tabA[0];
		}
		var TrAnalyse=$('trAnalyse'+idAnalyse);
		if(TrAnalyse){
			var Layer=TrAnalyse.getAttribute("layer");
	
			for(var i in this.tabEtatAnalyse)
			{
				if(!isNaN(parseInt(i)))
				{
					if(i!=idAnalyse)
					{
						var TrAnalyse2=$('trAnalyse'+i);
						var Layer2=TrAnalyse2.getAttribute("layer");
	
						if(Layer==Layer2){
							this.cocheAnalyse(i,0);
						}
					}
				}
			}
	
			this.cocheAnalyse(idAnalyse,etat);
	
			this.tabEtatAnalyse[parseInt(idAnalyse)]=etat;
	
			var TrAnalyse=$('trAnalyse'+idAnalyse);
	
			var Groupe=TrAnalyse.getAttribute("groupe");
		}



	},
	verifieEtatGroupe:function(idgroupe){
		var nbEtat=this.tabLayersGroupe[idgroupe].length;
		nbBoucle=nbEtat;

		for (var i=0;i<nbBoucle;i++) {
			var IdLayer=this.tabLayersGroupe[idgroupe][i];
			if(this.tabEtat[IdLayer]=="1"){

				nbEtat--;
			}
		}

		if(nbEtat==0){
			return "2";
		}else if (nbEtat<nbBoucle){
			return "1";
		}else{
			return "0";
		}
	},
	chgStateGroupe:function(idGroupe,etat){

		nbBoucle=this.tabLayersGroupe[idGroupe].length-1;
		var Chaine="";
		for(var i=0;i<nbBoucle;i++){
			var IdLayer=this.tabLayersGroupe[idGroupe][i];
			Chaine+=IdLayer+'='+etat+',';
		}
		var IdLayer=this.tabLayersGroupe[idGroupe][i];
		Chaine+=IdLayer+'='+etat;
		mainCarte.chgStateGroupe(Chaine);

	},
	cocheGroupe:function(idGroupe,etat){

		if(etat=="2"){
			Element.show('igBtnCocheG'+idGroupe);
			Element.hide('igBtnDecocheG'+idGroupe);
			Element.hide('igBtnMicocheG'+idGroupe);

		}else if(etat=="1"){
			Element.show('igBtnMicocheG'+idGroupe);
			Element.hide('igBtnDecocheG'+idGroupe);
			Element.hide('igBtnCocheG'+idGroupe);


		}else{
			Element.show('igBtnDecocheG'+idGroupe);
			Element.hide('igBtnMicocheG'+idGroupe);
			Element.hide('igBtnCocheG'+idGroupe);

		}




	},
	//permet de reagir au clic d'un groupe,!!!!! ici on passe un etat decoche ou coche, alors qu'en affichage on a 3 etat
	BtncocheGroupe:function(idGroupe,etat){
		if(etat=="1"){
			this.cocheGroupe(idGroupe,"2");
		}else{
			this.cocheGroupe(idGroupe,"0");
		}
		var tailleTab=this.tabLayersGroupe[idGroupe].length;

		for (var i=0;i<tailleTab ;i++){
			var idLayerS=this.tabLayersGroupe[idGroupe][i];
			this.cocheLayer(idLayerS,etat);
			this.tabEtat[idLayerS]=etat;
		}
		this.chgStateGroupe(idGroupe,etat);
	},
	addLayer:function(idLayer,idgroupe,etat,minScale,maxScale){
		//alert(typeof(this.tabLayersGroupe[idgroupe]));
		if(typeof(this.tabLayersGroupe[idgroupe])!='object'){
			this.tabLayersGroupe[idgroupe]=new Array();
		}
		this.tabLayersGroupe[idgroupe][this.tabLayersGroupe[idgroupe].length]=idLayer;
		//	alert(this.tabEtat);
		this.tabEtat[idLayer]=etat;
		this.tabminScale[idLayer]=minScale;
		this.tabmaxScale[idLayer]=maxScale;
		//mainCarte.tabLayers=this.tabEtat;

	},
	addAnalyse:function(idAnalyse,idgroupe,etat){
		if(typeof(this.tabAnalysesGroupe[idgroupe])!='object'){
			this.tabAnalysesGroupe[idgroupe]=new Array();
		}
		this.tabAnalysesGroupe[idgroupe][this.tabAnalysesGroupe[idgroupe].length]=idAnalyse;
		this.tabEtatAnalyse[idAnalyse]=etat;

	},
	setMinMaxScale:function(curentScale){

		//var TabLayers=document.getElementsByClassName('trLayer');
		var nbLayers=this.tabEtat.length;
		
		for(var i=0;i<nbLayers;i++){
			var minScaleL=this.tabminScale[i];
			var maxScaleL=this.tabmaxScale[i];
			if(minScaleL&&maxScaleL){
				//('ok');
				idLayer=i;
				UnLayer=$('trLay'+idLayer);
				TblUnLayer=$('tblLay'+idLayer);
				TblLayOptions=$('tblLayOptions'+idLayer);
				//alert(UnLayer.getAttribute('class'));
				if( minScaleL>parseFloat(curentScale) || maxScaleL<parseFloat(curentScale)){
			
					UnLayer.setAttribute('className','layers_couch disabled');
					UnLayer.setAttribute('class','layers_couch disabled');
					//TblUnLayer.setAttribute('className','trLayer ImgHorsZoom');
					//TblUnLayer.setAttribute('class','trLayer ImgHorsZoom');
					//TblLayOptions.setAttribute('className','trLayer ImgHorsZoom');
					//TblLayOptions.setAttribute('class','trLayer ImgHorsZoom');
					
				}else{
					UnLayer.setAttribute('className','layers_couch');
					UnLayer.setAttribute('class','layers_couch');
					//TblUnLayer.setAttribute('className','trLayer');
					//TblUnLayer.setAttribute('class','trLayer');
					//TblLayOptions.setAttribute('className','trLayer');
					//TblLayOptions.setAttribute('class','trLayer');
				}
			}

		}
	
	},
	onMapMoved:function (bBoxObj){
	
		GestEvtDyn.lanceEvenement('DEBUGDYN',['map moved sur legende']);
		//alert('tete');
		//alert('toto');
		//alert('zoom'+bBoxObj.zoom);
		//GestEvtDyn.lanceEvenement('MAPPOVED DEMANDE'+bBoxObj+++
		this.setMinMaxScale(bBoxObj.zoom);
	}

}

//pour cacher//afficher les groupes de legende
function displayModGroupe(idGroupe){
	var tabGroupe=$('tabG'+idGroupe);
	if(tabGroupe.style.display==""){
		tabGroupe.style.display="none";
		Element.hide('igBtnmoinsG'+idGroupe);
		Element.show('igBtnplusG'+idGroupe);

	}else{
		tabGroupe.style.display="";
		Element.show('igBtnmoinsG'+idGroupe);
		Element.hide('igBtnplusG'+idGroupe);
	}
}

//pour cacher//afficher les groupes d'analyses
function displayModGroupeAnalyse(idGroupe){
	Element.toggle('igBtnmoinsA'+idGroupe);
	Element.toggle('igBtnplusA'+idGroupe);
}
