/*

 Original Script By: http://www.enjoyxstudy.com/javascript/suggest/
 License: http://creativecommons.org/licenses/by/2.1/jp/


 Author: Kazuhiro, Osawa <ko@yappo.ne.jp>
         http://blog.yappo.jp/yappo/archives/000377.html

*/

IncSearch.Suggest_Ajax = IncSearch.Suggest;

IncSearch.Suggest_Ajax.prototype.__ajaxhack_initialize = IncSearch.Suggest_Ajax.prototype.initialize;
IncSearch.Suggest_Ajax.prototype.initialize = function (input, suggestArea, candidateList) {
  this.__ajaxhack_hook = new Array();
  this.__ajaxhack_version = function () {return '0.02';};

  this.__ajaxhack_input = input;
  this.__ajaxhack_suggestArea = suggestArea;
  if (!candidateList) candidateList = new Array();
  this.__ajaxhack_candidateList = candidateList;

  this.__ajaxhack_initialize(input, suggestArea, candidateList);

  this.register_hook('search', function (suggest) {suggest.search();});
};

// hook
IncSearch.Suggest_Ajax.prototype.register_hook = function (name, hook) {
  if (typeof hook == 'function') this.__ajaxhack_hook[name] = hook;
};
IncSearch.Suggest_Ajax.prototype.run_hook = function (name) {
  var hook = this.__ajaxhack_hook[name];
  if (typeof hook == 'function') hook(this);
};

// keydown/up (return Hack)
IncSearch.Suggest_Ajax.prototype.keyevent = function(event) {
  //$('debug').innerHTML = event.keyCode + '///' + this.activePosition;
  if (event.keyCode == Event.KEY_UP ||
      event.keyCode == Event.KEY_DOWN) {
    // key move
    this.moveActiveList(event.keyCode);
  } else if (event.keyCode == Event.KEY_ESC) {
    // cancel
    if (this.suggestList) {
      Event.stop(event);
      this.clearSuggestArea();
      this.input.value = this.inputText;
      this.oldText = this.input.value;
    }
  } else if ((event.keyCode != Event.KEY_TAB &&  this.activePosition != null && this.suggestList) ||
             event.keyCode == Event.KEY_RETURN) {
    // enter and move
    Event.stop(event);
    this.__ajaxhack_blurActionList(event);
  }
};
IncSearch.Suggest_Ajax.prototype.__ajaxhack_blurActionList = function(event) {
  this.run_hook('blur');

  if (!this.suggestList ||
      this.suggestList.length == 0){
    return;
  }

  this.unactive();
  this.oldText = this.input.value;

  setTimeout((function(){ this.clearSuggestArea(); }).bind(this), 500);
};

// blur Hack
IncSearch.Suggest_Ajax.prototype.__ajaxhack_blur = IncSearch.Suggest_Ajax.prototype.blur;
IncSearch.Suggest_Ajax.prototype.blur = function (event) {
  this.run_hook('blur');
  this.__ajaxhack_blur(event);
};

// loop Hack
IncSearch.Suggest_Ajax.prototype.checkLoop = function () {
  if (this.input.value != this.oldText) {
    this.oldText = this.input.value;
    this.__ajaxhack_search();
  }
  if (this.timer) clearTimeout(this.timer);
  this.timer = setTimeout(this.checkLoop.bind(this), this.interval);
};

// search Hack
IncSearch.Suggest_Ajax.prototype.__ajaxhack_search = function () {
  this.run_hook('search');
};

IncSearch.Suggest_Ajax.prototype.reload = function (candidateList) {
    this.candidateList = candidateList;
};

IncSearch.Suggest_Ajax.prototype.exit = function () {
  if (this.timer) clearTimeout(this.timer);
  this.timer = null;
};

IncSearch.Suggest_Ajax.prototype.getText = function () {
  return this.input.value;
  return this.oldText;
};

