 
//parametrages
var timeToDel="500";
var timeToLanceRequete=500;
var timerAjaxAC=-1; 
var testds=0;

var AideInput=Class.create();
/*nominput:l'element qui sera selectionne, iddata:unique, element renvoye,leindonne:rulvers donnees */
AideInput.prototype= {
	initialize:function(nomInput,IdData,lienDonnees){

		this.nomInput=nomInput;
		this.curentSel=0;this.tailleMax=0;
		this.fixTop=0;  
		this.objInput=$(nomInput); 
		ajaxEngine.registerRequest( 'get'+nomInput, lienDonnees);
		ajaxEngine.registerAjaxObject( IdData,this );
		this.disp="0";
		timerAjaxAC=-1;
		this.idDiv="AideSel"+nomInput; 
		
		this.objInput.onkeyup = this.lanceToComplete.bindAsEventListener(this); 
		this.objInput.onblur = this.enleveDiv.bindAsEventListener(this);
		this.objInput.onkeypress = handleEnter.bindAsEventListener(this.objInput);
	} 
	,
	changeSourceDonnees: function(nomInput,IdData,lienDonnees)
	{
		this.nomInput=nomInput;
		ajaxEngine.registerRequest( 'get'+nomInput, lienDonnees);
		ajaxEngine.registerAjaxObject( IdData,this );
	}
	,
	ajaxUpdate: function(ajaxResponse) {

		//document.body.appendChild(ajaxResponse);
		for(var i=ajaxResponse.childNodes.length-1;i>-1;i--){
			if(ajaxResponse.childNodes[i].nodeType==3){
				ajaxResponse.removeChild(ajaxResponse.childNodes[i]);
			}
		}
		var Myrech=this.objInput.value;
			if(Myrech.length>0){
				this.setListe(ajaxResponse);
			}
		
	},
	gereEnter:function(){
		if(this.disp=="1"){
		var MyChild=this.objDiv.childNodes[this.curentSel];
		this.setNewVal(MyChild.idref,MyChild.libelle);
		}
	},
	setListe :function(Response){
		this.curentSel=0;
		var taille=Response.childNodes.length;
		this.tailleMax=taille;
		if(taille>0){
			if(this.disp=="0"){
					this.displayDiv();
				}
			this.objDiv.innerHTML="";
			for(var i=0;i<taille;i++){
				var d=document.createElement('div');
				d.id=this.idDiv+Response.childNodes[i].getAttribute('IDL');
				d.idn=i;
				d.idref=Response.childNodes[i].getAttribute('IDL');
				if(i==0){d.className="actb_active";}else{d.className="actb_Noactive"; }
				d.libelle=Response.childNodes[i].getAttribute('LIBELLE');
				d.innerHTML=this.valeurRecherche(Response.childNodes[i].getAttribute('LIBELLE'),Response.childNodes[i].getAttribute('IDL'),Response.childNodes[i].getAttribute('LIBELLE'),Response.childNodes[i].getAttribute('EXTRA'));
				this.objDiv.appendChild(d);
				d.onclick=this.setNewValDiv.bindAsEventListener(this);
				d.onmouseover=this.changeHover.bindAsEventListener(this);
				var s=$('rec'+Response.childNodes[i].getAttribute('IDL'));
				try{
				s.idref=Response.childNodes[i].getAttribute('IDL');
				s.idn=i;
				s.libelle=Response.childNodes[i].getAttribute('LIBELLE');				
				s.onclick=this.setNewValDiv.bindAsEventListener(this);
				}catch(e){}

				try{
					if(Response.childNodes[i].getAttribute('BOLD')=="true")
					{
						d.className+=" actb_bold";
					}
				}catch(e){}
			}
			fSwapSelect(this.idDiv);
		}else if(taille==0){
			//this.objDiv.innerHTML="";
			this.enleveDiv();
		}
		try{Element.show("aidesaisie_img_"+this.nomInput);}catch(e){}
		try{Element.hide("aidesaisiewait_img_"+this.nomInput);}catch(e){}
	},

	setNewValDiv:function (evt){
		
		var DivSel=Event.element(evt);
		if(DivSel.idref!=''){
		this.setNewVal(DivSel.idref,DivSel.libelle);
		}
	},
	changeHover:function(evt){
		var DivSel=Event.element(evt);var newId=DivSel.idn;
		//on enleve l'ancien
		extra = "";
		if(this.objDiv.childNodes[this.curentSel].className.indexOf("bold")>-1)extra = " actb_bold";
		this.objDiv.childNodes[this.curentSel].className="actb_Noactive"+extra;
		extra = "";
		if(this.objDiv.childNodes[newId].className.indexOf("bold")>-1)extra = " actb_bold";
		this.objDiv.childNodes[newId].className="actb_active"+extra;
		this.curentSel=newId;
	},
	changeUp:function(){		
		if((this.curentSel+1)<this.tailleMax){
						
			extra = "";
			if(this.objDiv.childNodes[this.curentSel].className.indexOf("bold")>-1)extra = " actb_bold";
			this.objDiv.childNodes[this.curentSel].className="actb_Noactive"+extra;
			this.curentSel++;
			extra = "";
			if(this.objDiv.childNodes[this.curentSel].className.indexOf("bold")>-1)extra = " actb_bold";
			this.objDiv.childNodes[this.curentSel].className="actb_active"+extra;
		}

	},
	changeDown:function(){
		if((this.curentSel-1)>-1){
			extra = "";
			if(this.objDiv.childNodes[this.curentSel].className.indexOf("bold")>-1)extra = " actb_bold";

			this.objDiv.childNodes[this.curentSel].className="actb_Noactive"+extra;
			this.curentSel--;
			
			extra = "";
			if(this.objDiv.childNodes[this.curentSel].className.indexOf("bold")>-1)extra = " actb_bold";
			this.objDiv.childNodes[this.curentSel].className="actb_active"+extra;
		}

	},
	setNewVal:function(idr,libel){
		this.objInput.value=libel;
		 
		if(document.getElementById(this.objInput.name+"_hidden_values"))
		{ 
			document.getElementById(this.objInput.name+"_hidden_values").value=idr;	
		}
	 
		try{
			eval(this.nomInput+"_onSetVal(\""+idr+"\")");
		}catch(e)
		{
			
		}

		this.enleveDiv();
	},
	valeurRecherche:function (n,valob,libel,extra){
		var t = escape(this.objInput.value); 
		
		var tobuild = '';
		 
		var re = new RegExp(t, "i");
		//n=escape(n);

		var p = n.search(re);
		
		for (i=0;i<p;i++){
			tobuild += n.substr(i,1);
		}
		tobuild += '<span  class="actb_regex_match" id="rec'+valob+'" >';
		
		for (i=p;i<t.length+p;i++){
			for (i=p;i<t.length+p;i++){ 
				if(i>-1)tobuild += n.substr(i,1);
			}
		}
		tobuild += "</span>";
		for (i=t.length+p;i<n.length;i++){
			tobuild += n.substr(i,1);
		}
		  
 
		if(extra)
		{
			arrTmp = extra.split("|");
			
			extrasubstring = "";
			
			for(i=0;i<arrTmp.length;i++)
			{
				extrasubstring += " , "+arrTmp[i].substring(0,50); 
				if(arrTmp[i].length>40)extrasubstring+=" ... ";
			}
			
			if(extrasubstring.length>0)
			{
				tobuild += extrasubstring;
			}
		}
	 
		tobuild += "";

		return unescape(tobuild);


	}
	,
	lanceToComplete:function(evt){
 	 
 		if(this.objInput.value!="")
		{
			try{Element.show("aidesaisiewait_img_"+this.nomInput);}catch(e){}
			try{Element.hide("aidesaisie_img_"+this.nomInput);}catch(e){}
		

			if (evt.keyCode == 40 ){
				this.changeUp();
			}else if(evt.keyCode == 38){
				this.changeDown();

			}else if(evt.keyCode != 13){
				
				var Myrech=this.objInput.value;
				if(Myrech.length>0){ 
					if(timerAjaxAC!=-1){ 
						clearTimeout(timerAjaxAC);
					}
					timerAjaxAC=setTimeout ("lanceRequeteAjaxAC('"+this.nomInput+"','"+Myrech+"')",timeToLanceRequete); 
					testds++;
				}else{
					this.enleveDiv();
				}
			}else{
				this.gereEnter();
			}
		}
	},

	displayDiv:function(){
		try{
			fSwapSelect(this.idDiv);
			document.body.removeChild(document.getElementById(this.idDiv));
		}catch(E){

		}
		try{Element.show("aidesaisie_img_"+this.nomInput);}catch(e){}
		try{Element.hide("aidesaisiewait_img_"+this.nomInput);}catch(e){}
		this.disp="1";
		var TabPos=Position.cumulativeOffset(this.objInput);
		var a=document.createElement('div');
		a.id=this.idDiv;
		a.innerHTML="";
		a.className="divaide";
		a.style.position = "absolute";
		a.style.top = TabPos[1]+(this.objInput.offsetHeight) + this.fixTop + "px";	   
		a.style.left = TabPos[0] + "px";
		//a.style.width = this.objInput.offsetWidth  + "px";
		document.body.appendChild(a);
		this.objDiv=$(this.idDiv);
			

	},
	enleveDiv:function(){
			
			try{Element.hide("aidesaisiewait_img_"+this.nomInput);}catch(e){}
			try{Element.show("aidesaisie_img_"+this.nomInput);}catch(e){}
			autoId=setTimeout ("removeDivAide('"+this.idDiv+"')",250);
			//document.body.removeChild(this.objDiv);

		this.disp="0";
	}


}
function lanceRequeteAjaxAC(nomInput,Myrech){
 
	ajaxEngine.sendRequest( 'get'+nomInput,
				"args=" + Myrech); 
}
function removeDivAide(txtdiv){
	try{
		document.getElementById(txtdiv).innerHTML="";

		fSwapSelect(txtdiv);

		document.body.removeChild(document.getElementById(txtdiv));
		
		
	}catch(e){}

}
function handleEnter (event) {
 
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {

			this.focus();
			return false;
		}
		else
		return true;
	}

