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){
		this.curContexte=new ContexteWatcher(contexte,params);
		this.curContexteName=contexte;
		this.curParams=params;
		this._listenersNotify(contexte,'BEGIN');

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

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

var ContexteWatcher = Class.create();

//prototype contexte
ContexteWatcher.prototype = {

	initialize: function(nom,objContexte) {
		this.nom=nom;


		var tp =eval('new DynContexte'+nom+'();');
		this.setParams(objContexte);
		Object.extend(this, tp || {});



	},
	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(){
		var tp= this.objParam.DynmapParam;
		if(!tp){
			tp=	this.objParam.dynmapParam;
		}
		if(!tp){
			tp=	this.objParam.dynmapparam;
		}
		return tp;
	},
	getParamSpec:function(parametre){
		tp= this.objParam[parametre];
		return tp;
	}
}
var DynContexteDefault=Class.create();
DynContexteDefault.prototype={

	initialize:function(){

	}
}
var ActionWatcher = Class.create();

//portotype action
ActionWatcher.prototype = {

	initialize: function(nom,objAction) {

		var tp =eval('new DynAction'+nom+'();');
		this.setParams(objAction);
		Object.extend(this, tp || {});
	},
	setParams: function(options) {

		this.objParam = {
			DynmapParam:       ''
		}.extend(options || {});

	},
	getParamSpec:function(parametre){
		var tp= this.objParam[parametre];
		return tp;
	},
	getParam:function(){
	  var tp= this.getParamSpec('DynmapParam');
		if(!tp){
			tp=	this.objParam.dynmapParam;
		}
		if(!tp){
			tp=	this.objParam.dynmapparam;
		}
		return tp;
	}
}
var DynActionDefault=Class.create();
var DynObjImg=Class.create()
DynObjImg.prototype = {
	initialize: function(objImg){
		this.initGraph(objImg)
		this.objImg=objImg;
	},
	beginContext:function(name,param){

		if(param==this.objImg){
			this.objImg.src=	this.objImg.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="hand";
		objElem.setAttribute('srcoff',objElem.src);
	},
	mouseOv:function(evt){

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

			objElem.src=	objElem.srcover;
		}


	},
	mouseOut:function(evt){

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


}
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);
			this.tabObjImg[j]=new DynObjImg(objsContexte[j]);

			objsContexte[j].onmousedown= this.chgContexte.bindAsEventListener(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){
		alert(p1);
		alert(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 newCtxte=Event.element(evt).getAttribute('DynmapContexte');
		this.chgContextApi(newCtxte,Event.element(evt));
	},
	chgContextApi:function(newCtxte,param){
		if(newCtxte==this.currentContexte&&(param==this.DynManag.curParams)){
			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();
	//	alert(max);
		for(j=0; j<max; j++){
			this.tabObjImg[j]=new DynObjImg(objsContexte[j],objsContexte[j].getAttribute('DynmapAction'));

			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){
		alert(p1);
		alert(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 nomAction=Event.element(evt).getAttribute('DynmapAction');
		var ActionWatch=new ActionWatcher(nomAction,Event.element(evt));
		ActionWatch.onAction();
	}
}
