// (c) EIKONA AG, it.x informationssysteme gmbh, Alle Rechte vorbehalten.

// Patch beseitigt einen Autocompleter Bug: http://www.pro4j.de/scriptaculous-autocompleter-scroll-bugs
Object.extend(Autocompleter.Base.prototype, {
                baseInitialize : function(element, update, options) {
                this.superBaseInitialize(element, update, options);
                this.ignoreHoverEvent = false;
},
superBaseInitialize :Autocompleter.Base.prototype.baseInitialize,
scrollIntoViewUp : function() {
                var entry = this.getEntry(this.index);
                if (entry.viewportOffset().top < 0) {
                               // only scroll if entry out of bounds
                               entry.scrollIntoView(true);
                               // ignore next mouse event to avoid marking of entry
                               // after scrolling if mouse is over list
                               this.ignoreHoverEvent = true;
                               // make sure following mouse events are not ignored if mouse is
                               // not over list
                               setTimeout(this.resetIgnoreHover, 500);
                }
},
scrollIntoViewDown : function() {
                var entry = this.getEntry(this.index);
//              get viewport height (most browsers, ie + mozilla in quirks mode)
                // (for strict mode use document.viewport.getHeight()
                var windowHeight = window.innerHeight || document.body.clientHeight;
                // get y value of selected entry and add entry height to get lower bounds
                // y value
                var currentHeight = entry.viewportOffset().top + entry.getHeight();
                if (windowHeight - currentHeight < 0) {
                               // only scroll if entry out of bounds
                               this.getEntry(this.index).scrollIntoView(false);
                               // ignore next mouse event to avoid marking of entry
                               // after scrolling if mouse is over list
                               this.ignoreHoverEvent = true;
                               // make sure following mouse events are not ignored if mouse is
                               // not over list
                               setTimeout(this.resetIgnoreHover, 500);
                }
},
markPrevious : function() {
                if (this.index > 0) {
                               this.index--
                               // scroll up if list is stepped up
                               this.scrollIntoViewUp();
                } else {
                               this.index = this.entryCount - 1;
                               // scroll down on jump to last entry
                               this.scrollIntoViewDown();
                }
},
markNext : function() {
                if (this.index < this.entryCount - 1) {
                               this.index++
                               // scroll down if list is stepped down
                               this.scrollIntoViewDown();
                } else {
                               this.index = 0;
                               // scroll up on jump to first entry
                               this.scrollIntoViewUp();
                }
},
resetIgnoreHover : function() {
                this.ignoreHoverEvent = false;
},
onHover : function(event) {
                if (!this.ignoreHoverEvent) {
                               var element = Event.findElement(event, 'LI');
                               if (this.index != element.autocompleteIndex) {
                                               this.index = element.autocompleteIndex;
                                               this.render();
                               }
                } else {
                               this.ignoreHoverEvent = false;
                }
                Event.stop(event);
}
});

