/**
 * 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){
			
			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>'+
							'<span id="parameters'+d+'"></span>';
							//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 de l\'analyse </strong> </p>'+
									'<div id="paramniv_c'+d+'" style="align:left"></div><button class="bouton refresh" type="button" onclick="javascript:void(0);" id="param_ad'+d+'"><span>Actualiser</span></button></div>'+
									'<div id="rea'+d+'">'+

									/*'<p><strong>Recherche sur cet indicateur </strong> </p>'+
									'<div>Définir un critère<p id="rechercheA'+d+'"></p></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));

                        //NV plus utilise depuis le 08/02/2011 : filtres dans la popin
			//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){
                        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);
			}
		
		},
		//reconstruit 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';
                        }

                        //bouton + pour ouvrir la popin d'infos sur l'analyse
                        $('modifByClientA'+niv).innerHTML="<a href=\"javascript:void(0);\" class=\"layers_more\" onclick=\"var infoBoxAnalyse = new Dynmap.Widget.AnalysisInfoBox({},'"+a.ANALYSEID+"','1'); mainCarte.addControl(infoBoxAnalyse); infoBoxAnalyse.redraw();\" title=\"Informations et paramètres\">Edit.</a>";
                        $('modifByClientA'+niv).style.display='';

                        //affichage des parametres de l'analyse
			if(a.PARAMS != undefined && a.PARAMS == 1 ){
				this._displayParams(a);
				this._displayParams2(a);
			
			}else{
				Element.hide('paramniv'+niv);
				Element.hide('parameters'+niv);
			}

                        //NV plus utilise depuis le 08/02/2011 : export xls dans la popin
			//$('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;
			
			//recuperation des parametre par interrogation AJAX
			var myAjax = new Ajax.Request(url, { method : "get", parameters : params,  asynchronous : true,evalScripts:false,onComplete:this._a_displayParams,objetLie: this});

		},
		_a_displayParams:function(res){
			
			$('paramniv_c'+this.id).innerHTML=res.responseText;
		},
		_displayParams2:function(a){
			var args={};
			args.analyseId = a.ANALYSEID;
			mainCarte.callRest('DynmapMap','getAnalyseParameters',args,this._a_displayParams2.bind(this));
		},
		_a_displayParams2:function(res){
			var paramstr = "";
			
			eval('var rep = '+res.responseText);
			
			var result = rep.result;
			for(var i in result){
				if(typeof(result[i]) != "function"){
					paramstr += '<span style="display:block;"><strong style="font-size: 0.8em;">'+result[i].label+' : </strong> ';
					paramstr += '<span style="font-size: 0.9em;">'+result[i].value.libelle+'</span></span>';
				}
			}
			$('parameters'+this.id).innerHTML = paramstr;
			Element.show('parameters'+this.id);
		},
		rea:function(ida){
			
			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.ajouteEcouteur('STATE_ANALYSE_UPDATED',this._a_setAnalyseParam.bind(this));
			this.gestMap.lanceEvenement('SETNEWSTATEANALYSE',[this.a.ANALYSEID+'-'+this.a.LAYERID,val,params]);
		},
		_a_setAnalyseParam:function(ida){
			this.gestMap.enleveEcouteur('STATE_ANALYSE_UPDATED',this._a_setAnalyseParam.bind(this));
			var id = ''+ida.id;
			if(ida.layer > 1000){
				id += '-'+ida.layer;
			}
			this.rea(id);
		},
		_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);
			}
		}
		
		
};
