/*

    MochiRegExp: JavaScript Regular Expression Explorer
    Modified by: tectonyc

*/

  search_submit = function () {
    $("search_form").submit;
  }

  RegExpManager = function () {
    this.timer = null;
    this.searchCount = 0;
    this.originalText = null;
    bindMethods(this);
  };

  RegExpManager.prototype.initialize = function () {
    /*
       Fill in the event handlers and sample text, and do the initial update
       The reason we do the sample text here is so that "clear" really does
       clear the fields.
    */
    updateNodeAttributes("inp_regexp", {
        "value": "",
        //"onkeyup": this.findStringSoon,
        "onchange": this.findString
    });
    updateNodeAttributes("search_form", {
        "onsubmit": this.submit
    });
  };

  RegExpManager.prototype.findStringSoon = function () {
    /*
        If the user stops typing for half a second, do one update.
    */
    this.cancelTimer();
  };

  RegExpManager.prototype.cancelTimer = function () {
    /*
        Cancel the timer that waits for the user to idle for half a second.
    */
    if (this.timer) {
        this.timer.cancel();
    }
    this.timer = null;
  };

  RegExpManager.prototype.findString = function () {
    /*
        Search for our string but omit it if it's found in any sort of html tag
    */
    this.cancelTimer();
    var results = 0;
    var searchString = $("inp_regexp").value;
    if (this.searchCount == 0) {
      this.originalText = $("services_text").innerHTML;
      $("services_found").innerHTML = $("services_text").innerHTML;
      swapDOM("services_text", null);
      this.searchCount++;
    } else {
      $("services_found").innerHTML = this.originalText;
    }
    if (searchString == "" || searchString == " "){
      return null;
    }
    if (searchString.indexOf(" ") == searchString.length-1) {
      searchString = searchString.substring(0,searchString.length-1);
    }
    if (searchString.indexOf(" ") == 0) {
      searchString = searchString.substring(1,searchString.length);
    }
    if (searchString.indexOf(" ") != -1) {
      var searchStringArr = searchString.split(" ");
      for (x = 0; x <= searchStringArr.length-1; x++){
        var replaceString = "<span class=\"pink\">" + searchStringArr[x] + "</span>";
        var thisSearchString = searchStringArr[x] + "(?!_anchor)";
        var re = new RegExp(thisSearchString, "gi");
        if ($("services_found").innerHTML.match(re) != null) {
          results += $("services_found").innerHTML.match(re).length;
        }
        if ($("services_found").innerHTML.match(re) != null){
          $("results").innerHTML = "<span class=\"pink\">&nbsp;" + results + "&nbsp;</span> results";
        } else {
          $("results").innerHTML = "";
        }
        $("services_found").innerHTML = $("services_found").innerHTML.replace(re, replaceString);
      }
    } else {
      var thisSearchString = searchString + "(?!_anchor)";
      var re = new RegExp(thisSearchString, "gi");
      var replaceString = "<span class=\"pink\">" + searchString + "</span>";
      var results = $("services_found").innerHTML.match(re).length;
      $("results").innerHTML = "<span class=\"pink\">&nbsp;" + results + "&nbsp;</span> results";
      $("services_found").innerHTML = $("services_found").innerHTML.replace(re, replaceString);
    }
  };

  RegExpManager.prototype.submit = function () {
    this.findString();
    return false;
  };

  regExpManager = new RegExpManager();
  addLoadEvent(regExpManager.initialize);

