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 = [];
        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() {
        this.populator(this, this.getToken()); // this is the populator specified in the constructor.
    },

    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: 10,
            partialSearch: true,
            partialChars: 2,
            ignoreCase: true,
            fullSearch: false,
            baseUrl: "http://www.altomfotball.no/element.do?", //Should override this
            imageUrl: "http://www.tv2sporten.no/jsport/multimedia/", //Should override this
            imageUrlId: "http://www.tv2.no/jsport/bilder/", //Should override this
            missingImageUrl: "http://www.altomfotball.no/graphics/personImageMissing.gif",
            selector: function(instance) {
                var ret       = []; // Beginning matches
                var partial   = []; // Inside matches
                var entry     = instance.getToken();
                var count     = 0;
                var valueSelector = instance.options.valueSelector;
                var currentType = 0;
                
                for (var i = 0; i < instance.options.array.length &&
                        ret.length < instance.options.choices; i++) {


                    var elem = valueSelector(instance.options.array[i]);
                 	var tokens = elem.split( "#" );
                 	
                 	if(tokens.length < 4) { continue; }
                 	
                 	var type = tokens[0];
                 	var name = tokens[1];
                 	var tournamentid = tokens[2];
                 	var seasonid = tokens[3];
                 	var teamid = tokens[4];
                 	var personid = tokens[5];
                 	var imagepath = tokens[6];
                 	var imageid = tokens[7];

//                 	var type = tokens[0];
//                 	var tournamentid = tokens[1];
//                 	var name = tokens[2];
//                 	var seasonid = "";
//                 	var teamid = "";
//                 	var personid = "";
//                 	var imagepath = tokens[3];
//                 	var imageid = tokens[4];


                 	var murl = "";
                 	
                 	if(type != currentType) { //Insert header for type
                 		currentType = type;
                 		ret.push("<li class=\"searchheader\">" + (currentType == 1 ? "Liga:" : (currentType == 2 ? "Lag:" : "Spillere:")) + "</li>"); 
                 	}
                 	
                 	if(type == 1) {
						murl = instance.options.baseUrl + "cmd=tournamentMain&tournamentId=" + tournamentid + "&seasonId=" + seasonid;
                 	} else if(type == 2) {
                 		murl = instance.options.baseUrl + "cmd=team&tournamentId=" + tournamentid + "&seasonId=" + seasonid + "&teamId=" + teamid;
                 	} else if(type == 3) {
                 		murl = instance.options.baseUrl + "cmd=person&tournamentId=" + tournamentid + "&seasonId=" + seasonid + "&teamId=" + teamid + "&personId=" + personid;
                 	}
                 	
					if(imagepath.length > 0) {
						imageurl = instance.options.imageUrl + imagepath;
					} else if(imageid.length > 0){
						imageurl = instance.options.imageUrlId + imageid + (type == 2 ? ".gif" : ".jpg");
					} else {
						//imageurl = instance.options.missingImageUrl;
						imageurl = ''; 
					}
					
					var html = "<li onclick=\"window.location='" + murl + "'\">"
						+ "<div class=\"acleft\">" + name + "</div>"
						+ "<div class=\"acright\">"
						+ (imageurl.length > 0 ? "<img src=\"" + imageurl + "\" border=\"0\" height=\"30\" alt=\"" + name + "\" />" : "")
						+ "</div><div class=\"acclear\">&nbsp;</div>";
                     	+ "</li>";

					ret.push(html);


//                    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><strong>" + elem.substr(0, entry.length) + "</strong>" +
//                                    elem.substr(entry.length) + "</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>" + elem.substr(0, foundPos) + "<strong>" +
//                                        elem.substr(foundPos, entry.length) + "</strong>" +
//                                        elem.substr(foundPos + entry.length) + "</li>");
//                                break;
//                            }
//                        }
//                        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 || {});
    }
});