fDomOffset = function( oObj, sProp )
{
 var iVal = 0;
 while (oObj && oObj.tagName != 'BODY') {
  eval('iVal += oObj.' + sProp + ';');
  oObj = oObj.offsetParent;
 }
 return iVal;
}
fSwapSelect = function( oDiv )
{ 
	try{
 var i = 0, oSlt = null, iST = 0, iSL = 0, iSW = 0, iSH = 0, isLeft = false, isTop = false, sVis = '';
 var oObj = document.getElementById(oDiv);

 var iOT  = fDomOffset(oObj, 'offsetTop');
 var iOL  = fDomOffset(oObj, 'offsetLeft');
 var iOW  = oObj.offsetWidth;
 var iOH  = oObj.offsetHeight-20;
 var oSelects = document.getElementsByTagName('SELECT');
 if (oSelects.length > 0) {
	 
  for (i = 0; i < oSelects.length; i++) {
   isLeft = false; isTop = false; oSlt = oSelects[i];
   iST = fDomOffset(oSlt, 'offsetTop'); iSL = fDomOffset(oSlt, 'offsetLeft');
   iSW = oSlt.offsetWidth; iSH = oSlt.offsetHeight;
   // On verifie le Magical Square
   if ((iOL > (iSL - iOW)) && (iOL < (iSL + iSW))) {isLeft = true;}
   if ((iOT > (iST - iOH)) && (iOT < (iST + iSH))) {isTop = true;}
   if (isLeft && isTop) {   
	sVis = (oObj.style.visibility == 'hidden') ? 'visible' : 'hidden';
	 
    if (oSlt.style.visibility != sVis) {oSlt.style.visibility = sVis;}
	 
   } else {
    if (oSlt.style.visibility != 'visible') {oSlt.style.visibility = 'visible';}
   }
  }
 }}catch(e){}
}

//Fonction biblio
function aidesaisiebiblio(modele,champ)
{	
	var w = 200;
	var h = 200;

	window.open('/index.php?cont=aidesaisie&event=biblio&mod='+modele+'&champ='+champ);
}

