/**
 * Création des AjaxTextInput
 * Requis : JQuery
 */

/* Mettre en forme les AjaxTextInput lorsque tous les éléments de la fenêtre sont chargés */
$(window).load( function(){
	
	/* Begin : Load */
	$(".AjaxTextInput").each(function(obj){ /* pour chaque élément... */
		// name : en-tête de nom
		this.name = $(this).attr("name");
		
		// sid : espace de suivi (gestion_suivi) (à effacer)
		this.gsid = $(this).attr("sid");
		$(this).removeAttr("sid");
		
		// oid : id de l'objet (unique) (à effacer)
		this.objid = $(this).attr("oid");
		$(this).removeAttr("oid");
		
		// link : fonction d'appel (à effacer)
		this.cFunction = eval($(this).attr("link"));
		$(this).removeAttr("link");
		
		// id : name+oid (à créer)
		this.id = this.name+this.objid;
		$(this).attr("id",this.id);
		$(this).attr("name",this.id);
		
		// Valeur de l'objet
		this.valeur = $(this).val();
		
		// Méthode de Sélection
		this.beginSelect = function(){
			$(this).removeClass('disabledAjaxTextInput');
			$(this).addClass('enabledAjaxTextInput');
			$(this).select();
		}
		
		// Méthode de Sélection
		this.endSelect = function(){
			$(this).removeClass('enabledAjaxTextInput');
			$(this).addClass('disabledAjaxTextInput');
		}
		
		// Méthode d'envoi
		this.actionChanger = function(){
			var valTmp = $(this).val();
			if( valTmp!=this.valeur ){
				this.valeur = valTmp;
				this.cFunction(this.id, this.gsid, this.objid, valTmp);
			}
		}
		
		// init du style
		$(this).addClass('disabledAjaxTextInput');
		
		// Actions sur DblClick (double-clic)
		$(this).dblclick(function(){ this.beginSelect(); });
		
		// Actions sur Blur (perte de focus)
		$(this).blur(function(){
			this.endSelect();
			this.actionChanger();
		});
		
		// Actions sur keyPress (pour appui de la touche ENTREE)
		$(this).keypress(function(e){
			if (e.which == 13){
				this.endSelect();
				this.actionChanger();
			}
		});
	});
	/* End : Load */
});
