/*
	Créer un div d'autocompletion pour les villes et l'attache à un champ
*/

function AutoComplete(oText, dbTable, dbField)
{
	var oDiv = document.createElement("ul");
	oDiv.id = "autocomplete_"+oText;
	oDiv.AutoComplete = this;
	oDiv.className = "autocomplete";
	oDiv.style.visibility = "hidden";
	
	document.body.appendChild(oDiv);
	
	// initialize member variables
	this.oText = $(oText);
	this.oDiv = "autocomplete_"+oText;
	this.dbTable = dbTable;
	this.dbField = dbField;
			
	// attach handlers to the text-box
	$(oText).AutoComplete = this;
	$(oText).onkeyup = AutoComplete.prototype.onTextChange;
	$(oText).onblur = AutoComplete.prototype.onTextBlur;
}

AutoComplete.prototype.onTextBlur = function()
{
	this.AutoComplete.onblur();
}

AutoComplete.prototype.onblur = function()
{
	$(this.oDiv).style.visibility = "hidden";
}

AutoComplete.prototype.onTextChange = function()
{
	this.AutoComplete.onchange();
}

AutoComplete.prototype.onchange = function()
{
	if(this.oText.value.length > 0)
	{
		$(this.oDiv).style.visibility = "visible";
		$(this.oDiv).style.left = this.calculOffset(this.oText, "offsetLeft")+"px";
	    $(this.oDiv).style.top = this.calculOffset(this.oText, "offsetTop")+(this.oText.offsetHeight-1)+"px";
	    //$(this.oDiv).style.minWidth = (this.oText.offsetWidth-2)+"px";
		$(this.oDiv).innerHTML = "Chargement...";
		
		ajaxQuery('interfaces/ajx_autocomplete.php', this.oDiv, 'obj='+this.oText.id+'&text='+this.oText.value);
		
	}
	else
	{
		$(this.oDiv).style.visibility = "hidden";
		$(this.oDiv).innerHTML = "";
	}
}


AutoComplete.prototype.calculOffset = function(input, attr)
{
	var kb = 0;
	while(input)
	{
		kb += input[attr];
		input = input.offsetParent;
	}
	return kb;
}

function autoCompleteResize(div)
{
	if($(div).offsetHeight > 150)
	{
		$(div).style.height = "150px";
	}
}