var DynManagementContextClass = Class.create();
//Gestionnaire de contexte
DynManagementContextClass.prototype = {
	initialize:function(){

		this.extend(new GestEvt(),this);
		this.curParams='';

		this.ajouteEvenement('DefaultBEGIN');
		this.ajouteEvenement('DefaultEND');
				
		this.beginContext('Default');
	},
	addWatcher:function(contexte,objet){

		this.ajouteEcouteur(contexte+'BEGIN',objet.beginContext,objet);
		this.ajouteEcouteur(contexte+'END',objet.endContext,objet);
	},
	beginContext:function(contexte,params,id){
		if(typeof(id)=='undefined' || !id){
			id=contexte;
		}
	
		this.curContexte=new ContexteWatcher(contexte,params,id);
	
		this.curContexte.setMap(mainMap);
	
		this.curContexteName=contexte;
		this._curId= id;
		this.curParams=params;
		
	
		this._listenersNotify(contexte,'BEGIN');

		this.curContexte.onStart();
	
		return this.curContexte;
	},
	endContext:function(contexte){

		this.curContexte.onEnd();
		this._listenersNotify(contexte,'END');
	},
	endCurrentContext:function(){
		this.endContext(this.curContexteName);
	},
	_listenersNotify:function(nameEvent,typeNotify){
		this.lanceEvenement(nameEvent+typeNotify,[nameEvent,this.curParams,this._curId]);
	}
}

var ContexteWatcher = Class.create();

//prototype contexte
ContexteWatcher.prototype = {

	initialize: function(nom,objContexte,id) {
		if(typeof(id)=='undefined' || !id){
			id=nom;
		}
		this.nom=nom;
		if(window['DynContexte'+nom])	{
			var tp =eval('new DynContexte'+nom+'();');
			this.setParams(objContexte);
			Object.extend(this, tp || {});
		}else{
			var nameCla = nom.charAt(0).toUpperCase();
			nameCla=  nameCla + nom.substr(1);

			if(window['Dynmap'] && window['Dynmap']['Context']&& window['Dynmap']['Context'][ nameCla])	{
				
				var tp =eval('new Dynmap.Context.'+nameCla+'();');
				tp.setParams(objContexte);
				
				tp.contextName= nom;
				Object.extend(this,tp);
			
			}else{
				var tp =eval('new DynContexte'+nom+'();');
				this.setParams(objContexte);
				Object.extend(this, tp || {});
			}
		}
		this.id=id;
		
	},
	setMap : function(map) {
		this.map = map;
	},
	setParams: function(options) {

		this.objParam = {
			dynmapparam:       ''
		}.extend(options || {});
		var rowidCurrent=getCurrentObjectDrawed();
		if(rowidCurrent!=0){
			this.objParam.dynmapROWID=rowidCurrent;
		}

	},
	onStart:function(){
		try{
		SetevenementCl(0);
		}catch(e){

			//alert('pb ici');
		}
	},
	onEnd:function(){
		try{
		SetevenementCl(0);
		}catch(e){
		//SetevenementCl(0);
		}
	},
	setOption :function (options){

		this.options = {

		}.extend(options || {});
	},
	getParam:function(){
		if(this.objParam['DynmapParam']){
			return this.objParam['DynmapParam'];
		}
		
		return this.getParamSpec('dynmapparam');
	},
	getParamSpec:function(parametre){
	
		var parametreU=parametre.toLowerCase();
		var parametreUc=parametre.toLowerCase();
		
		parametreU='data-'+parametreU;

		if(this.objParam[parametreU]!=undefined){
			return this.objParam[parametreU];
		}
		if(this.objParam[parametreUc]!=undefined){
			return this.objParam[parametreUc];
		}
		if(this.objParam['data-'+parametre]){
			return this.objParam[parametre];
		}
		if(this[parametre]){
			return this[parametre];
		}
		return null;
		
	}
}
var DynContexteDefault=Class.create();
DynContexteDefault.prototype={

	initialize:function(){

	}
}

var ActionWatcher = Class.create();

//portotype action
ActionWatcher.prototype = {

	initialize: function(nom,parameters) {
		this.nom= nom;
		
		var ObjActTp=new Object();
		for(var h in parameters){
			hMin=h.toLowerCase();
			ObjActTp[hMin]=parameters[h];
			ObjActTp[h]=parameters[h];
		}
		this.parameters= parameters;
	
	},
	onAction:function(){
		Dynmap.Action.execute(this.nom,this.parameters,mainMap);
	}
}

