var searchSuggestionCtrl = {
  more_suggestions : "more suggestions",
  timerMove : null,
  aCont : [],
  aText : [],
  iCurSug : null,
  bIsMore : false,
  ini_with_more_text : function (more_suggestions)
  {
    this.more_suggestions = more_suggestions;
    this.winWr = _wrapper;
    this.getLoad();
  },
  ini : function (winWr)
  {
    this.winWr = typeof(winWr) != "undefined" ? winWr : _wrapper;
    //this.winWr.setOnloadListener(this);
    this.getLoad();
  },
  getLoad : function ()
  {
    this.inp = this.winWr.getElement(this.config.inpId);
    this.inp_id = this.winWr.getElement(this.config.inp_idId);
    this.div = this.winWr.getElement(this.config.divId);
    this.cont = this.winWr.getElement(this.config.divContId);
    this.searchForm = this.winWr.getForm(this.config.formName);
    if (this.winWr.bv.isIE) this.inp.addListener(this, "onkeydown", "pressKey");
      else this.inp.addListener(this, "onkeypress", "pressKey");
    this.div.move(this.inp.getX(), this.inp.getY() + this.inp.getHeight());
    if (this.winWr.bv.isIE) this.winWr.addListener(this, "onkeydown", "hideSuggestion");
      else this.winWr.addListener(this, "onkeypress", "hideSuggestion");
    this.winWr.addListener(this, "onclick", "hideSuggestion");
    this.sugLoader = this.winWr.getLoadWrapper('/search_suggestion.ajax.php', "get");
    this.sugLoader.addListener(this, "ondataload", "showSuggestion");
    this.sugLoader.addListener(this, "ondataerror", "onDataError");
  },
  pressKey : function (evtWr)
  {
    var keyCode;
    var maxLength;
    if (evtWr.evt.keyCode) keyCode = evtWr.evt.keyCode;
    else if (evtWr.evt.which) keyCode = evtWr.evt.which;
    else return;
    if (this.div.isDisplay()) {
      if (keyCode == 40 || keyCode == 38)  {
        if (this.iCurSug == null && keyCode == 40) {
          this.iCurSug = 0;
        } else {
          this._mouseOut(this.aCont[this.iCurSug]);
          if (keyCode == 40) {
            this.iCurSug++;
            maxLength = this.bIsMore ? this.aCont.length - 2 : this.aCont.length - 1;
            if (this.iCurSug > maxLength) {
              this.iCurSug = 0;
            }
          } else {
            this.iCurSug--;
            if (this.iCurSug < 0) {
              this.iCurSug = this.bIsMore ? this.aCont.length - 2 : this.aCont.length - 1;
            }
          }
        }
        this._mouseOver(this.aCont[this.iCurSug]);
        evtWr.stopBubbling();
        return;
      }
      if (keyCode == 13 && this.iCurSug != null) {
        evtWr.eventDrop();
        this._checkSuggestion(this.aText[this.iCurSug]);
      }
    } // this.div.isDisplay()
    evtWr.stopBubbling();
    this.timerMove = this.winWr.setTimeout(this.config.moveDelay, this, "sendSuggestion");
  },
  hideSuggestion : function(evtWr)
  {
    this.div.setDisplay(false);
  },
  sendSuggestion : function(evtWr)
  {
    clearTimeout(this.timerMove);
    this._sendSuggestion(0);
  },
  sendMoreSuggestions : function(evtWr)
  {
    this._sendSuggestion(1);
  },
  _sendSuggestion : function(par_more)
  {
    var val = this.inp.elm.value;
    if (val) {
      this.sugLoader.send({keywords:val,more:par_more});
    } else {
      this.hideSuggestion();
    }
  },
  showSuggestion : function(oData)
  {
    var aData = oData.data;
    this.hideSuggestion();
    this.removeCont();
    if (!aData.length) {
      return;
    }
    for (var i=0; i < aData.length; i++) {
      this.createDiv(aData[i]['product_name'], aData[i]['product_id']);
    }
    if (oData.more) {
      this.createMoreDiv();
      this.bIsMore = true;
    }
    this.div.move(this.inp.getX(), this.inp.getY() + this.inp.getHeight());
    this.inp.elm.focus();
    this.div.setDisplay(true);
  },
  _createDiv : function(sText, sId)
  {
    var newDiv = this.winWr.doc.createElement("div");
    newDiv.appendChild(this.winWr.doc.createTextNode(sText));
    this.cont.elm.appendChild(newDiv);
    var newDivWr = this.winWr.getElmWrapper(this.cont.elm.lastChild);
    newDivWr.addListener(this, "onmouseover", "mouseOver").i = this.aCont.length;
    newDivWr.addListener(this, "onmouseout", "mouseOut").i = this.aCont.length;
    this.aCont[this.aCont.length] = newDivWr;
    this.aText[this.aText.length] = [sText, sId];
    return newDivWr;
  },
  createDiv : function(sText, sId)
  {
    var newDivWr = this._createDiv(sText, sId);
    newDivWr.addListener(this, "onclick", "checkSuggestion").aInfo = [sText, sId];
  },
  createMoreDiv : function()
  {
    var newDivWr = this._createDiv(this.more_suggestions);
    newDivWr.addListener(this, "onclick", "sendMoreSuggestions");
    newDivWr.setClass('divSearchSubHeader');
  },
  removeCont : function()
  {
    var cont = this.cont.elm;
    for (var i = 0; i < this.aCont.length; i++) {
      this.aCont[i].removeListener(this, "onmouseover");
      this.aCont[i].removeListener(this, "onmouseout");
      this.aCont[i].removeListener(this, "onclick");
      cont.removeChild(this.aCont[i].elm);
    }
    this.aCont = [];
    this.aText = [];
    this.bIsMore = false;
    this.iCurSug = null;
  },
  onDataError : function(evtWr)
  {
    //alert("Error!");
  },
  mouseOver : function(evtWr, par)
  {
    if (this.iCurSug != null) {
      this._mouseOut(this.aCont[this.iCurSug]);
    }
    this.iCurSug = par.i;
    this._mouseOver(evtWr.elmWr);
  },
  _mouseOver : function(elmWr)
  {
    if (elmWr != null) {
      elmWr.addClass('selected');
    }
  },
  mouseOut : function(evtWr, par)
  {
    //this.iCurSug = par.i;
    this._mouseOut(evtWr.elmWr);
  },
  _mouseOut : function(elmWr)
  {
    if (elmWr != null) {
      elmWr.removeClass('selected');
    }
  },
  checkSuggestion : function(evtWr, par)
  {
    this._checkSuggestion(par.aInfo);
  },
  _checkSuggestion : function(aInfo)
  {
    this.inp.elm.value = aInfo[0];
    this.inp_id.elm.value = aInfo[1];
    this.searchForm.elm.submit();
  },
  config : {
    inpId : "inp_keywords",
    inp_idId : "inp_prod_id",
    divId : "divSearchSuggestion",
    divContId : "divSuggestionContent",
    formName : "quick_find",
    moveDelay : 500
  }
}
