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



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.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);
	},
	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'));
				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){}
			}
			fSwapSelect(this.idDiv);
		}else if(taille==0){
			//this.objDiv.innerHTML="";
			try{Element.show("aidesaisie_img_"+this.nomInput);}catch(e){}
			try{Element.hide("aidesaisiewait_img_"+this.nomInput);}catch(e){}
			this.enleveDiv();
		}
	},

	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
		this.objDiv.childNodes[this.curentSel].className="actb_Noactive";
		this.objDiv.childNodes[newId].className="actb_active";
		this.curentSel=newId;
	},
	changeUp:function(){
		if((this.curentSel+1)<this.tailleMax){
			this.objDiv.childNodes[this.curentSel].className="actb_Noactive";
			this.curentSel++;
			this.objDiv.childNodes[this.curentSel].className="actb_active";
		}

	},
	changeDown:function(){
		if((this.curentSel-1)>-1){
			this.objDiv.childNodes[this.curentSel].className="actb_Noactive";
			this.curentSel--;
			this.objDiv.childNodes[this.curentSel].className="actb_active";
		}

	},
	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;	
		}

		this.enleveDiv();
	},
	valeurRecherche:function (n,valob,libel){
		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);
		}

		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);







			}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) + "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){
	timerAjaxAC=-1;
	ajaxEngine.sendRequest( 'get'+nomInput,
				"args=" + Myrech); 
}
function removeDivAide(txtdiv){
	try{
		document.getElementById(txtdiv).innerHTML="";

		//fSwapSelect(txtdiv);
		 var oSelects = document.getElementsByTagName('SELECT');
		 if (oSelects.length > 0) {
			 for (i = 0; i < oSelects.length; i++) {
				 oSlt = oSelects[i];
				 if (oSlt.style.visibility != 'visible') {oSlt.style.visibility = 'visible';}
			 }
		 }

		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;
 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,'');
}

