/**
 * Cette classe s'enregiste aupres d'un modele permetttant de choisir une analyse pour voir cette analyse, ainsi que les controles associés(telecharger, etc)
 * le modele doit lancer l'evenement ASK_LAUNCH_ANALYSE
 * elle s'enregistre également aupres de la carte , pour s'effacer lorsque on cherche a  ne plus la voir
 */

AnalyseCtrlViewer= Class.create();
AnalyseCtrlViewer.prototype={
		initialize:function(id,gestEvt,gestMap,widthLegend){
			this.id=id;
			this.idD=$(id);
			this.test=1;
			//alert(this.idD);
			this.widthLegend=widthLegend-10;
			this.rech=new RechercheAnalyse(id);
			//this.param=new AnalyseParam(id);
			this._prepareInterface(id);
			gestEvt.ajouteEcouteur('ASK_LAUNCH_ANALYSE',this.addAnalyse,this);
			gestMap.ajouteEcouteur('SETNEWSTATEANALYSE',this._setNewState,this);
			this.gestMap=gestMap;
			this.a=false;
			this.rech.ajouteEcouteur( 'RECHERCHE_ANALYSE_MESSAGE',this.handleMes,this);
			gestMap.ajouteEcouteur('RMFILTREELEMENT',this.rmFiltre,this);
			gestMap.ajouteEcouteur('ANALYSEMODIFIED',this.rea,this);
			
			this.rech.ajouteEcouteur( 'RECHERCHE_FILTRE_ADDED',this.setLF,this);
		},
		_prepareInterface:function(d){
			//construction du bloc titre
			/*var	inH='<div class="analyses" id="analysem'+d+' style="display:none;">'+
				'<div id="selectT'+d+'" style="display:none;">'+
				'<table cellpadding="0" cellspacing="0" border="0" width="100%"> '+
				'<tr><td width="15" width="100%">'+
				'<a id="dispA'+d+'" href="javascript:void(0);" title="cacher">'+
				'<img src="/dynmap/legende/themes/styleXP/checkbox_checked.gif" border="0"></a>'+
				'<a  id="hideA'+d+'" href="javascript:void(0);" title="visible" style="display:none">'+
				'<img src="/dynmap/legende/themes/styleXP/checkbox_unchecked.gif" border="0"></a></td>'+
				'<td> <div id=\'titreT'+d+'\' class="titreT"></div>	'+
				'<div id="stitreT'+d+'" class="stitreT"></div>			'		+				
				'</td><td width="10"><div id="modifByClientA'+d+'"></div></td><td width="10" ><div id="exportExcelA'+d+'"></div></td></tr></table>';*/
			var	inH='<div class="analyses" id="analysem'+d+' style="display:none;">'+
				'<div id="selectT'+d+'" style="display:none;">'+
					'<ul class="analyse_analys_bloc">'+
						'<li class="analyse_analys">'+
							'<span id="modifByClientA'+d+'"></span>'+
							'<span id="exportExcelA'+d+'"></span>'+
							'<a id="dispA'+d+'" href="javascript:void(0);" title="cacher" class="analyse_analys_link checkbox_on"><span id=\'titreT'+d+'\' class="titreT"></span></a>'+
							'<a id="hideA'+d+'" href="javascript:void(0);" title="visible"class="analyse_analys_link checkbox_off" style="display:none"><span id=\'titreT2'+d+'\' class="titreT"></span></a>'+
							'<em id="stitreT'+d+'" class="analyse_analys_more"></em>';
							//construction du bloc d interface
							inH+='<div id="interfaceA'+d+'" style="display:none;" class="interfaceAnalyse">'+
								'<div id="imgT'+d+'"></div>'+
								'<div id="paramniv'+d+'" style="display:none;">'+
									'<p><strong>Paramètres  sur cet indicateur </strong> </p>'+
									'<div id="paramniv_c'+d+'" style="align:left"></div><a href="javascript:void(0);" class="rechInterfaceChoix2_parametre" id="param_ad'+d+'" ><img src="/dynmap/images/rechInterfaceChoix_view.png" alt="lancer" /></a></div>'+
									'<div id="rea'+d+'">'+
									'<p><strong>Recherche sur cet indicateur </strong> </p>'+
									'<div>Définir un critère</p><p id="rechercheA'+d+'"></div>'+
									'<div class="rechInterfaceChoix1_bouton">'+
										'<a href="javascript:void(0);" class="rechInterfaceChoix2_localise" id="locad'+d+'" ><img src="/dynmap/images/rechInterfaceChoix_localise.png" alt="localiser" /></a>'+
										'<a href="javascript:void(0);" class="rechInterfaceChoix2_filtre" id="filtred'+d+'" ><img src="/dynmap/images/rechInterfaceChoix_filtre.png" alt="Filtrer" /></a>'+
										'<a href="javascript:void(0);" id="fraA'+d+'" title="enlever le filtre" style="display:none" class="rechInterfaceChoix2_unfiltre"><img src="/dynmap/images/rechInterfaceChoix_unfiltre.png" alt="enlever le filtre" /></a>'+
									'</div>'+
									'<div id="rechresA'+d+'" style="display:none">'+
										'<p id="rechrestxtA'+d+'"></p>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</li>'+
					'</ul>'+
				'</div>';
				this.idD=$(this.id); 
			if(this.idD){
				this.idD.innerHTML=inH;
			}
			
			this.rech.setRechercheInterface();
			Event.observe('hideA'+d,'click', this.chgView.bindAsEventListener(this, 1));
			Event.observe('dispA'+d,'click', this.chgView.bindAsEventListener(this, 0));
			
			Event.observe('locad'+d,'click', this.rech.getListeRowidE.bindAsEventListener(this.rech, 'loca'));
			Event.observe('param_ad'+d,'click', this.paramAnalyse.bindAsEventListener(this));
			Event.observe('filtred'+d,'click',  this.rech.getListeRowidE.bindAsEventListener(this.rech, 'filtre'));
			Event.observe('fraA'+d,'click',  this.rech.remFiltreE.bindAsEventListener(this.rech, 'filtre'));
			
		},
		
		addAnalyse:function(analyse){
			//on reconstruit la legende si elle est differente
			//alert(this.test);
			//if(this.a!=analyse){
				this.a=analyse;
				this.addLegende(analyse);
			//}
			this.a=analyse;
			this.rech.setA(analyse);
			this._setAllview();
			
			
		},
		_setAllview:function(){
			Element.show('analysem'+this.id);
			Element.show('selectT'+this.id);
			Element.show('interfaceA'+this.id);
			Element.hide('hideA'+this.id);
			Element.show('dispA'+this.id);
			if(this.a.TYPE!=4 &&this.a.TYPE!=6 &&this.a.TYPE!=1){
				Element.show('rea'+this.id);
				
			}else{
				Element.hide('rea'+this.id);
			}
		
		},
		//reoconstruit la legende
		addLegende:function(a){
			var niv=this.id;
			var imgS='/dynmap/getLegend.php?A='+a.ANALYSEID;
			imgS+='&path_application=/'+path_application+'/&B=FFFFFF&title=no&typelegende=PNG&wImg='+this.widthLegend+'&modeInclusion=img&rand='+Math.random();
			
			$('imgT'+niv).innerHTML='<img src="'+imgS+'" />';
			var linkExport='/dynmap/class/modules/mvccarte.php?cont=ANALYSERECHERCHE&event=doExport&type=excel';
			linkExport+='&path_application='+path_application+'&idA='+a.ANALYSEID;
			$('titreT'+niv).innerHTML=a.TITLE;
			$('titreT2'+niv).innerHTML=a.TITLE;
			if(a.S_TITLE&&a.S_TITLE!=''){
				$('stitreT'+niv).innerHTML=a.S_TITLE;
	      		$('stitreT'+niv).style.display='';
			}else{
				$('stitreT'+niv).style.display='none';
	    	}
	    	
	    	if(a.MODIFBYCLIENT&&a.MODIFBYCLIENT=='OUI')
	    	{
	    		$('modifByClientA'+niv).innerHTML="<a href=\"javascript:void(0);\" class=\"analyse_edit\" onclick=\"window.open('/dynmap/class/modules/mvccarte.php?cont=ANALYSECLIENT&event=getInterfaceModification&idAnalyse="+a.ANALYSEID+"&path_application="+path_application+"','Analyse','width=400,height=320,scrollbars=yes');\" title=\"Modifier\">Edit.</a>";
	      		$('modifByClientA'+niv).style.display='';
			}else{
				$('modifByClientA'+niv).style.display='none';
	    	}
			
			if(a.PARAMS != undefined && a.PARAMS == 1 ){
				this._displayParams(a);
			}else{
					Element.hide('paramniv'+niv);
			}
			
			$('exportExcelA'+niv).innerHTML='<a href="javascript:void(0);" class=\"analyse_excel\" onClick="window.open(\''+linkExport+'\',\'export_excel\',\'width=500, height=200\');" title="exporter en excel">Exporter en excel</a>';
			
		},
		_displayParams:function(a){
			Element.show('paramniv'+this.id);
			var url = ' /dynmap/flashengine/dynmapcontroler.php?method=ANALYSECLIENT&args=getInterfaceParametreAnalyse';
			url+='&path_application='+path_application;
			var params ='idAnalyse='+a.ANALYSEID;
			
		
			var myAjax = new Ajax.Request(url, { method : "get", parameters : params,  asynchronous : true,evalScripts:false,onComplete:this._a_displayParams,objetLie: this});

			//http://simalinux/dynmap/flashengine/dynmapcontroler.php?path_application=/cartes/carte_demo_jl/&method=RAPPORT&args=validateDataSource,idRapport|6&_=&randNumberCache=0.9076398112856366
			
			//recuperation des parametre par interrogation AJAX
		},
		_a_displayParams:function(res){
			
			$('paramniv_c'+this.id).innerHTML=res.responseText;
			
		},
		rea:function(ida){
			//alert('test');
			if(this.a){
				if(ida.indexOf('-'!=-1)){
					tabA=ida.split('-');
					ida=tabA[0];
				}
				if(this.a.ANALYSEID==ida){
					this.addLegende(this.a);
				}
			}
		},
		chgView:function(evt,val){
			//alert(val);
			//on relance la vue
			if(this.a){
				this.gestMap.lanceEvenement('SETNEWSTATEANALYSE',[this.a.ANALYSEID+'-'+this.a.LAYERID,val]);
			}
			if(val){
				Element.hide('hideA'+this.id);
				Element.show('dispA'+this.id);
			}else{
				Element.show('hideA'+this.id);
				Element.hide('dispA'+this.id);
			}
			
		},
		paramAnalyse:function(){
			var elements  =Form.serialize('frmAnalyseParam'+this.a.ANALYSEID);
			//var elements  =Form.serialize('frmAnalyseParam'+this.a.ANALYSEID);
			
			this.setAnalyseParam('fromreport=1&'+elements);
			//this.setAnalyseParam(params)
		},
		setAnalyseParam:function(params){
			val=0;
			if($('hideA'+this.id).style.display=='none'){
				val=1;
			}
			this.gestMap.lanceEvenement('SETNEWSTATEANALYSE',[this.a.ANALYSEID+'-'+this.a.LAYERID,val,params]);
		},
		_setNewState:function(descA,state){
			var tabA=descA.split('-');
			var a=tabA[0];
			if(this.a.ANALYSEID==a){
				if(state){
					
					Element.hide('hideA'+this.id);
					Element.show('dispA'+this.id);
					
					Element.show('interfaceA'+this.id);
				}else{
					
					Element.show('hideA'+this.id);
					Element.hide('dispA'+this.id);
					
					Element.hide('interfaceA'+this.id);
				}
				
			}
		},
		handleMes:function(mes){
			$('rechresA'+this.id).style.display='';
			$('rechrestxtA'+this.id).innerHTML=mes;
		},
		hideMes:function(){
			$('rechresA'+this.id).style.display='none';
		},
		setLF:function(lf){
			this.layerF=lf;
			Element.show('fraA'+this.id);
		},
		rmFiltre:function(idF){
			if(idF==this.layerF){
				Element.hide('fraA'+this.id);
				$('rechrestxtA'+this.id).innerHTML='';
			}
		}
		
		
}
