var Mbsw = { };

Mbsw.Autocompleter = Class.create(Ajax.Autocompleter, {

  initialize: function($super, element, update, url, context, options) {
    $super(element, update, url, options);
    Event.observe(this.element, 'focus', this.mbswOnFocus.bindAsEventListener(this));
    this.context = context.split(",");
  },

  installStdContextCallback: function() {
    this.options.callback = this.stdContextCallback.bind(this);
  },

  stdContextCallback: function(inputfield, querystring) {
    var acfield = document.getElementById("acfield");
    var infield = this.element;
    var ret = encodeURIComponent(acfield.name) + '=' + encodeURIComponent(infield.id);
    ret = ret + "&" + encodeURIComponent(infield.name) + '=' + encodeURIComponent(infield.value);
    for (var index = 0, l = this.context.length; index < l; ++index) {
      if (this.context[index].length > 0) {
        infield = document.getElementById(this.context[index]);
        ret = ret + "&" + encodeURIComponent(infield.name) + '=' + encodeURIComponent(infield.value);
      }
    }
    return ret;
  },

  onObserverEvent: function($super) {
    var ctxInputStr = '';
    var ctxInputElem;
    for (var index = 0, l = this.context.length; index < l; ++index) {
      if (this.context[index].length > 0) {
        ctxInputElem = document.getElementById(this.context[index]);
        ctxInputStr = ctxInputStr + ctxInputElem.value;
      }
    }
    if (ctxInputStr.length > 0) {
      var oldMinChars = this.options.minChars;
      this.options.minChars = 0;
      $super();
      this.options.minChars = oldMinChars;
    } else {
      $super();
    }
  },

  mbswOnFocus: function(event) {
    this.changed = false;
    this.hasFocus = true;
    if (this.observer) clearTimeout(this.observer);
    this.observer = setTimeout(this.onObserverEvent.bind(this), this.options.frequency * 1000);
  },

  markNext: function() {
    var scrolledUp = false;
    if (this.index < this.entryCount - 1) { this.index++; }
    else { this.index = 0; scrolledUp = true; }
    this.scrollIfNeeded(scrolledUp);
  },

  markPrevious: function() {
    var scrolledUp = true;
    if (this.index > 0) { this.index--; }
    else { this.index = this.entryCount - 1; scrolledUp = false; }
    this.scrollIfNeeded(scrolledUp);
  },

  scrollIfNeeded: function(scrolledUp) {
    var entry = this.getEntry(this.index);
    var entryTop = entry.viewportOffset()[1];
    if (( scrolledUp && (entryTop < 0)) ||
        (!scrolledUp && (entryTop + entry.getHeight() > this.getWindowHeight()))) {
      entry.scrollIntoView(scrolledUp);
    }
  },

  getWindowHeight: function() {
    var ret = 0;
    if (typeof(window.innerHeight) == 'number') {
      ret = window.innerHeight; //Non-IE
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
      ret = document.documentElement.clientHeight; //IE 6+ in 'standards compliant mode'
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight )) {
      ret = document.body.clientHeight; //IE 4 compatible
    }
    return ret;
  }

});