var RapportViewer = Class.create();
RapportViewer.prototype={
	initialize:function(div,path_application,params){
		this.extend(new GestEvt(),this);
		this.opt = {
				'categorieRapport': 'DYNMAP',
				'map': ''
			}.extend(params || {});
		this.divId=div;
		this.div=$(div);
		this.a=false;
		this.path_application=path_application;
		
		this.cr=this.opt.categorieRapport;
		this.rs=-1;
		this.updateViewer();
		this.ajouteEvenement('DS_MODIFIED');
		this.ajouteEvenement('DS_DELETE');
	},
	updateViewer:function(){
		var dynmap_urlpars = "path_application="+path_application+"&";
		dynmap_urlpars += 'method=RAPPORT&args=geListeJsonReport,typeRapport|'+this.cr+'|map|'+this.opt.map;
		
		var myAjax = new Ajax.Request(
		dynmap_url,
		{method: 'get', parameters: dynmap_urlpars, onComplete: this._updateViewer.bind(this)}
		);
	},
	_updateViewer:function(res){
		try{
			eval('var res='+res.responseText);
			this.rapports=res.rapports;
			var newSel='<select id="sel_'+this.divId+'" class="selectionListeRapport"> </select>&nbsp;<img border="0" src="/dynmap/images/excel_12.png';
			newSel+='" id="xl_'+this.divId+'"  />'
			this.div.innerHTML=newSel;
			this.Ob=$('sel_'+this.divId);
			var max=this.rapports.length;
			new_option = new Option('-- aucun --','-1');
			new_option.setAttribute("class","select_choisir");
			this.Ob.options[0]=new_option;
			for(i=0;i<max;i++){
				new_option = new Option(this.rapports[i].TITRE,i);
				new_option.setAttribute("title",this.rapports[i].TITRE);
				this.Ob.options[this.Ob.length]=new_option;
			}
			Event.observe(this.Ob, 'change', this._rapportChoosen.bindAsEventListener(this), false);
			Event.observe($('xl_'+this.divId), 'click', this._xport.bindAsEventListener(this), false);
		}catch(e){
			
		}
	},
	_xport:function(evt){
		if(this.rs==-1){
			alert('veuillez choisir un rapport');
			return false;
		}
		var ser='';
		if(this.rs==-2){
			ser+='&'+Form.serialize('data_chooser'+this.tchoose);
			this._bindDatums();
		}
		if(this._curRap['TYPE_SRC']=='SELECTION'){
			ser+='&selection_listen='+this._curRap['LAYER_SELECTION'];
		}
		var url='/dynmap/class/modules/mvccarte.php?cont=RAPPORT&event=viewRapport&idRapport='+this.rs[0];
		url +='&presenter=XLS';
		url +='&path_application='+this.path_application;
		url +=ser;
		window.open(url,'',"width=640,height=480,resizable=yes,menubar=yes");
	
	},
	_rapportChoosen:function(evt){
		var nVal=$F('sel_'+this.divId);
		
		if(this.a){
			this.div.removeChild(this.a);
			this.a=false;
		}
		if(nVal=='-1'){
			this.rs=-1;
			this.lanceEvenement('DS_DELETE');
		}else{
			this.tchoose=this.rapports[nVal].ID;
			this._curRap=this.rapports[nVal];
			var dynmap_urlpars="path_application="+path_application+"&";
			dynmap_urlpars +='method=RAPPORT&args=validateDataSource,idRapport|'+this.tchoose;
			
			var myAjax = new Ajax.Request(
					dynmap_url,
					{method: 'get', parameters: dynmap_urlpars, onComplete: this._updateChoose.bind(this)}
					);
		}
	},
	_updateChoose:function(res){
		if(res.responseText=="1"){
			var opt={};
			if(this._curRap['TYPE_SRC']=='SELECTION'){
				opt['selection_listen']=this._curRap['LAYER_SELECTION'];
			}
			this.lanceEvenement('DS_MODIFIED',[this.tchoose,opt]);
			this.rs=[this.tchoose,opt];
		}else{
		
			var aT='<div id="'+this.divId+'help" class="rapportaide_def rapportaide" >';
			aT+=res.responseText+' </div>'
			new Insertion.Bottom(this.divId,aT); 
			this.rs=-2;
			this.a=$(this.divId+'help');
		//	Form.focusFirstElement('data_chooser'+this.tchoose);
			
			Event.observe($('gen_rap_'+this.tchoose), 'click', this._dataOk.bindAsEventListener(this), false);
		}
	},
	_bindDatums:function(){
		var elements=Form.getElementsA('data_chooser'+this.tchoose);
		if(this._curRap['TYPE_SRC']=='SELECTION'){
			elements['selection_listen']=this._curRap['LAYER_SELECTION'];
		}
		this.rs=[this.tchoose,elements];
	},
	_dataOk:function(e){
		this._bindDatums();
		this.lanceEvenement('DS_MODIFIED',this.rs);
		//this.rs=[this.tchoose,opt];
	}
};