var arrayListes = Array();
function getObjetListe(idInput)
{
	return arrayListes[idInput];	
}

var arrayChar = new Array("\\", ".", "$", "[", "]", "(", ")", "{", "}", "^", "?", "*", "+", "-");
function getRegExp(strRegExp)
{
	for (var i = 0; i < arrayChar.length; ++i)
		strRegExp = strRegExp.replace(new RegExp("\\" + arrayChar[i]), "\\" + arrayChar[i]);
	return new RegExp(".*" + strRegExp + ".*", "gi");
}

function ListeRecherche(idInput, largeur)
{
	// Configuration
	this.borderWidth = 1;
	
	// Varibles
	arrayListes[idInput] = this;
	this.initialise = false;
	this.mouseOver = false;
	this.input = document.getElementById(idInput);
	this.timer = null;
	
	// Div principale
	this.div = document.createElement("div");
	this.div.onmouseover = function() { getObjetListe(idInput).mouseOver = true; }
	this.div.onmouseout = function() { getObjetListe(idInput).mouseOver = false; }
	this.div.id = "div_" + idInput;
	this.div.onclick = function() { getObjetListe(idInput).masquer(true); }
	this.div.className = "div_recherche";
	this.div.style.borderWidth = this.borderWidth;
	if (largeur)
		this.div.style.width = (largeur - this.borderWidth * 2) + "px";
	
	// Textes de la div principale
	this.span_liste_vide = document.createElement("span");
	this.span_liste_vide.className = "liste_vide";
	this.span_liste_vide.innerHTML = "Liste vide";
	this.div.appendChild(this.span_liste_vide);
	this.span_aucun_resultat = document.createElement("span");
	this.span_aucun_resultat.className = "aucun_resultat";
	this.span_aucun_resultat.style.display = "none";
	this.span_aucun_resultat.innerHTML = "Aucun résultat";
	this.div.appendChild(this.span_aucun_resultat);
	
	this.input.onfocus = function() { getObjetListe(idInput).afficher(); }
	
	this.initialiser = function()
	{
		this.masquer();
		var objbody = document.getElementsByTagName("body").item(0);
		objbody.appendChild(this.div);
		this.input.onblur = function() { getObjetListe(idInput).masquer(); }
		this.input.onkeyup = function() { getObjetListe(idInput).rechercherElements(); }
		return true;
	}
	
	this.ajouterElement = function(libelle)
	{
		this.span_liste_vide.style.display = "none";
		var div_a = document.createElement("div");
		div_a.className = "div_element";
		var a = document.createElement("a");
		a.innerHTML = libelle;
		a.href = "#";
		a.onclick = function() { return getObjetListe(idInput).remplirInput(libelle); }
		div_a.appendChild(a);
		this.div.appendChild(div_a);
		return true;
	}
	
	this.afficher = function()
	{
		if (!this.initialise)
			this.initialise = this.initialiser();
		this.rechercherElements();
		position = this.getInputPosition();
		this.div.style.top = position[0] + 20 + "px";
		this.div.style.left = position[1] + "px";
		if (navigator.appName == "Microsoft Internet Explorer"
			&& !navigator.appVersion.match(new RegExp("7\.0.*", "gi")))
			return false;
		this.div.style.display = "block";
		return true;
	}
	
	this.masquer = function(force)
	{
		if (!this.mouseOver || force)
			this.div.style.display = "none";
		return true;
	}
	
	this.rechercherElements = function()
	{
		if (this.timer)
			window.clearTimeout(this.timer);
		this.timer = window.setTimeout("getObjetListe(\"" + this.input.id + "\").recherche()", 250);
		return true;
	}
	
	this.recherche = function()
	{
		divs = this.div.getElementsByTagName("div");
		nbDisplay = 0;
		reg = getRegExp(this.input.value);
		for(var i = 0; i < divs.length; ++i)
		{
			texte_a = divs[i].getElementsByTagName("a").item(0).innerHTML;
			if (texte_a.match(reg))
			{
				divs[i].style.display = "block";
				++nbDisplay;
			}
			else
				divs[i].style.display = "none";
		}
		this.span_aucun_resultat.style.display = divs.length && nbDisplay ? "none" : "block";
		return true;	
	}
	
	this.remplirInput = function(texte)
	{
		this.input.value = texte;
		this.recherche();
		this.mouseOver = false;
		return false;
	}
	
	this.getInputPosition = function()
	{
		var x = y = 0;
		var obj = this.input;
		for(; obj; obj = obj.offsetParent)
		{
			x += obj.offsetTop;
			y += obj.offsetLeft;
		}
		return new Array(x, y);
	}
}