var comboBox = Class.create({
	zero: function() {
		this.div.scrollTop = 0;
		this._selected = null;
		this._selectedIndex = null;
		this.tailleBoite = this.div.getHeight();
		try {
			this.tailleElement = this.div.firstDescendant("ul").firstDescendant("li").getHeight();
			this.nbElements = this.div.firstDescendant("ul").childElements("li").length;
		} catch (e) {
			this.tailleElement = 0;
			this.nbElements = 0;
		}
		this.derniereValeur = this.input.value;
		if(this.nbElements > 0) {
			this.change(null, 0);
		}
		
	},
	
	placer: function() {
		var pos = this.input.cumulativeOffset();
		//var larg = this.input.getWidth();
		var larg = 230;
		this.div.setStyle({top: pos.top+ this.input.offsetHeight+"px", left: pos.left+"px", width: larg+"px"});
	},
	
	over: function(ev) {
		if(Event.element(ev).descendantOf(this.div)) {
			this.change(this._selectedIndex, Event.element(ev).previousSiblings().length, false);
		}
	},
	
	gererFleche: function(ev) {
		if(this.div.style.display == "block") {
			if(ev.keyCode == 40 || ev.keyCode == 39) {
				ev.stop();
				this.bouger(1);
			} else if(ev.keyCode == 38 || ev.keyCode == 37) {
				ev.stop();
				this.bouger(-1);
			} else if(ev.keyCode == 13 || ev.keyCode == 9) {
				ev.stop();
				this.empecher = true;
				if(this._selected != null) {
					this.select();
				}
			}
		}
	},
	
	envoi: function() {
		if(this.derniereValeur != this.input.value) {
			if(typeof this.xhrEnCours == 'object') {
				this.xhrEnCours.abort();
			}
			
			var url = this.monUrl;
			var input = this.input;
			var div = this.div;
			var objet = this;
			
			this.xhrEnCours = new Ajax.Request(url, 
				{
					
					parameters: {i: input.value},
					method: 'get',
					onSuccess: function(retour) {
						if(retour.responseXML.getElementsByTagName("req").item(0).firstChild.data == "Rien") {
							div.style.display="none";
						} else if(retour.responseXML.getElementsByTagName("req").item(0).firstChild.data == input.value) {
							if(retour.responseXML.getElementsByTagName("commun").item(0).firstChild.data == "Rien") {
								div.style.display="none";
							} else {
								div.innerHTML = retour.responseXML.getElementsByTagName("commun").item(0).firstChild.data;
								div.style.display="block";
								objet.zero();
							}
						}
					}
				}
			);
		}
	},
	
	initialize: function(input, url, selectCustom) {
		this.input = $(input);
		
		var vanDiv = $(document.createElement("div"));
		vanDiv.addClassName("combonew");
		$(document.body).insert({bottom: vanDiv});
		this.div = vanDiv;
		
		this.monUrl = url;
		this.selectCustom = selectCustom;
		this.firefox = false;
		this.empecher = false;
		
		this.input.setAttribute("autocomplete", "off");
		this.zero();
		this.placer();
		this.input.observe('keydown', this.gererFleche.bindAsEventListener(this));
		this.input.observe('keyup', this.envoi.bindAsEventListener(this));
		this.input.observe('keypress', this.empeche.bindAsEventListener(this));
		this.div.observe('mouseover', this.over.bindAsEventListener(this));
		this.div.observe('click', this.select.bindAsEventListener(this));
	},
	
	empeche: function(ev) {
		if(this.firefox == true && ev.keyCode == 13 && this.empecher == true) {
			ev.stop();
		} else if(ev.keyCode == 0 && ev.which != 0) {
			this.firefox = true;
		}
		this.empecher = false;
	},
	
	
	
	
	bouger: function(combien) {
		var ancien = this._selectedIndex;
		var nouveau = this._selectedIndex;
		
		if(nouveau == null) {
			nouveau = 0;
		} else {
			nouveau += combien;

			if(nouveau < 0) {
				nouveau = this.nbElements -1;
			} else if(nouveau >= this.nbElements) {
				nouveau = 0;
			}
		}
		this.change(ancien, nouveau, true);
	}, 
	
	change: function(ancien, nouveau, scroll) {
		if(ancien != null) {
			this.div.firstDescendant("ul").childElements("li")[ancien].removeClassName('pris');
		}
		
		this._selectedIndex = nouveau;
		this._selected = this.div.firstDescendant("ul").childElements("li")[nouveau];
		
		this._selected.addClassName('pris');
		
		if(scroll) {		
			// on place la selection au milieu de la boite
			this.div.scrollTop = this.tailleElement * (this._selectedIndex - Math.floor((this.tailleBoite / this.tailleElement) / 2));
		}
	},
	
	select: function() {
		this.input.value = this._selected.firstChild.data;
		this.derniereValeur = this.input.value;
		this.input.focus();
		this.div.style.display = "none";
		if(this.selectCustom != undefined && this.selectCustom != null) {
			this.selectCustom();
		}
	}
});