

function OSCSearchSuggest( options ){
  this.inputSelector = 'form[name="quick_find"] input[name="keywords"]';
  this.typeDelay = 300;
  this.minimalChars = 2;
  this.searchInput = [];
  this._request = false;
  this._requestTimer = -1;
  
  if ( typeof options !='undefined' ) {
    for( var k in options ) this[k] = options[k];
  }

  this.suggestRequest = function() {
    this._request = $.ajax( {
      url: 'searchsuggest.php',
      data: $(this.form).serialize(),
      dataType: 'json',
      _caller:this,
      success:function(data){
        this._caller.suggestRender(data);
      }
    });
  }
  
  this.suggestRender = function(data) {
    if ( data.length==0 ) {
      $('div.suggestBox',$(this).parent()).html('');
      this.suggestTable = false;
      this.suggestIdx = -1;
      $('div.suggestBox',$(this).parent()).css('display','none');
    }else{
      var table = '<table width="350" height="20" class="suggestTable">';
      for( var i=0; i<data.length; i++ ) {
        table += '<tr class="suggestTableRow" rel="'+i+'" onclick="window.location.href=\''+data[i].url+'\';"><td class="suggestTableCell"><a href="'+data[i].url+'">'+data[i].name+'</a></td><td class="suggestTableCellRight">'+data[i].type+'</td></tr>';
      }
      table += '</table>';
      this.suggestTable = $('div.suggestBox',$(this).parent()).html(table);
      $('tr.suggestTableRow', this.suggestTable).hover(
        function(){ $('tr.suggestTableRowHover', $(this).parent()).removeClass('suggestTableRowHover'); $(this).addClass('suggestTableRowHover'); this.suggestIdx = $(this).attr('rel'); },
        function(){ $(this).removeClass('suggestTableRowHover') }
      );
      var idx=0;
      $('tr.suggestTableRow', this.suggestTable).each( function(){
        $(this).attr('idx',idx);
        idx++;
      } );
      /*$('tr.suggestTableRow', this.suggestTable).bind( 'click', function(){
        //$('a',$(this)).click();
        //window.location = $('a', $(this)).attr('href');
        //return false;
        //window.location.href = $('a', $(this)).attr('href');
      } );*/
      $('tr.suggestTableRow:first', this.suggestTable).addClass('suggestTableRowHover');
      this.suggestIdx = '0';
      this.suggestTable.css('display', 'block');
    }
  }

  this.suggestUp = function() {
    if (this.suggestTable) {
      $('tr.suggestTableRowHover', this.suggestTable).removeClass('suggestTableRowHover');
      var tIdx = parseInt(this.suggestIdx)-1;
      var newSelected = $('tr.suggestTableRow[idx="'+tIdx+'"]', this.suggestTable);
      if ( newSelected.length>0 ) {
        this.suggestIdx = tIdx;
        newSelected.addClass('suggestTableRowHover');
      }else{
        $('tr.suggestTableRow[idx="'+(tIdx+1)+'"]', this.suggestTable).addClass('suggestTableRowHover');
      }
    }
  }
  this.suggestDown = function() {
    if (this.suggestTable) {
      $('tr.suggestTableRowHover', this.suggestTable).removeClass('suggestTableRowHover');
      var tIdx = parseInt(this.suggestIdx)+1;
      var newSelected = $('tr.suggestTableRow[idx="'+tIdx+'"]', this.suggestTable);
      if ( newSelected.length>0 ) {
        this.suggestIdx = tIdx;
        newSelected.addClass('suggestTableRowHover');
      }else{
        $('tr.suggestTableRow[idx="'+(tIdx-1)+'"]', this.suggestTable).addClass('suggestTableRowHover');
      }
    }
  }
  this.suggestChoose = function() {
    if (this.suggestTable) {
      //$('tr.suggestTableRowHover', this.suggestTable).click();
      window.location.href = $('tr.suggestTableRowHover a', this.suggestTable).attr('href'); 
      return true;
    }
    return false;
  }
  this.suggestTyping = function(inputObj) {
    clearTimeout(inputObj._requestTimer);
    if ( inputObj.searchInput.length>0 && inputObj.searchInput.val().length>=inputObj.minimalChars ) {
      inputObj._requestTimer = setTimeout( function(){inputObj.suggestRequest()}, inputObj.typeDelay);
    }
  }
  this.suggestClose = function(inputObj) {
    var cl = function() {
      $('form[name="quick_find"] input[name="keywords"]').get(0).suggestClosing();
    }
    setTimeout( cl, 500);
    //setTimeout( this.suggestClosing, 100);
    
  }
  this.suggestClosing = function(inputObj) {
    clearTimeout(this._requestTimer);
    if (this.suggestTable) {
      $('div.suggestBox',$(this).parent()).html('');
      this.suggestTable = false;
      this.suggestIdx = -1;
      $('div.suggestBox',$(this).parent()).css('display', 'none');
    }
  }

  this.txtKeyWatchGuard = function(event){
    switch ( event.keyCode ) {
      case 13:
        if ( this.suggestChoose(this) ) {
          //event.stopPropagation();
          event.preventDefault();
          return false;
        }
        break;
    }
  }
  this.txtKeyWatch = function(event){
    switch ( event.keyCode ) {
      case 27:
        this.suggestClose(this);
      case 38:
        this.suggestUp(this);
        break;
      case 40:
        this.suggestDown(this);
        break;
      default:
        this.suggestTyping(this);
    }
  }
  
  
  this.searchInput = $(this.inputSelector);
  if ( this.searchInput.length>0 ) {
    this.searchInput.attr('autocomplete', 'off');
    this.searchInput.bind('keyup', this.txtKeyWatch);
    this.searchInput.bind('keydown', this.txtKeyWatchGuard);
    this.searchInput.bind('blur', this.suggestClose );
    //this.searchInput.after('<div style="position: absolute; overflow: auto; top: 40px; left: 800px; z-index: 10000;" class="suggestBox"></div>');
    this.searchInput.after('<div class="suggestBoxHolder"><div class="suggestBox"></div></div>');
    $.extend(this.searchInput.get(0), this);
  }

  return this;
}
