var SearchFilter = Behavior.create({
  initialize: function(options) {
    this.timerRunning = false;
    this.listId = options.listId;
    this.nomatchId = options.nomatchId;
  },

  reset: function() {
    $(this.listId).show();
    $(this.nomatchId).hide();
    $$('#' + this.listId + ' li').each(function(li) {
      li.show();
    });
  },

  filter: function() {
    var term = this.element.getValue().strip().toLowerCase()

    if (term.blank()) {
      this.reset();
    } else {
      var matches = 0;

      $$('#' + this.listId + ' li').each(function(li) {
        var name = li.getAttribute('data-name');
        if (name.toLowerCase().include(term)) {
          li.show();
          matches += 1
        } else {
          li.hide();
        }
      });

      if (matches == 0) {
        $(this.listId).hide();
        $(this.nomatchId).show();
      } else {
        $(this.listId).show();
        $(this.nomatchId).hide();
      }
    }
    this.timerRunning = false
  },

  maybeStartTimer: function() {
    if (!this.timerRunning) {
      this.timerRunning = true
      setTimeout(this.filter.bind(this), 250)
    }
  },

  onchange: function() { this.maybeStartTimer(); },
  onkeyup:  function() { this.maybeStartTimer(); }
});

Event.addBehavior({
  'dd.source' : function() {
    this.hide();
  },
  'span.toggle_source a:click' : function() {
    var source = this.up('dl.method').down('dd.source');
    source.toggle();
    if(source.visible()) {
      this.innerHTML = 'Hide Source';
    } else {
      this.innerHTML = 'Show Source';
    }
    return false;
  },

  'input[data-ghost]' : function() {
    var ghostValue = this.getAttribute('data-ghost');

    if(this.value != ghostValue) {
      this.removeClassName('ghosted');
    }
  },

  'input[data-ghost]:focus' : function() {
    var ghostValue = this.getAttribute('data-ghost');

    if(this.value == ghostValue) {
      this.value = "";
    }
    this.removeClassName('ghosted');
  },

  'input[data-ghost]:blur' : function(e) {
    var ghostValue = this.getAttribute('data-ghost');

    if(!this.value) {
      this.value = ghostValue;
      this.addClassName('ghosted');
    }
  },
  'input[name="filter_classes_q"]' : SearchFilter({
    listId    : 'classlist',
    nomatchId : 'noclassmatch'
  }),
  'input[name="filter_methods_q"]' : SearchFilter({
    listId    : 'methodlist',
    nomatchId : 'nomethodsmatch'
  }),
  'span.toggle_list > a:click' : function() {
    var toggle = $(this.getAttribute('data-toggle'));
    if(toggle.visible()) {
      this.innerHTML = 'Show';
      Effect.SlideUp(this.getAttribute('data-toggle'));
    } else {
      this.innerHTML = 'Hide';
      Effect.SlideDown(this.getAttribute('data-toggle'));
    }
    return false;
  },
  'dd.explanation pre' : function() {
    this.addClassName('brush: ruby');
  },
  'section > pre' : function() {
    if(this.parentNode.id != 'description') {
      this.addClassName('brush: ruby; light: true');
    }
  },
  'dd.source > code' : function() {
    var pre = $(document.createElement('pre'));

    var text = this.textContent;
    var match = /, line\s+(\d+)/.exec(text);

    if(match) {
      var line = parseInt(match[1]) - 1;
      pre.addClassName('brush: ruby; first-line: ' + line);
    } else {
      pre.addClassName('brush: c');
    }

    pre.textContent = this.textContent;
    this.parentNode.replaceChild(pre, this);
  },
  'article section#description pre' : function() {
    var div = $(document.createElement('div'));

    div.addClassName('example-code');
    this.parentNode.replaceChild(div, this);
    div.appendChild(this);

    var text = this.textContent;
    var xml_match = /^<\?xml/.exec(text);
    if(text.match(/[^\n]*\n[^\n]*/gi).length > 1) {
      if(xml_match) {
        this.addClassName('brush: xml');
      } else {
        this.addClassName('brush: ruby');
      }
    }
  }
});
