var tableSearch; // Scope it in the doc.
(function($){
  // Collection of Search Filters
  tableSearch = {
    table       : null,
    timerDelay  : 400,    
    timer       : null,
    // Select box change
    state: function(index) {
      tableSearch.table.fnFilter($(this).val(), index, true, false, false);
    },
    // Text inserted into input[text]
    textIn: function(index) {
      var val = $(this).val();

      
      if(val.length >= 3) {
        // Make sure we don't roundtrip too much
        if(tableSearch.timer) window.clearTimeout(tableSearch.timer);
        tableSearch.timer = window.setTimeout(function() {
          tableSearch.table.fnFilter(val, index, false, false, false);
        }, tableSearch.timerDelay);        
      }
        
      if(val.length >= 1)
        tableSearch.clearable.call(this, index);
    },
    // Text deleted from input[text]  
    textOut: function(index) {
      var val = $(this).val();

      
      // Make sure we don't roundtrip too much
      if(tableSearch.timer) window.clearTimeout(tableSearch.timer);
      tableSearch.timer = window.setTimeout(function() {
        tableSearch.table.fnFilter(val, index, false, false, false);
      }, tableSearch.timerDelay);
      
      if(val.length == 0)
        tableSearch.cancelClearable.call(this, index);
    },
    // Make a textfield clearable with the Sibling .cancel
    clearable: function(index) {
      $(this).next(".cancel").show();
      
      $(this)
        .next(".cancel")
          .bind("click", function() { tableSearch.clear.call(this, index); })
          .show();
    },
    // hide the sibling .cancel and clear the handler
    cancelClearable: function(index) {
      $(this)
        .next(".cancel")
          .unbind("click", function() { tableSearch.clear.call(this, index); })
          .hide();
    },
    // Click handler for the .cancel
    clear: function(index) {
      $(this)
        .prev("input[type=text]")
          .val("");
      tableSearch.cancelClearable.call($(this).prev("input[type=text]"), index);
          
      // Make sure we don't roundtrip too much
      if(tableSearch.timer) window.clearTimeout(tableSearch.timer);
      tableSearch.timer = window.setTimeout(function() {
        tableSearch.table.fnFilter("", index, false, false, false);          
      }, tableSearch.timerDelay);
    }
    
  };

  // JQuery plugin to call the Text box filters in TableSearch
  $.fn.isTextBoxFilter = function(index) {
    this
      .after("<a class='cancel'></a>")
        .next(".cancel")
          .hide()
        .end()
      .keyup(function()   { tableSearch.textIn.call(this, index); })
      .keydown(function() { this.inputLength = $(this).val().length; })
      .keyup(function(e)  { 
        if(this.inputLength > $(this).val().length)
          tableSearch.textOut.call(this, index); 
      });
      
      var val = this.val();
      if(val.length >= 1) {
        tableSearch.clearable.call(this, index);
      };      
  };
  
  // JQuery plugin to add a DatePicket and add Clear filter buttons.
  $.fn.isDateFilter = function(index) {
    this
      .after("<a class='cancel'></a>")
        .next(".cancel")
          .hide()
        .end()
      .change(function(e)  { 
        if($(this).val().length == 0)
          tableSearch.textOut.call(this, index); 
        else
          tableSearch.textIn.call(this, index);
      })
      .datepicker({ dateFormat: 'dd-mm-yy' });
      
      var val = this.val();
      if(val.length >= 1) {
        tableSearch.clearable.call(this, index);
      };
  };  
})(jQuery);
