/**************************

MERCI DE COMMENTER TOUTES CES FONCTIONS, ON N'Y COMPREND RIEN !!!
MERCI DE COMMENTER TOUTES CES FONCTIONS, ON N'Y COMPREND RIEN !!!
MERCI DE COMMENTER TOUTES CES FONCTIONS, ON N'Y COMPREND RIEN !!!
MERCI DE COMMENTER TOUTES CES FONCTIONS, ON N'Y COMPREND RIEN !!!
MERCI DE COMMENTER TOUTES CES FONCTIONS, ON N'Y COMPREND RIEN !!!
MERCI DE COMMENTER TOUTES CES FONCTIONS, ON N'Y COMPREND RIEN !!!
MERCI DE COMMENTER TOUTES CES FONCTIONS, ON N'Y COMPREND RIEN !!!

**************************/


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){
		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(){
		return this.getParamSpec('dynmapparam');
	},
	getParamSpec:function(parametre){
		parametre=parametre.toLowerCase();
		return this.objParam[parametre];
		
	}
}
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+'();');
			var ObjActTp=new Object();
		for(var h in objAction){
			hMin=h.toLowerCase();
			ObjActTp[hMin]=objAction[h];
		}
		this.setParams(ObjActTp);
		Object.extend(this, tp || {});
	},
	setParams: function(options) {

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

	},
	getParamSpec:function(parametre){
		parametre=parametre.toLowerCase();
		var tp= this.objParam[parametre];
		return tp;
	},
	getParam:function(){
		//var tp= this.objParam.DynmapParam;
		return this.getParamSpec('dynmapparam');
	}
}
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.dynmapparam==this.objImg.getAttribute('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";
		objElem.srcoff=objElem.src;
	},
	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 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){
		
		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 newCtxte=Event.element(evt).getAttribute(this.attributeType);

		var objParam= new Object();
		for (var h=0;h<Event.element(evt).attributes.length  ; h++)
		{
				var attribute=Event.element(evt).attributes.item(h);
			//	alert(attribute.name);
				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 nomAction=Event.element(evt).getAttribute(this.attributeType);
		var objParam= new Object();
		for (var h=0;h<Event.element(evt).attributes.length  ; h++)
		{
				var attribute=Event.element(evt).attributes.item(h);
				objParam[attribute.name]=attribute.value;

		}
		var ActionWatch=new ActionWatcher(nomAction,objParam);
	
		ActionWatch.onAction();
	}
}

