/* 
 * CoursesOnline.js
 *
 * Some novel functions and additions that extend on script.aculo.us.
 * 
 * (C) Andrew Jessup
 * With a big up to Bob Silva of http://www.railtie.net/ for the autocompleter caching code
 * 
 */

/*
 *	Autocompleter
 */

Ajax.Autocompleter.prototype.getUpdatedChoicesOrig = Ajax.Autocompleter.prototype.getUpdatedChoices

Ajax.Autocompleter.prototype.cache = true

String.prototype.startsWith = function(prefix) {
    return (this.toLowerCase().indexOf(prefix.toLowerCase()) === 0);
}

Ajax.Autocompleter.prototype.getUpdatedChoices = function() {
    if (this.cache && this.getToken().length > 1
        && this.update.firstChild
        && this.update.firstChild.childNodes
        && this.update.firstChild.childNodes.length > 0 )
    {
        for (var i = 0; i < this.entryCount; i++) {
            var entry = this.getEntry(i);
            if (this.options.select) {
                var nodes = document.getElementsByClassName(this.options.select, entry) || [];
                if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select);
            } else {
                value = Element.collectTextNodesIgnoreClass(entry, 'informal');
            }
            if (!value.startsWith(this.getToken())) {
                entry.style.display = 'none';
                if (this.index == entry.autocompleteIndex) {
                    this.index = entry.autocompleteIndex+1;
                }
            }
            else
            {
                entry.style.display = 'block';
                if (this.index > entry.autocompleteIndex) {
                  this.index = entry.autocompleteIndex;
                }
            }
        }
        this.render();
    }
    else
    {
        this.getUpdatedChoicesOrig();
    }
}