var DynActionDefault=Class.create();
var DynObjImg=Class.create()
DynObjImg.prototype = {
	initialize: function(objImg){
		this.initGraph(objImg)
		this.objImg=objImg;
		if(this.objImg.getAttribute('DynmapContexte')=='Default'){
			this.objImg.src=	this.objImg.getAttribute('srcon');
		}
	},
	beginContext:function(name,param){
		
		if(!param){
		
			param={'dynmapparam':''};
		}else{
		
		}
	
	
		if((param.dynmapparam==this.objImg.getAttribute('DynmapParam'))
	||		(!this.objImg.getAttribute('DynmapParam')&& param.dynmapparam=='')
		){
		
			this.objImg.src=	this.objImg.getAttribute('srcon');
		}
		if((param['data-dynmapparam']==this.objImg.getAttribute('data-dynmapparam'))
				||		(!this.objImg.getAttribute('data-dynmapparam')&& param['data-dynmapparam']=='')
					){
		
						this.objImg.src=	this.objImg.getAttribute('srcon');
					}
	},
	endContext:function(name){

		this.objImg.src=	this.objImg.srcoff;

	},
	initGraph:function(objElem,act){
		if(!objElem.getAttribute('srcon')){
			objElem.setAttribute('srcon',modeleNom+'/'+act+'_on.gif');
		}
		if(!objElem.getAttribute('srcover')){
			objElem.setAttribute('srcover','null');
		}
		objElem.style.cursor="pointer";
		if(!objElem.getAttribute('srcoff')){
			objElem.srcoff=objElem.src;
		}else{
			objElem.srcoff = objElem.getAttribute('srcoff');
		}
	},
	mouseOv:function(evt){

		var objElem =Event.element(evt);
		if(objElem.getAttribute('srcover')!='null'){
			objElem.srctemp=	objElem.src;

			objElem.src=	objElem.getAttribute('srcover');
		}


	},
	mouseOut:function(evt){

		var objElem =Event.element(evt);
		if(objElem.getAttribute('srcover')!='null'){
			objElem.src=	objElem.srctemp;
		}
	}


}
var DynObjAcontext=Class.create()
DynObjAcontext.prototype = {
	initialize: function(objImg){
		this.initGraph(objImg)
		this.objImg=objImg;
		if(this.objImg.getAttribute('DynmapContexte')=='Default'){
			Element.addClassName(this.objImg,this.objImg.getAttribute('classon'));
			
		}
	},
	beginContext:function(name,param){
		if(!param){
			param={'dynmapparam':''};
		}else{
			
		}
		Element.removeClassName(this.objImg,this.objImg.getAttribute('classon'));
		if(param.dynmapparam==this.objImg.getAttribute('DynmapParam')){
			Element.addClassName(this.objImg,this.objImg.getAttribute('classon'));
		}
	},
	endContext:function(name){
		Element.addClassName(this.objImg,this.objImg.getAttribute('classoff'));
		Element.removeClassName(this.objImg,this.objImg.getAttribute('classon'));
	

	},
	initGraph:function(objElem,act){
		if(!objElem.getAttribute('classon')){
			objElem.setAttribute('classon',objElem.className+'_on');
		}
		if(!objElem.getAttribute('classoff')){
			objElem.classoff=objElem.className;
		}else{
			objElem.className = objElem.getAttribute('classoff');
		}
	},
	mouseOv:function(evt){

		


	},
	mouseOut:function(evt){

		
	}


}
var DynLinkerImages=Class.create();
DynLinkerImages.prototype = {

	initialize: function(attributeType,DynManag) {
		this.attributeType=attributeType;
		this.tabObjImg=new Array();
		objsContexte=getElementsByAttribute(document,'*',attributeType);
		var max=objsContexte.length;
		this.DynManag=DynManag;
		for(var j=0; j<max; j++){

			var nAt=objsContexte[j].getAttribute(attributeType);
			if(objsContexte[j].nodeName =='IMG'){
				this.tabObjImg[j]=new DynObjImg(objsContexte[j]);
			}	
			if(objsContexte[j].nodeName =='A'){
				this.tabObjImg[j]=new DynObjAcontext(objsContexte[j]);
			}	
			Dynmap.Event.observe(objsContexte[j],'mousedown',Dynmap.Function.bindAsEventListener(this.chgContexte,this) ); 

			objsContexte[j].onmouseover= this.tabObjImg[j].mouseOv.bindAsEventListener(this.tabObjImg[j]);

			objsContexte[j].onmouseout= this.tabObjImg[j].mouseOut.bindAsEventListener(this.tabObjImg[j]);

			this.DynManag.addWatcher(nAt,this);
			this.DynManag.addWatcher(nAt,this.tabObjImg[j]);
			var nat2=objsContexte[j].getAttribute('listener');
			if(nat2){
				
				var params=nat2.split('|');
				GestEvtDyn.ajouteEcouteur(params[0],this.bindParam,objsContexte[j]);
			}
		}
		this.currentContexte='Default';


	},
	bindParam:function(p1,p2){
		
		var nat2=this.getAttribute('listener');

		if(nat2){
			var params=nat2.split('|');
			if(params[2]=='undefined'||params[2]==p2){
				this.setAttribute(params[1],p1);
			
			}
		}
	
	},
	chgContexte:function(evt){
		var key='';
		var initiator =  Event.element(evt);
	
		while (initiator.parentNode && (!initiator.getAttribute(this.attributeType)))
				initiator = initiator.parentNode;
		
		var element= initiator.getAttribute(this.attributeType);
		var newCtxte=initiator.getAttribute(this.attributeType);

		var objParam= new Object();
		for (var h=0;h<initiator.attributes.length  ; h++)
		{
				var attribute=initiator.attributes.item(h);
			
				key =attribute.name.toLowerCase();
				objParam[key]=attribute.value;

		}
		this.chgContextApi(newCtxte,objParam);
	},
	chgContextApi:function(newCtxte,param){
	
		if(typeof(param)=='undefined'){
			param={};
		}
		if(newCtxte==this.currentContexte&&(param.dynmapparam==this.DynManag.curParams.dynmapparam)){
		
			this.DynManag.endContext(newCtxte);
	
			this.DynManag.beginContext('Default',{});
		}else{
		
			this.DynManag.endCurrentContext();
			
			this.DynManag.beginContext(newCtxte,param);
	
		}

	},
	beginContext:function(name){
		this.currentContexte=name;
	},
	endContext:function(name){
		this.currentContexte='';
	}

}
var DynActionManagement=Class.create();
DynActionManagement.prototype = {

	initialize: function(attributeType) {
		this.tabAction=new Array();
		this.attributeType=attributeType;
		objsContexte=getElementsByAttribute(document,'*',attributeType);
		var max=objsContexte.length;
		//return;
		var j=0;
		this.tabObjImg=new Array();
		for(j=0; j<max; j++){
			this.tabObjImg[j]=new DynObjImg(objsContexte[j],objsContexte[j].getAttribute(attributeType));

			objsContexte[j].onclick = this.lanceAction.bindAsEventListener(this);
			objsContexte[j].onmouseover= this.tabObjImg[j].mouseOv.bindAsEventListener(this.tabObjImg[j]);
			objsContexte[j].onmouseout= this.tabObjImg[j].mouseOut.bindAsEventListener(this.tabObjImg[j]);
			var nat2=objsContexte[j].getAttribute('listener');
			if(nat2){
				
				var params=nat2.split('|');
				GestEvtDyn.ajouteEcouteur(params[0],this.bindParam,objsContexte[j]);
			}
		}
	},
	bindParam:function(p1,p2){
		
		var nat2=this.getAttribute('listener');

		if(nat2){
			var params=nat2.split('|');
			if(params[2]=='undefined'||params[2]==p2){
				this.setAttribute(params[1],p1);
			}
		}
	
	},
	lanceAction:function(evt){
		var initiator= Dynmap.Event.element(evt);
		while (initiator.parentNode && (!initiator.getAttribute(this.attributeType)))
				initiator = initiator.parentNode;
		var nomAction=initiator.getAttribute(this.attributeType);
		var objParam= new Object();
		for (var h=0;h<initiator.attributes.length  ; h++)
		{
				var attribute=initiator.attributes.item(h);
				objParam[attribute.name]=attribute.value;

		}
		objParam['eventLauncher']=initiator;
		if(window['mainMap']||mainMap){
			Dynmap.Action.execute(nomAction,objParam,mainMap); 
		}else{
			Dynmap.Action.execute(nomAction,objParam,null); 
		}
		Dynmap.Event.stop(evt);
		
	}
}

