/**
 * 
 * refactorisation de la classe layer user common ...
 */
var AnalysePersoManager=Class.create();
AnalysePersoManager.prototype=Object.extend(new GestEvt(), {
	tabAnalysesPerso:null,
	obs:null,
	initialize:function(obs){
		this.obs=obs;
		this.obs.ajouteEvenement('DELETEANALYSEPERSO');		 
		this.obs.ajouteEcouteur('STATE_ANALYSE_UPDATED',this.updateStateA,this);
		this.obs.ajouteEcouteur('SETNEWSTATEANALYSE',this.chgStateAnalyse,this);
		this.obs.ajouteEcouteur('DELETEANALYSEPERSO',this.deleteAnalysePerso,this);
		this.initInterfaceAnalysePerso.lieEvenement('USERLOGGED');
		this.tabAnalysesPerso=new Array();

	},
	openCreateWindow:function(){
		var linkOpener='/dynmap/extensions/indexcarte.php?cont=creationanalyseclient';
		linkOpener+='&path_application='+path_application;
		openWindow(linkOpener,"creationanalyse","top=200,left=200,width=510,height=600,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
	},
	openModifWindow:function(idAnalyse){
		var linkOpener='/dynmap/extensions/indexcarte.php?cont=creationanalyseclient';
		linkOpener+='&path_application='+path_application;
		linkOpener+='&event=getInterfaceModificationAnalyse';
		linkOpener+='&idAnalyse='+idAnalyse;
		openWindow(linkOpener,"creationanalyse","top=200,left=200,width=510,height=600,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
	},	
	chgStateButton:function(idAnalyse,param,state){
	//	keyJs=param.id;
		keyJs = idAnalyse;
		if(state){
			Element.show('ACoche'+keyJs);
			Element.hide('ADecoche'+keyJs);
			Element.show('legendeAnalyse'+keyJs);
			
		}else{
			Element.hide('ACoche'+keyJs);
			Element.show('ADecoche'+keyJs);
			Element.hide('legendeAnalyse'+keyJs);
			
		}
	},
	updateStateA:function(evt){
		
		//;params:object =>id:id de l'analyse,layer:couche de l'analyse,state=etat 1/0
		//STATE_ANALYSE_UPDATED
		if(this.tabAnalysesPerso && this.tabAnalysesPerso[evt.id]){
			if(this.tabAnalysesPerso[evt.id].state!=evt.state){
				this.chgStateButton(evt.id,this.tabAnalysesPerso[evt.id],evt.state);
				this.tabAnalysesPerso[evt.id].state = evt.state;
			}
		}
	},
	chgStateAnalyse:function(idAnalyse,state){
		/*
		if(state=='0'){
			state=0;
		}else{
			state=1;
		}
		if(idAnalyse.indexOf('-')!=-1){
			tabA=idAnalyse.split('-');
			idAnalyse=tabA[0];
		}
		if(this.tabAnalysesPerso && this.tabAnalysesPerso[idAnalyse]){
			this.chgStateButton(idAnalyse,this.tabAnalysesPerso[idAnalyse],state);
			this.tabAnalysesPerso[idAnalyse].state = state;
		}
		alert('fini aanlyse');
		*/
	},
	deleteAnalysePerso:function(idAnalyse){
		var test=confirm('Supprimer l\'analyse?');
		if(test == true){
			this._analyseToDelete = idAnalyse;
			this.obs.ajouteEcouteur('STATE_ANALYSE_UPDATED',this._realDelete,this);
			this.obs.lanceEvenement('SETNEWSTATEANALYSE',[this.tabAnalysesPerso[idAnalyse]['key'],'0','']);
		//	return ;
				}
	},
	_realDelete:function(evt){
		if(this._analyseToDelete==evt.id){
			this.obs.enleveEcouteur('STATE_ANALYSE_UPDATED',this._realDelete);
			if(evt.layer >= 1000 && evt.layer < 10000){
				mainCarte.removeLayer(evt.layer);
			}
			var url = '/dynmap/class/modules/mvccarte.php?cont=IndividualAnalyse';
			url+='&event=deleteAnalysePerso';
			url+='&path_application='+path_application;
			url+='&idAnalyse='+evt.id;
			this._analyseToDelete =0;
			this.tabAnalysesPerso[evt.id] = null;
			var myAjax = new Ajax.Request(url, { method : "post", parameters : '',  asynchronous : true,onComplete:this.a_delete,objetLie:this});
		}
	},
	a_delete:function(res){
		this.initInterfaceAnalysePerso(1);
	},
	
	
	
	majCreateAnalysePerso:function(idAnalyse,position){
		this.tempPosition=position;
		this.tempAnalyse=idAnalyse;

		var urlPath='&path_application='+path_application;
    	
		var urlDyn=dynmap_url+'?method=GETINITPARAMSLAYER&args='+this.tabAnalysesPerso[idAnalyse]['layer']+urlPath;
		var myAjax = new Ajax.Request(urlDyn, { method : "get", parameters : '',  asynchronous : true,onComplete:this.a_majCreateAnalysePerso,objetLie:this,evalScripts:true});
 
  },
	a_majCreateAnalysePerso:function(res){
		eval('response = ' + res.responseText);
		mainCarte.addPersonalLayer(response,"top");
		
		this.obs.lanceEvenement('SETNEWSTATEANALYSE',[this.tabAnalysesPerso[this.tempAnalyse]['key'],'1','']);
		this.initInterfaceAnalysePerso(1);
		
	},
	
	
	
	initInterfaceAnalysePerso:function(uLog){
		if(uLog!='-1'){
			
			Element.show('interfacePersonnelAnalyse');
			var url = '/dynmap/class/modules/mvccarte.php?cont=IndividualAnalyse';
			url+='&event=getInterfaceAnalyseFromCurrentUser';
			url+='&path_application='+path_application;
			
			var  pars = '';
			if(this.tabAnalysesPerso){
				var str = '';
				for(i in this.tabAnalysesPerso){
					if(this.tabAnalysesPerso[i]){
						str += "&tabAnaPerso["+i+"][key]="+this.tabAnalysesPerso[i]['key'];
						str += "&tabAnaPerso["+i+"][layer]="+this.tabAnalysesPerso[i]['layer'];
						str += "&tabAnaPerso["+i+"][state]="+this.tabAnalysesPerso[i]['state'];
					}
				}
				pars = str;
			}
			var myAjax5 = new Ajax.Updater($('analysesPerso'),url, { method : "get", parameters : pars,  asynchronous : true,evalScripts:true,onComplete:this.a_initInterfaceAnalysePersoff});
		}
	},
	a_initInterfaceAnalysePersoff:function(res){
      
//		$('analysesPerso').innerHTML=res.responseText;
		/*
		for(i in tabAnalysesPerso){
			if(i != 'extend'){
				if(tabStateAnalysePerso[i] == 1){
					analysePersoManager.chgStateAnalyseButton(i,1);
				}else{
					analysePersoManager.chgStateAnalyseButton(i,0);
					tabStateAnalysePerso[i] = 0;
				}
			}
		}
		*/
	}
});

var analysePersoManager=new AnalysePersoManager(GestEvtDyn);


