SearchController = Class.create({
  initialize: function() {
    this.cleaner = /[^a-zA-Z 0-9]+/g
    $('searchSiteForm').observe('submit', this.onSearch.bindAsEventListener(this));
    $('goSearch').observe('click', this.onSearch.bindAsEventListener(this));
  }, 
  onSearch: function(event) {
    Event.stop(event);
    var tag = $('what').value ? $('what').value.strip().replace(this.cleaner, '') : "";
    if (tag.length < 3) {
      alert('Please type a valid string to search for. The word must be longer than two characters.');
      $('what').focus();
    }
    else {
      function isValidWord(text) {
        return text != 'the' && text != 'in' && text != 'at' && text != 'of' && text != 'on' && text.match(/\w+$/) 
      }
      tag = tag.toLowerCase().split(' ').findAll(isValidWord).join('-');
      document.location = '/tags/' + tag;
    }
  }
})


Event.observe(window, "load", function() { new SearchController();  }, false);
