Autocompleter.DWR = Class.create();
Autocompleter.DWR.prototype = Object.extend(new Autocompleter.Base(), {
    initialize: function(element, update, populator, options) {
        this.baseInitialize(element, update, options);
        this.options.array = new Array(0);
        this.populator = populator;
        if (this.options.afterUpdateElement) {
             this.afterUpdateCallback = this.options.afterUpdateElement;
             this.options.afterUpdateElement = this.afterUpdateElement.bind(this);
        }
    },

    // called by the autocompleter on an event.
    getUpdatedChoices: function() {
        var entry = this.getToken();
        if (entry.length >= this.options.partialChars) {
            this.populator(this, this.getToken()); // this is the populator specified in the constructor.
        }
        else {
           this.hide();
        }
    },

    afterUpdateElement: function(element,selectedElement) {
        this.afterUpdateCallback(element, selectedElement,this.options.array[this.index]);
    },

    // should be called by the populator (specified in the constructor)
    setChoices: function(array) {
        this.options.array = array;
        this.updateChoices(this.options.selector(this));
    },

    setOptions: function(options) {
        this.options = Object.extend({
            choices: 22,
            partialSearch: true,
            partialChars: 2,
            ignoreCase: true,
            fullSearch: true,
            selector: function(instance) {
                var ret       = []; // Beginning matches
                var partial   = []; // Inside matches
                var entry     = instance.getToken();
                var count     = 0;
                var valueSelector = instance.options.valueSelector;
				
				var isTheFirst = false;
                for (var i = 0; i < instance.options.array.length && ret.length < instance.options.choices ; i++) {
                	var elem = valueSelector(instance.options.array[i]);
                    
                    var elemToDisplay;// = elem; // variable pour l'affichage.
                    // Sa c'est moi qui l'ai fait
                    if (elem.isPoiSet) {
                        elemToDisplay = elem.poiTO.title;
                        if (isTheFirst) {
                            ret.push("<li style='width:200px'><table class=\"firstTableAjaxList\"><tr><td class=\"firstTdAjaxList\"><strong>" + elemToDisplay.substr(0, entry.length) + "</strong>" +
                            elemToDisplay.substr(entry.length) +
                            "</td><td class=\"secondTdAjaxList\"><font class=\"typeOfCompletionLabel\">POINT D'INTERET</font></td></tr></table></li>");
                            isTheFirst = false;
                        } else {
                            ret.push("<li style='width:200px'><table class=\"tableAjaxList\"><tr><td class=\"firstTdAjaxList\"><strong>" + elemToDisplay.substr(0, entry.length) + "</strong>" +
                            elemToDisplay.substr(entry.length) +
                            "</td></tr></table></li>");
                        }
                    } else if (elem.isCitySet) {
                        elemToDisplay = elem.cityTO.nameToDisplay;
                        if (isTheFirst) {
                            ret.push("<li style='width:200px'><table class=\"firstTableAjaxList\"><tr><td class=\"firstTdAjaxList\"><strong>" + elemToDisplay.substr(0, entry.length) + "</strong>" +
                            elemToDisplay.substr(entry.length) +
                            "</td><td class=\"secondTdAjaxList\"><font class=\"typeOfCompletionLabel\">VILLES</font></td></tr></table></li>");
                            isTheFirst = false;
                        } else {
                            ret.push("<li style='width:200px'><table class=\"tableAjaxList\"><tr><td class=\"firstTdAjaxList\"><strong>" + elemToDisplay.substr(0, entry.length) + "</strong>" +
                            elemToDisplay.substr(entry.length) +
                            "</td></tr></table></li>");
                        }

                    } else if (elem.isHotelDesc) {
                    	if (elem.generalDescription != null && elem.generalDescription != ""){
                    		ret.push("<p>" + elem.generalDescription + "</p>");
                    	}
	                    if (elem.inclusiveDescription != null && elem.inclusiveDescription != ""){
	                    	ret.push("<h3>Inclus</h3><p>" + elem.inclusiveDescription + "</p>");
	                    }
	                    if (elem.locationDescription != null && elem.locationDescription != ""){
	                    	ret.push("<h3>Situation</h3><p>" + elem.locationDescription + "</p>");
	                    }
	                    if (elem.locationDesc != null && elem.locationDesc != ""){
	                    	ret.push("<h3>Situation</h3><p>" + elem.locationDesc + "</p>");
	                    }
	                    if (elem.exteriorDescription != null && elem.exteriorDescription != ""){
	                    	ret.push("<h3>Façade extérieure</h3><p>" + elem.exteriorDescription + "</p>");
	                    }
	                    if (elem.lobbyDescription != null && elem.lobbyDescription != ""){
	                    	ret.push("<h3>Hall d'accueil</h3><p>" + elem.lobbyDescription + "</p>");
	                    }
	                    if (elem.roomsDescription != null && elem.roomsDescription != ""){
                    	ret.push("<h3>Chambres</h3><p>" + elem.roomsDescription + "</p>");
	                    }
	                    if (elem.restaurantDescription != null && elem.restaurantDescription != ""){
                    	ret.push("<h3>Restaurant</h3><p>" + elem.restaurantDescription + "</p>");
	                    }
	                    if (elem.copyright != null && elem.copyright != ""){
	                    	ret.push("<p>" + elem.copyright + "</p>");
	                    }
	                    if (elem.pleasenoteDescription != null && elem.pleasenoteDescription != ""){
	                    	ret.push("<h3>Veuillez Noter</h3><p>" + elem.pleasenoteDescription + "</p>");
	                    }
	                    
	                    if(instance.options.addressDivId){
	                    	var addrDiv = document.getElementById(instance.options.addressDivId);
	                    	var emptyAddr = document.getElementById("empty_"+instance.options.addressDivId);
	                    	emptyAddr.style.display = 'none';
	                    	addrDiv.innerHTML = "Adresse : " + elem.address;
	                    }
	                    
                        isTheFirst = false;
                        //instance.show();
                        instance.hasFocus = true;

                    } else if (elem.isTitle)  {
						isTheFirst = true;
					} else {
						var elemToDisplay = elem;
                        //on ignore les accents 
                        entry = no_accent(entry);
                        elem = no_accent(elem);
                        
                        var foundPos = instance.options.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase()) : elem.indexOf(entry);
                        
                        while (foundPos != -1) {
                            if (foundPos == 0 && elem.length != entry.length) {
                                ret.push("<li style='width:200px'><table><tr><td><strong>" + elemToDisplay.substr(0, entry.length) + "</strong>" +
                                elemToDisplay.substr(entry.length) +
                                "</td></tr></table></li>");
                                break;
                            } else if (entry.length >= instance.options.partialChars && instance.options.partialSearch && foundPos != -1) {
                                if (instance.options.fullSearch ||
                                /\s/.test(elem.substr(foundPos - 1, 1))) {
                                    partial.push("<li style='width:200px'><table><tr><td>" + elemToDisplay.substr(0, foundPos) + "<strong>" +
                                    elemToDisplay.substr(foundPos, entry.length) +
                                    "</strong>" +
                                    elemToDisplay.substr(foundPos + entry.length) +
                                    "</td></tr></table></li>");
                                    break;
                                } else {
                                    alert("on l'ajoute pas");
                                }
                            } else {
                                alert("on l'ajoute pas non plus");
                            }
                            foundPos = instance.options.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : elem.indexOf(entry, foundPos + 1);
                        }
                    }
                }
				
                if (partial.length)
                    ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
                    return "<ul>" + ret.join('') + "</ul>";
            },

            valueSelector: function(object){ return object; }
        }, options || {});
    }

});

