var QuickSearchControl = Class.create( {}, {
    
  init: function () {
  
      //Options
    this.sDefaultSearchText = '';
    
    this.bSubmitIfEmpty = true;
    
    this.iTypingDelay = 150; //msec
    
    this.iMinSearchTermLength = 3; //symbols
    
    this.iSuggBlockTopOffset = 1; //pixels

      //System variables
    this.iTypingTimeout = 0;
    
    this.oCurrSuggestion = null;
    
    this.aSuggestions = [];
    
    this.sCurrPostedKeywords = '';
  
    this.oSuggBlock = null;
    this.oSuggList = null;
  
      //Initialization                 
    if( (this._serverData.iMinSearchTermLength) && (this._serverData.iMinSearchTermLength > 0)) {
      this.iMinSearchTermLength = this._serverData.iMinSearchTermLength;
    }  
      

    if( this._serverData.bNotSubmitIfEmpty ) {
      this.bSubmitIfEmpty = false;
    }  
      
    if( (this._serverData.sDefaultSearchText) ) {
      this.sDefaultSearchText = this._serverData.sDefaultSearchText;
    }  
      
    this.oSearchForm = $('#' + this._serverData.aDom.sSearchFormId);
    this.oSearchField = $('#' + this._serverData.aDom.sSearchFieldId);
    this.oSearchFieldBlock = $('#' + this._serverData.aDom.sSearchFieldBlockId);
    this.oButtonSubmit = $('#' + this._serverData.aDom.sButtonSubmitId);
    
    this.oSearchField.focus(this.getHandler(this, 'onTextFieldFocus', [this.sDefaultSearchText]));
    this.oSearchField.blur(this.getHandler(this, 'onTextFieldBlur', [this.sDefaultSearchText]));
    
    this.oButtonSubmit.click(this.getHandler(this, 'onSubmitButtonClick'));
    
    this.oSearchField.keyup(this.getHandler(this, 'onKeywordsKeyup'));
    
    $('body').click(this.getHandler(this, 'onBodyClick'));
    
    this.oSearchForm.submit(this.getHandler(this, 'onFormSubmit'));
    
  },//This is a last punched card of init function 
  

  onKeywordsKeyup: function(oEvent) {
  
    if((oEvent.keyCode == 8) || ((oEvent.keyCode > 40) && (oEvent.keyCode > 93 || oEvent.keyCode < 91) && (oEvent.keyCode > 157 || oEvent.keyCode < 112))) {
    
      if( this.oSearchField.val().length >= this.iMinSearchTermLength ) {
      
        if(this.iTypingTimeout > 0) { 
      
          clearTimeout(this.iTypingTimeout);
        
        }
          
        this.iTypingTimeout = setTimeout(this.getHandler(this, 'getSuggestions'), this.iTypingDelay);
        
      } else {
      
        this.closeSuggBlock();
      
      }
    
    }
    
    
    if( (oEvent.keyCode == 38) && this.oSuggBlock ) {
    
      this.stepUp();
    
    }
  

    if( (oEvent.keyCode == 40) && this.oSuggBlock ) {
    
      this.stepDown();
    
    }
 

    if( oEvent.keyCode == 13 ) {
    
      this.onEnterKeyup();
    
    }

  },//This is a last punched card of onKeywordsKeyup function 
  
  
  getSuggestions: function() {
  
    this.sCurrPostedKeywords = this.oSearchField.val();
    
    var oData = {};
    
    oData[this._serverData.aActions.sQuickSearch] = true;
    oData['keywords'] = this.sCurrPostedKeywords;
    
    $.post(this._serverData.sRequestUrl, oData, this.getHandler(this.onGetSuggestionsResponse), "json"); 
  
  },//This is a last punched card of getSuggestions function 
  
  
  onGetSuggestionsResponse: function(mResponse) {
    
    this.closeSuggBlock();
    
    if(!mResponse || (this.sCurrPostedKeywords != this.oSearchField.val()) ) {
    
      return false;
     
    } else {
    
      $('body').append(mResponse.suggestions);

      this.oSuggBlock = $('#' + this._serverData.aDom.sSuggBlockId);
      this.oSuggList = $('#' + this._serverData.aDom.sSuggListId);
      
      this.oSuggBlock.click(this.getHandler(this.onSuggBlockClick));
      
      this.aSuggestions = this.oSuggList.children();
      this.aSuggestions.hover(this.getHandler(this.onSuggestionOver), this.getHandler(this.onSuggestionOut));
      
      var oSearchBlockPosition = this.oSearchFieldBlock.position();
      
      var iPositionTop = oSearchBlockPosition.top + this.oSearchFieldBlock.height() + this.iSuggBlockTopOffset; 
      var iPositionLeft = oSearchBlockPosition.left; 
      
      this.oSuggBlock.css('top', iPositionTop);
      this.oSuggBlock.css('left', iPositionLeft);
      
      this.oSuggBlock.show();
            
    }
  
  },//This is a last punched card of onGetSuggestionsResponse function 
  

  onBodyClick: function() {
  
    this.closeSuggBlock();
  
  },//This is a last punched card of onBodyClick function 
  
  
  onSuggBlockClick: function(oEvent) {
  
    oEvent.stopPropagation();  
  
  },//This is a last punched card of onSuggBlockClick function 
  
  
  onSubmitButtonClick: function() {
  
    this.oSearchForm.submit();
  
  },//This is a last punched card of onSubmitButtonClick function          
  
  
  onFormSubmit: function() {
  
    var sKeywords = this.oSearchField.val();
    
    if( this.bSubmitIfEmpty ) {

      if( sKeywords == this.sDefaultSearchText ) {
        sKeywords = '';
      }
      
      return true;
    
    } else {
    
      if( (sKeywords != this.sDefaultSearchText) && (sKeywords != '') ) {
    
        return true;
      
      }
    
    }
    
    return false;
  
  },//This is a last punched card of onFormSubmit function          

  
  onEnterKeyup: function() {
  
    if(this.oCurrSuggestion) {
    
      location.href = $(this.oCurrSuggestion).find('a:first').attr('href');    
    
    } else {
    
      this.oSearchForm.submit();

    }
    
  },//This is a last punched card of onEnterKeyup function          
  
  
  onSuggestionOver: function(oEvent, oElement) {
  
    this.highlightItem(oElement);
    
  },//This is a last punched card of onSuggestionOver function          
  
  
  onSuggestionOut: function(oEvent, oElement) {
  
    this.unhighlightItem(oElement);
    
    if($(oElement)[0] == $(this.oCurrSuggestion)[0]) {
      this.oCurrSuggestion = null;
    } 
    
  },//This is a last punched card of onSuggestionOut function          
  
  
  onTextFieldFocus: function (sDefaultText, oEvent, oElement) {
    
    var oTarget = $(oElement);
    
    if( oTarget.val() == sDefaultText ){
    
      oTarget.val('');
      
    }
  
  },//This is a last punched card of onTextFieldFocus function          
                    
  
  onTextFieldBlur: function (sDefaultText, oEvent, oElement) {
  
    var oTarget = $(oElement);
    
    if( oTarget.val() == '' ) { 
    
      oTarget.val(sDefaultText);
      
    }
  
  },//This is a last punched card of onTextFieldBlur function          
  

    //Functions
      
  closeSuggBlock: function() {
  
    if(this.oSuggBlock) {
    
      this.oSuggBlock.hide();
      
      this.oSuggBlock.remove();
      
      this.oSuggBlock = null;
      
      this.aSuggestions = [];
      
      this.oCurrSuggestion = null;
      
    }
  
  },//This is a last punched card of closeSuggBlock function 

  
  highlightItem: function(oItem) {
  
    if(oItem) {
    
      this.unhighlightItem(this.oCurrSuggestion);
    
      $(oItem).addClass('ActiveSuggestion');
    
      this.oCurrSuggestion = oItem;
      
    }
    
  },//This is a last punched card of highlightItem function 
  

  unhighlightItem: function(oItem) {
  
    if(oItem) {
    
      $(oItem).removeClass('ActiveSuggestion');
    
      /*if(oItem == this.oCurrSuggestion) {
        this.oCurrSuggestion = null;
      } */
      
    }
  
  },//This is a last punched card of unhighlightItem function 
  
  
  stepUp: function() {
  
   if(this.oCurrSuggestion) {
     
     oElement = $(this.oCurrSuggestion).prev();  
     
     if(oElement.length == 0) {
     
       oElement = this.aSuggestions[this.aSuggestions.length - 1];  
       
     }
     
   } else {
   
     oElement = this.aSuggestions[this.aSuggestions.length - 1];  
     
   }
   
   this.highlightItem(oElement);
  
  },//This is a last punched card of stepUp function 
  

  stepDown: function() {
  
    if(this.oCurrSuggestion) {
    
     oElement = $(this.oCurrSuggestion).next();  
      
     if(oElement.length == 0) {
       oElement = this.aSuggestions[0];  
     }
      
    } else {
    
      oElement = this.aSuggestions[0];
      
    }
    
    this.highlightItem(oElement);
  
  },//This is a last punched card of stepDown function 

  
  lastFunc: function() { }//This is a last punched card of lastFunc function
  
}, [Control] );